处理两个应用程序:openURL:sourceApplication:annotation

时间:2014-08-28 07:43:38

标签: ios objective-c facebook swift

我有2个需要使用application:openURL:sourceApplication:annotation方法的SDK。一个是FacebookSDK,第二个是GooglePlusSDK

这些是方法:

- (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];
}

我的问题是如何将它们迁移到一个方法中?什么是最好的方式?

提前致谢。

5 个答案:

答案 0 :(得分:22)

使用逻辑OR运算符||

- (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版

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)
}

答案 1 :(得分:9)

你可能会这样做:

- (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;
}

答案 2 :(得分:6)

一种更清洁的方式(在Swift中)将是:

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
}

答案 3 :(得分:3)

您还可以检查URL

的基础前缀
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString  *)sourceApplication annotation:(id)annotation
{
if FB URL prefix
//Call FB method
else 
//Google plus method
}

答案 4 :(得分:2)

我最终做了:

- (BOOL)application:(UIApplication *)application //facebook & Google+
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return( [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation]
           ||

           [GPPURLHandler handleURL:url
                  sourceApplication:sourceApplication
                         annotation:annotation]
           );

    ;
}