Reputation: 21726
When iOS application is opened from some URL AppDelegates's
methods are called in such a sequence:
1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
How to know in didFinishLaunchingWithOptions
method if application was opened from URL or not. May be there are some launching options which I miss?
Upvotes: 6
Views: 6898
Reputation: 6942
Actually answers about UIApplicationLaunchOptionsURLKey
are correct but not complete. In case if user for example taps Universal Link in Messages app and is redirected into your app, you receive these launch options instead of UIApplicationLaunchOptionsURLKey
:
[
UIApplicationLaunchOptionsSourceApplicationKey: com.apple.MobileSMS,
UIApplicationLaunchOptionsUserActivityDictionaryKey: [
UIApplicationLaunchOptionsUserActivityKey: <NSUserActivity>,
UIApplicationLaunchOptionsUserActivityTypeKey: NSUserActivityTypeBrowsingWeb
]
]
So, to check if user landed into the app from URL you need this code in this case:
let isFromUrl = ((launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? NSDictionary)?[UIApplicationLaunchOptionsKey.userActivityType] as? String == NSUserActivityTypeBrowsingWeb)
and if the check passes, handle incoming URL in:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let url = userActivity.webpageURL
// do your stuff ...
}
return false
}
Upvotes: 3
Reputation: 2856
You can inspect launchOptions
passed to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
.
Look for section Launch Options Keys
in reference docs, specifically UIApplicationLaunchOptionsURLKey
Upvotes: 9
Reputation: 901
First, you should implement application:didFinishLaunchingWithOptions:
Check the URL. It should return YES if you can open it or NO if you can't.
And then implement application:handleOpenURL:
Open the URL. It should return YES if successful or NO.
Upvotes: 1
Reputation: 1260
If your app has been launch from a URL You will find a
UIApplicationLaunchOptionsURLKey
in the launchOptions
Dictionary of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
On a related note the handleOpenURL:
method is deprecated, you should use:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
Upvotes: 6