1

everyone!

When my app in foreground I want it to show alert when DidReceiveLocalNotification triggered.

I can add alert to mimic local notifications in AppDelegate.swift, but the problem is I don't know how to add method from my ViewController to UIAlertAction closure (see commented line), to finish animation when timer stopped.

My code below:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {


    let alertTimerEnds = UIAlertController(title: "Timer finished!", message: nil, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "OK", style: .Default) { finished in

        print("You've pressed OK button")

        //self.ViewController().finishAnimation()

    }

    alertTimerEnds.addAction(okAction)
    self.window?.rootViewController?.presentViewController(alertTimerEnds, animated: true, completion: nil)

}

Maybe I should do it in ViewController usind AppDelegate?

    let someAppDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
    someAppDelegate?.application(UIApplication.sharedApplication(), didReceiveLocalNotification: UILocalNotification) { code for alert }
odvan
  • 59
  • 2
  • 8
  • Do you know didReceiveLocalNotification calls only when you tap on localnotification? What exactly you want to do ? – Hasya Aug 11 '16 at 12:38
  • @Hasya Didn't know about that. My code works, showing alert when app in foreground and received notification. At least in simulator. The problem is, I want to use method in this alert, to finish animation. See commented line. This method located in main ViewController. My friend suggest me to use NSNotification in appdeleagete and then ViewController just need to be tuned to react on this particular NSNotification. Will try it later. – odvan Aug 11 '16 at 16:15
  • Could anyone enlight me why the h&ll I can't edit my question for typo's? – odvan Aug 11 '16 at 16:24

2 Answers2

0

If you want to do that

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
{

    var viewController : UIViewController = (application.keyWindow?.rootViewController)!

    while ((viewController.presentedViewController) != nil) {

        viewController = viewController.presentedViewController!
    }

    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in}))

    viewController.presentViewController(alert, animated: true, completion: nil)

})

Show UIAlertController in didReceiveLocalNotification method

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Try with below code, this will display alert on viewcontroller which is present at a moment.

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    var latestViewController : UIViewController!
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    if let viewControllers = appDelegate.window?.rootViewController?.presentedViewController {

        latestViewController = viewControllers as UIViewController

    }
    else if let viewControllers = appDelegate.window?.rootViewController?.childViewControllers {

        latestViewController = viewControllers.last! as UIViewController

    }

    //var alert: UIAlertView!

    //alert = UIAlertView(title: "Title", message:"Message , delegate: nil, cancelButtonTitle:"Ok" )

    //alert.show()

    let alert = UIAlertController(title: "Title", message:"Message", preferredStyle: .Alert)
    let action = UIAlertAction(title: "OK", style: .Default) { _ in
    // Put here any code that you would like to execute when
    // the user taps that OK button (may be empty in your case if that's just
    // an informative alert)
    }

    alert.addAction(action)
    latestViewController.presentViewController(alert, animated: true){}

}

How do I migrate from UIAlertView (deprecated in iOS8)

Community
  • 1
  • 1
Hasya
  • 9,792
  • 4
  • 31
  • 46