1

I am developing an application where the user can set a notification reminder to change the password of the CoreData instance to wich the reminder was set.

enter image description here

When the user hits the "First Action" button I want him to be redirected to the second and fourth view.

Heres what Im trying

Relevant code from my AirlineViewController.swift and AppDelegate.swift

AirlineViewController.swift:

@IBAction func buttonTapped(sender: AnyObject) {
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"showAMessage:", name: "actionOnePressed", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"drawAShape:", name: "actionTwoPressed", object: nil)


    var dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015;
    dateComp.month = 08;
    dateComp.day = 03;
    dateComp.hour = 21;
    dateComp.minute = 03;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!


    var notification:UILocalNotification = UILocalNotification()
    notification.category = "FIRST_CATEGORY"
    notification.alertBody = "Hi, I am a notification"
    notification.fireDate = date

    UIApplication.sharedApplication().scheduleLocalNotification(notification)

}


func drawAShape(notification:NSNotification){
    println("drawAShape")
    var view:UIView = UIView(frame:CGRectMake(10, 10, 100, 100))
    view.backgroundColor = UIColor.redColor()

    self.view.addSubview(view)
    println("drawAShape")

}

func showAMessage(notification:NSNotification){

    var message:UIAlertController = UIAlertController(title: "A Notification Message", message: "Hello there", preferredStyle: UIAlertControllerStyle.Alert)
    message.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(message, animated: true, completion: nil)
}

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Actions
    var firstAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    firstAction.identifier = "FIRST_ACTION"
    firstAction.title = "First Actions"

    firstAction.activationMode = UIUserNotificationActivationMode.Foreground
    firstAction.destructive = true
    firstAction.authenticationRequired = false

    var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    secondAction.identifier = "SECOND_ACTION"
    secondAction.title = "Second Actions"

    secondAction.activationMode = UIUserNotificationActivationMode.Foreground
    secondAction.destructive = false
    secondAction.authenticationRequired = false

    var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    thirdAction.identifier = "THIRD_ACTION"
    thirdAction.title = "Third Actions"

    thirdAction.activationMode = UIUserNotificationActivationMode.Background
    thirdAction.destructive = false
    thirdAction.authenticationRequired = false

    //Category

    var firstCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
    firstCategory.identifier = "FIRST_CATEGORY"

    let defaultActions:NSArray = [firstAction, secondAction, thirdAction]
    let minimalActions:NSArray = [firstAction, secondAction]

    firstCategory.setActions(defaultActions as [AnyObject], forContext: UIUserNotificationActionContext.Default)
    firstCategory.setActions(minimalActions as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)

    //NSSet of all our categories
    let categories:NSSet = NSSet(objects: firstCategory)


    let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge

    let mySettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>)

    UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)


    return true
}

func application(application: UIApplication!,
    handleActionWithIdentifier identifier:String!,
    forLocalNotification notification:UILocalNotification!,
    completionHandler: (() -> Void)!){

        if (identifier == "FIRST_ACTION"){

            NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)

        }else if (identifier == "SECOND_ACTION"){
            NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)

        }

        completionHandler()       
}

The problem with this code is that it just opens the last view or the first if the app is completely closed.

Can anybody help me

The Dude
  • 1,088
  • 7
  • 16
  • 30
  • possible duplicate of [Attempt to present \* on \* whose view is not in the window hierarchy](http://stackoverflow.com/questions/13350938/attempt-to-present-on-whose-view-is-not-in-the-window-hierarchy) – Shmidt Aug 03 '15 at 19:30

1 Answers1

0

There is no built-in mechanism I know of to have UILocalNotification take you directly to a specific part of your app's UI.

You can attach a userInfo dictionary to a UILocalNotification before you post it.

You can then interrogate that userInfo dictionary, figure out where it is supposed to take the user in your UI, and write custom code to take the user to that part of the app UI.

I'm doing exactly that for an app I'm developing right now for a client. I embed the information needed to let the app invoke the screen in question. (The specifics depend on the design of the app, which uses a custom forms description file to describe the hierarchy of view controllers in the app.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272