IOS push Notifications Deep linking? - ios

I have implemented Push notifications and deep link in my app in traditional method using lot of switch cases and creating controller objects and redirecting to that controller but now code is getting unmaintainable because of lot of new scenario.
So I want to know how to handle deep link with push notifications, can URL Schemes work in my case and if URL schemes can work what payload should I send in the push message.

I just did this for my app. It all hinges on how your app is architected, i.e. how you navigate. Our app has a set of "home" pages, i.e. the top level things you can do. We already had the ability to navigate from anywhere back to a home page (by popping the current VC stack and closing any/all modals and then going to the new home page). To that, we added that as each VC loads, it checks state and sees if there is something it should do (configure itself, or push another VC). Once a VC has acted on state which applies to it, it clears that state.
Once you have built your mechanism to navigate to a particular spot, when you receive a push you just set your state and call whatever function navigates you back to the home page. Then that page checks state, does the right thing, and so on.

Related

Quit the application on a specific view

I have a doubt:
I have an app with 10 views. I want that, if the user is on View1 and sends the app to the background, it terminates the application (exit (0)). But I wanted this to happen only on View1, on the other screens, if the app goes to the background and then returns, it will continue where it left off.
What can I do?
Apple's guidelines seem to be strictly against terminating your app programmatically (for example, with exit()); it would go against what iOS users expect of how apps work.
Instead, I recommend the following approach:
When the app is sent to the background (applicationWillResignActive(_:) is called), check which view controller is currently being displayed.
If it's such that you wish to start over next time the app is brought to the foreground, just reset the app window's root view controller to whatever the initial view controller of your app is (typically, it involves reloading the inital view controller from Main.stroyboard, but your setup could be different).
You can not choose at runtime whether your app goes to the background or is terminated when the user presses the home button ("multitasking"); that is set in stone in your Info.plist file at build time.
Also, remember that even if you are in the screen that you wish to preserve when the user resumes, your app might be terminated by the system while it is in the background (to reclaim scarce system resources), so in that case it will still start from the initial screen. To prevent this, you might want to check out the APIs for state preservation and restoration.
Here is another SO question asking how to find the identity of the current view controller. Why not query the current view when you receive applicationWillResignActive indicating that your app is going to move to the background and then choose the action you want?
As far as I understand your description Preserving and Restoring State is what you are looking for.
Excerpt from Documentation:
The preservation and restoration process is mostly automatic, but you need to tell iOS which parts of your app to preserve. The steps for preserving your app’s view controllers are as follows:
Required
Assign restoration identifiers to the view controllers whose
configuration you want to preserve; see Tagging View Controllers for
Preservation.
Tell iOS how to create or locate new view controller objects at
launch time; see Restoring View Controllers at Launch Time.
Optional
For each view controller, store any specific configuration data needed to return that view controller to its original configuration; see Encoding and Decoding Your View Controller’s State.
Here is a link to Preserving Your App’s Visual Appearance Across Launches

What architecture can I use to change screens in Swift when there's an event outside of the current view?

For example, I have a Notification that occurs when there is an "unauthorized" error returned from the api server that should bring the user back to the login screen. This event could appear anywhere in the program.
Do I create a new viewless controller to manage states and have it monitor for this notification and then present the login view controller?
I'm thinking about push notifications too, received by the app delegate. Some of these may cause the data model to get updated and then the screen to change. Who should make the screen change? In the past I put all this in the AppDelegate. But there must be a more elegant way!
I also found out about FlowControllers. But they don't play nicely with Interface Builder, at least according to this solution.
Please let me know if you need more specific information about my project.

Start app and navigate to the view previously displayed

I have an app (iOS, Swift) which is usually started in the morning by the user. The user will press a couple of buttons, enter some text, move to other views (using a navigation controller) and so on. When everything is filled out the right way, the user will put the iPhone into standby mode. As far as the app life cycle is concerned, it will enter the background state. When the user opens the app again, he will still be on the same page, with the same parameter, ... Everything is fine.
In case of a suspended app (due to a lack of resources or other stuff), the app will start again from the beginning and not from the view where the user has navigated to before. In such a case, what's the best way to navigate to the specific view the user was when bringing the phone into the standby mode and keep respectively build up the whole navigation stack as it was before the suspension?
You can use Apple state restoration and preservation technique for this purpose. Here you can find an example provided by Apple.
Hope this will help.

applicationDidBecomeActive keep logged in similar to instagram

What I'm looking for is a mechanism to keep my app from resetting to the root view controller every time the application gets backgrounded or goes inactive. There are many apps out there that operate this way, namely Instagram, eBay, etc.
My instincts told me initially to poke into the AppDelegate's applicationWillEnterForeground method, where I would try to present the viewcontroller I'm after, however when I instantiate the viewController, I can present it, but without the navigation controller that normally be there.
This makes me think that I need to save the "state" of the application (maybe the NavigationController's stack?) and then restore the stack somehow when it gets relaunched.
I have watched the execution timings of each event and notice that closing the application and relaunching it will start the application fresh. I assume that my NSUserDefaults are still in place and thus could be checked for a logged in user. This could help determine which view in the navigation controller to push to (either login or dashboard).
Any direction is greatly appreciated.
The most revealing answer in this post was the use of some storage (NSUserDefaults) in order to store persistent data across uses.
For my specific case, this was storing a key holding user info. Then when the application loads, the would-be first view comes up, but if that key is missing, will modally pull a login view in front of it.
To do this I would use NSUserDefaults to store the current view of the app and then switch to that view when your app finishes launching. See the answers to this question: Swift: How to store user preferences?

Chromecast - return to homepage when done casting

I'm writing an iOS app that casts pictures. I want to make it in a way that when the user quits the picture viewing scene, it goes back to the homepage (the one displaying app's name) on the big screen. I've tried calling the stop method for mediaControlChannel and it's not going back to the homepage. So apparently this is not the one I'm looking for. So which method should I call to make it go back to homepage?
SDK doesn't have any understanding of your "homepage"; that is a concept in your app so you need to write your receiver so that if user ends the process of viewing picture, it switches to that page. Whether receiver on its own can determine that the above process has ended or sender has to signal the receiver depends on your application and its details but regardless, going to your home page is an action that you have to define and handle.

Resources