I use URL scheme to open app from today widget. Everything worked fine - when widget was tapped, app loaded and this method from AppDelegate was executed.
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {}
Later I added url scheme for facebook login and app is still opened from widget but this method from AppDelegate is not being executed. Facebook login works fine.
My info.plist part about url schemes looks like this now.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>MY_URL_SCHEME</string>
<string>fb{someid}</string>
</array>
</dict>
</array>
Update: I found out that Facebook also need this method in AppDelegate
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return SDKApplicationDelegate.shared.application(application, open: url, options: options)
}
So it also captures launching from widget. How should I use both this methods?
We can't implement both methods in an AppDelegate. From iOS 9.0, below method has been introduced.
There are a bunch of keys which this delegate have in options dictionary like "UIApplicationOpenURLOptionsKey", "UIApplicationOpenURLOptionsAnnotationKey" etc which you can use according to your need. You can make use of dictionary "options" given in a method like this :-
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
if ([url.scheme isEqualToString:@"YOUR_FACEBOOK_ID"]){
return [[FBSDKApplicationDelegate sharedInstance] application:app openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
return NO;
}
SWIFT CODE :-
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.scheme == "YOUR_FACEBOOK_ID"{
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[.sourceApplication] as! String!, annotation: options[.annotation])
}
return false
}