Use one URL scheme. You can add different paths or arguments for different task.
For example, I've an app that displays multiple items in a today extension. If you tap an item the app is opened.
- (void)_openItem:(SPItem *)item
{
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"myapp://localhost/open_item?itemID=%@", item.itemID]];
if (url != nil)
{
[self.extensionContext openURL:url completionHandler:nil];
}
}
As you've already mentioned in you question, you need to implement - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
In my case it more or less looks like this:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
BOOL ret;
if ([url.scheme isEqualToString:@"myapp"])
{
if ([url.path isEqual:@"open_item"])
{
@try
{
NSDictionary *query = [url.query queryDictionary];
NSString* itemID = query[@"itemID"];
[self _gotoItemWithID:itemID completionHandler:nil];
}
@catch (NSException *exception) {
}
ret = YES;
}
}
else
{
ret = NO;
}
return ret;
}
As you can see, I first check, if the url scheme. If it's the one I expect, I check the path. By using different paths I'm able to implement different commands the today extension is able to execute. Each command may have different arguments. In case of the "open_item" command, I expect the parameter "itemID".
By returning YES, you tell iOS you were able to handle the URL your app was called with.
In my app [self _gotoItemWithID:itemID completionHandler:nil]
does all the need tasks to display the item. In your case you would need a function to display the second view controller.
Edit:
I forgot to mention that queryDictionary
is a method in an NSString extension. It takes a string (self
) and tries to extract URL parameter and return them as dictionary.
- (NSDictionary*)queryDictionary
{
NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];
NSMutableDictionary* pairs = [NSMutableDictionary dictionary];
NSScanner* scanner = [[NSScanner alloc] initWithString:self];
while (![scanner isAtEnd])
{
NSString* pairString;
[scanner scanUpToCharactersFromSet:delimiterSet
intoString:&pairString] ;
[scanner scanCharactersFromSet:delimiterSet intoString:NULL];
NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
if ([kvPair count] == 2)
{
NSString* key = [[kvPair objectAtIndex:0] stringByRemovingPercentEncoding];
NSString* value = [[kvPair objectAtIndex:1] stringByRemovingPercentEncoding];
[pairs setObject:value forKey:key] ;
}
}
return [NSDictionary dictionaryWithDictionary:pairs];
}