I have 2 SDK's that needs to use the application:openURL:sourceApplication:annotation
method. One is FacebookSDK
and the second is GooglePlusSDK
.
Those are the methods:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
My question is how is it possible to migrate them into one method? What is the best way?
Thanks in advance.
Use logical OR operator ||
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication] || [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
Swift Version
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String!, annotation: AnyObject) -> Bool {
return FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication) || GPPURLHandler.handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}
You could probably do like this:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL shouldOpen = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
shouldOpen = shouldOpen ? shouldOpen : [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
return shouldOpen;
}
A cleaner way (here in Swift) would be:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String!, annotation: AnyObject) -> Bool {
var handled: Bool
if let facebook = url.scheme?.hasPrefix("fb") {
handled = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication)
} else {
handled = GPPURLHandler.handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}
// include any specific handling code as necessary
return handled
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With