Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle a custom URL in an already-open app?
    text
    copied!<p><br> I have an app based on the Utility template (i.e. Main and Flip view controllers). The Flip view allows selecting a certain item to be used on the main view. So far - works great.</p> <p>Now I tried adding a custom URL. Something to the effect of: <code>myapp://itemID=40</code> that will basically tell the main view: "no need to flip - you'll be dealing with item 40".</p> <p>I registered the URL type scheme as "<code>myapp</code>" and added the following method to the app delegate:</p> <pre><code>- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if (!url) { return NO; } NSString *urlString = [url absoluteString]; NSLog(@"URL received: %@", urlString); NSString *itemID = [urlString stringByReplacingOccurrencesOfString:@"myapp://itemID=" withString:@""]; NSLog(@"Item received: %@", itemID); [_mainViewController setStartupItem:itemID]; return YES; } </code></pre> <p>As you can see, the <code>itemID</code> is set to a property called <code>startupItem</code> in the <code>mainViewController</code>.</p> <p>I then added one line to the regular <code>application</code> method to verify that <code>startupItem</code> will be <code>nil</code> in case of no URL:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Make sure URL string is empty [_mainViewController setStartupItem:nil]; // Override point for customization after application launch. // Add the main view controller's view to the window and display. self.window.rootViewController = self.mainViewController; [self.window makeKeyAndVisible]; return YES; } </code></pre> <p>And in MainViewController.m I added code to handle the item to the <code>viewDidLoad</code> event.</p> <p>And here's my problem: this scheme works great if the app is started from a URL the first time. If it's already running, then we never reach <code>viewDidLoad</code> again and therefore don't handle that particular item, but go on as if none was passed.</p> <p>My humble question is: which <code>UIViewController</code> should I put my handling code in? Or, am I approaching all this the wrong way? Should this be handled in my model?</p> <p>As always, thanks in advance for your time!</p> <p>Guy</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload