How to present a modal on Apple watch from the extension delegate - ios

I am updating the watch kit support of a client app for Watch OS 2.0.
The iOS app adds a custom action to local notifications that cause the watch to display a custom button if the phone is locked when the local notification fires.
When the user taps the button, the extension delegate sends a message to the phone using the WCSession sendMessage:replyHandler: method.
If the phone is still locked when the user taps the action's button, I would like to display a message to the user saying "please unlock your phone to " (details about "the stuff" are not important.)
However, in WatchKit 2.0, the Extension delegate is a separate critter from interface controllers, and runs on the watch.
All the methods I can find that display an interface controller, like presentAlertControllerWithTitle:message:preferredStyle:actions: are methods of WKInterfaceController.
How do I display a new interface controller from the Extension delegate? I'm trying to figure this out from the docs (and google searching) but so far I've struck out, despite a fairly extensive search.

I had a similar issue when trying to show UI in response to extension delegate messages.
You can get the root controller through [[WKExtension sharedExtension] rootInterfaceController] but there isn't any way to walk the controller hierarchy from the root controller so it isn't really useful if you have other controllers pushed/presented.
I ended up creating a WKInterfaceController subclass that subscribes to NSNotificationCenter on didAppear and unsubscribes on willDisappear which theoretically should mean only one controller (the visible one) would be listening at a time, though I didn't really test it thoroughly enough to guarantee there wasn't any weird corner cases.
The extension delegate sends a notification when receiving a local or remote notification and the notification callback in the interface controller just calls presentAlertControllerWithTitle:... and all my interface controllers descend from that subclass.
I was trying to show an alert when receiving a remote notification while the watch app was open and not in response to the app opening from a notification action though. I'm not sure what order the interface controller didAppear and the extension delegate handleActionWithIdentifier methods would get called in so you may not have any interface controllers listening to the notification when handling the action. If handleAction... gets called first then you might have to add some logic to the interface controller on didAppear to check if there was some pending notification to be shown and have it show it then.

Related

Perform action when opening app for the second time

When I open the app it fires the events viewDidLoad and viewDidAppear form my View Controller but when I close it and run it again it does not call any of them.
Any idea?
You need to read up on application states. Here is a link I found online outlining the different states:
http://www.techrepublic.com/blog/software-engineer/understand-the-states-and-transitions-of-an-ios-app/
What you really want is to be notified when your app becomes active.
Probably the easiest way is to implement the function applicationDidBecomeActive() in your app delegate. That will be called when your app becomes active as the foreground app either on launch, or when it returns to the foreground as the active app.
Note that if you want that notification sent to some object other than the app delegate you can listen for the UIApplicationDidBecomeActive notification.

Message notification when inside another view controller

I'm carrating on an app that was written by a programmer before me. It has only 2 view controllers: the main one displaying chat (its the root view controller) and a second one to tinker with your profile. I was asked to implement some kind of notification (preferably one that pops up at the top of the screen) to the user that a message has arrived when they are inside that profile-tinkering view controller. Must I use the APNS server technique, or can I use an easier technique by utilizing the App Delegate somehow?
Use APNS, I prefer the local notification,didReceiveLocalnotification method will get called,then display an alert
Use delegate,assign delegate to the profile controller,and implement the required method,when you have a message arrived,display an alert
When you are inside another view, the APNS wont get displayed because the application is already in foreground.
When app is in foreground and you receive an APNS, didReceiveRemoteNotification method will get called.
You can read APNS package here and notify user of this in a way you want, for e.g. you can display an alert.

Remote Notification 'application:didFinishLaunchingWithOptions:' method not called

Target: 7.0 and UP
Testing on: iPhone6 Device
I have been testing Regular Push Notifications (Remote).
I have discovered that if a user clicks on the app icon -- the "applicationDidFinishLaunching:" method is never called.
I have been reading and re-reading the guide on "Handling Local and Remote Notifications" and to confirm that I understood everything referred to: Handling Remote Notifications
It says If a user taps the default button in the alert or taps (or clicks) the app icon, then the app should call its delegate "application:didFinishLaunchingWithOptions" method. And if its a remote notification, it should call "application:didReceiveRemoteNotification:fetchCompletionHandler:".
Neither one of these methods get called if I click on the app icon.
The only method that does get called is applicationWillEnterForeground.
I do have a storyboard file. So the way it loads:
--> navigation controller --> table view controller --> so on
Am I supposed to somehow set the appDelegate in the storyboard file?
I have done a lot of research and so far I have had no luck.
My push alerts do come through so I know that it works. I just haven't been able to determine why these other methods are not being called when the user clicks on the app icon.
Any suggestions or help is appreciated.
The only solution if you want to handle when a user clicks on the app icon is to either A.) set up silent notifications or B.) Set up something on your server so that you can make a request to it to find out if there was a new update.

IOS Is there a way to send user to particular view after tapping "view" on notification?

I am using push notifications in my app and I would like to send user to particular view, not the view he last saw. Is it possible?
You need to implement the appropriate AppDelegate messages.
Specifically, you will receive the APNS payload on the application:didFinishLaunchingWithOptions:
You might also receive the payload in a different message, application:didReceiveRemoteNotification: if the application is active.
Then when you know that your app was launched because the user touched a notification, you can direct him to a specific view accordingly.
I don't think that you have control over what the user sees while app is starting app (opening animation). After the app is fully active, you can send him wherever you want to by opening proper view controllers, setting proper tab, etc...

iPhone UILocalNotification load specific view

I have my local notification working, but when I click View to come back to the app, in the app delegate I am trying to load a specific view, but it wont take. Is this possible or not? It always just comes back to the last view viewed.
When you tap "View" on the notification, it takes you back to your application, and your application shows whatever it was showing before (if it was backgrounded) or is launched.
If you need to show a particular UI in response to the notification, consider implementing the <UIApplicationDelegate> method -[<UIApplicationDelegate> application:didReceiveLocalNotification:]. In that method you can inspect the notification and transition to the appropriate interface.

Resources