In the iOS you don’t open a view directly (as can be done in Android) and yes the application is opened to then open the screen you want. This is all done in your Appdelegate, sort of like this:
In the application:didFinishLaunchingWithOptions:
, which is where the lifecycle of your application starts as soon as it is opened by pressing the notification, you will have this:
if let options: NSDictionary = launchOptions {
var localNotification = options.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey) as? NSDictionary
if localNotification != nil {
handleLocalNotification(application, userInfo: localNotification!)
}
}
This indicates that your application is being opened through notification and has been terminated before, so you will receive this key that exists on launchOptions
.
The method handleLocalNotification
is what will perform the functions you want with the notification information:
func handleLocalNotification(application: UIApplication, userInfo: NSDictionary?) {
window?.rootViewController = viewController
}
Here, you have the variable userInfo
with information from your notification (if anything) and viewController
is your view, that its startup will depend on how you are doing your project, is something similar to what you should have already implemented.
And the implementation of delegate didReceiveLocalNotification
, if it has not been closed and submits to the same method we created above:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
handleLocalNotification(application, userInfo: notification.userInfo)
}
This is all done inside the right Appdelegate?
– Leonardo Rocha
That, the first section within the method
didFinishLaunchingWithOptions:
and the second is the I created, just to be apart, but you can put everything there in that condition from within.– Paulo Rodrigues
I tested and didn’t open the View... what could I be doing wrong? Inside the handleLocalNotification() I called a println() to see if it was really calling the method, but apparently not.
– Leonardo Rocha
I put a println inside Handle to see if it was calling but apparently not...
– Leonardo Rocha
Oops, sorry, forgot the implementation of delegate
didReceiveLocalNotification
. There is the situation of when the application opens after it is terminated (for when it enters bydidFinishLaunchingWithOptions
) and when it is opened without being closed. When I say closed, it is when the user presses twice the button home and terminates the application, or if your application does not allow running on background.– Paulo Rodrigues
Oh yes! I think it now works, but what I need to implement within the didReceiveLocalNotification?
– Leonardo Rocha
I put it in the edit of my answer. It will only submit to the method we already created above. I also changed the parameter
userInfo
for optional, since it may be that you did not want to pass anything in the notification, just open a screen.– Paulo Rodrigues
Thank you so much! Now it works!
– Leonardo Rocha