Triggering a specific action when the app enters foreground from a local notification in iOS? (using swift)

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.

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?

  • Fit image of random size into a UIWebview (IOS)
  • how to get correct hight when we have some content on UIWebView in swift iOS?
  • Stop the auto resize of the uiwebview web content when focus on input field
  • Playing youtube videos on iOS
  • Ignoring NSURLErrorDomain error -999 does not work in UIWebView
  • Refresh a UIWebView
  • 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!
    }
    

    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()");
    }
    

    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.

    2 Solutions Collect From Internet About “Triggering a specific action when the app enters foreground from a local notification in iOS? (using swift)”

    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)
            }
        }
    }
    

    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)
        }
    }
    

    In Swift 3, it replaces and generates the following.

        override func viewDidLoad() {
            super.viewDidLoad()
    
            foregroundNotification = NotificationCenter.default.addObserver(forName: 
            NSNotification.Name.UIApplicationWillEnterForeground, object: nil, queue: OperationQueue.main) {
               [unowned self] notification in
    
            // do whatever you want when the app is brought back to the foreground
        }