当应用程序从iOS中的本地通知进入前台时触发特定操作? (使用swift) [英] Triggering a specific action when the app enters foreground from a local notification in iOS? (using swift)

查看:365
本文介绍了当应用程序从iOS中的本地通知进入前台时触发特定操作? (使用swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新语言Swift构建iOS应用程序。现在它是一个HTML5应用程序,使用UIWebView显示HTML内容。该应用程序具有本地通知,我想要做的是当应用程序通过单击(触摸)本地通知进入前台时触发UIWebView中的特定javascript方法。

I am building an iOS app using the new language Swift. Now it is an HTML5 app, that displays HTML content using the UIWebView. The app has local notifications, and what i want to do is trigger a specific javascript method in the UIWebView when the app enters foreground by clicking (touching) the local notification.

我看过这个问题,但它似乎没有解决我的问题。我也遇到了这个问题,告诉我使用UIApplicationState,这很好,因为这将帮助我知道应用程序从通知进入前台。但是当应用程序恢复时,我如何在应用程序恢复时显示的视图的viewController中调用方法?

I have had a look at this question, but it does not seem to solve my problem. I have also come across this question which tells me about using UIApplicationState, which is good as that would help me know the the app enters foreground from a notification. But when the app resumes and how do i invoke a method in the viewController of the view that gets displayed when the app resumes?

我想做的是获取一个我的ViewController的实例,并将其中的属性设置为true。如下所示

What i would like to do is get an instance of my ViewController and set a property in it to true. Something as follows

class FirstViewController: UIViewController,UIWebViewDelegate { 
  var execute:Bool = false;
  @IBOutlet var tasksView: UIWebView!
}

在我的AppDelegate中我有方法

And in my AppDelegate i have the method

func applicationWillEnterForeground(application: UIApplication!) {
    let viewController = self.window!.rootViewController;
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("FirstView") as FirstViewController
    setViewController.execute = true;


}

所以我想做的是当应用程序再次进入前台时,我想查看执行变量并按如下方式运行方法,

so what i would like to do is when the app enters foreground again, i want to look at the execute variable and run the method as follows,

if execute{
 tasksView.stringByEvaluatingJavaScriptFromString("document.getElementById('sample').click()");
}

我应该在哪里放置逻辑代码以从webview触发javascript ?它会在viewDidLoad方法,还是其中一个webView委托方法?我试图将该代码放在viewDidLoad方法中,但布尔执行的值设置为其初始值,而不是当应用程序进入前台时在委托中设置的值。

Where should i put the code for the logic to trigger the javascript from the webview? would it be on viewDidLoad method, or one of the webView delegate methods? i have tried to put that code in the viewDidLoad method but the value of the boolean execute is set to its initial value and not the value set in the delegate when the app enters foreground.

推荐答案

如果我希望在将应用程序带回前台时通知视图控制器,我可能只需注册 .UIApplicationWillEnterForeground 通知(完全绕过app委托方法):

If I want a view controller to be notified when the app is brought back to the foreground, I might just register for the .UIApplicationWillEnterForeground notification (bypassing the app delegate method entirely):

class ViewController: UIViewController {

    private var notification: NSObjectProtocol?

    override func viewDidLoad() {
        super.viewDidLoad()

        notification = NotificationCenter.default.addObserver(forName: .UIApplicationWillEnterForeground, object: nil, queue: .main) {
            [unowned self] notification in

            // do whatever you want when the app is brought back to the foreground
        }
    }

    deinit {
        // make sure to remove the observer when this view controller is dismissed/deallocated

        if let notification = notification {
             NotificationCenter.default.removeObserver(notification)
        }
    }
}

注意,在完成关闭,我包括 [unowned self] ,以避免强大的引用循环,如果您碰巧引用 self (如果你要更新,你可能需要这样做)在一个类变量或做几乎任何有趣的事情)。

Note, in the completion closure, I include [unowned self] to avoid strong reference cycle that prevents the view controller from being deallocated if you happen to reference self inside the block (which you presumably will need to do if you're going to be updating a class variable or do practically anything interesting).

或者,如果你不想使用基于块的通知,你可以使用旧的基于选择器的方法:

Alternatively, if you don't want to use the block-based notification, you can use the old selector-based approach:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: .UIApplicationWillEnterForeground, object: nil)

    }

    func willEnterForeground(_ notification: NSNotification!) {
        // do whatever you want when the app is brought back to the foreground
    }

    deinit {
        // make sure to remove the observer when this view controller is dismissed/deallocated

        NotificationCenter.default.removeObserver(self)
    }
}

这篇关于当应用程序从iOS中的本地通知进入前台时触发特定操作? (使用swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆