处理openURL:使用Facebook和Google

时间:2013-06-19 18:00:15

标签: ios facebook cocoa-touch oauth-2.0

我的应用中的用户可以使用2种服务登录:Facebook或Google

然而,

一切正常:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation: (id)annotation {
    ...
}

我应该决定拨打Facebook回拨或Google回调

如果用户拥有应用程序,那么它很容易,而不是由sourceApplication决定的 但是当没有(没有本地Facebook帐户链接,没有FB应用程序,没有GooglePlus应用程序)时,它链接到浏览器:(我不知道它是来自Facebook还是谷歌

有没有办法决定打电话?像

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation: (id)annotation {

    // how to decide?
    if (facebook) {

        return [FBSession.activeSession handleOpenURL:url];

    } else if (google) {

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

    }

}

10 个答案:

答案 0 :(得分:34)

我们不需要明确需要检查网址,下面的代码就是这样做的: -

- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation
{

    if ([GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation]) {
        return YES;
    }else if([FBAppCall handleOpenURL:url sourceApplication:sourceApplication]){
        return YES;
    }

    return NO;
}

答案 1 :(得分:5)

您可以尝试以下操作:

if ([[url absoluteString] rangeOfString:@"<Your Google Bundle ID>"].location ==            NSNotFound)
{
    // Call FBAppCall's handleOpenURL:sourceApplication to handle Facebook app responses
    BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
    // You can add your app-specific url handling code here if needed
    return wasHandled;
}
else
{
    return [GPPURLHandler handleURL:url
                  sourceApplication:sourceApplication
                         annotation:annotation];
}
return YES;

中调用上述方法
(BOOL)application:(UIApplication *)application
          openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
       annotation:(id)annotation 

appDelegeate.m

基本上,这将是检查网址前缀,然后调用google +方法,如果网址前缀是你的谷歌+包ID,如果没有,它将调用fb方法。 希望这有帮助

答案 2 :(得分:4)

方法&#34;应用程序:openURL:sourceApplication:annotation:&#34;从iOS9弃用。所以现在你可以使用。

- (BOOL)application:(UIApplication *)app
        openURL:(NSURL *)url
        options:(NSDictionary *)options {
       // For Google sign in SDK
       if ([[GIDSignIn sharedInstance] handleURL:url
                               sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                      annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]) {
          return YES;
       // For Facebook SDK
       }else if ( [[FBSDKApplicationDelegate sharedInstance] application:app
                                                          openURL:url
                                                sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                       annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]){
          return YES;
      //For Google plus SDK
      }else if ([GPPURLHandler handleURL:url
                       sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                              annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]){
          return YES;
      }

     return NO;
}

答案 3 :(得分:3)

对于Swift用户,(来自user2144179的想法)

导入框架

import Firebase
import GoogleSignIn
import FacebookCore // (FBSDKCore's alternative for swift)

并在您的委托方法中

// when your target is under iOS 9.0
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    let isFBOpenUrl = SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
    let isGoogleOpenUrl = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)
    if isFBOpenUrl { return true }
    if isGoogleOpenUrl { return true }
    return false
}

或者你可以使用另一个&#39;打开网址:&#39;方法,如果你的目标是9.0+。 SDK还包括一种方法。

Firebase参考:https://firebase.google.com/docs/auth/ios/google-signin

Facebook参考:https://developers.facebook.com/docs/swift/reference/classes/applicationdelegate.html

答案 4 :(得分:0)

您可以尝试使用Swift 4.2版本:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool 
{
    if (url.scheme?.hasPrefix("fb"))! {
        return SDKApplicationDelegate.shared.application(app, open: url, options: options)
    } 
    else
    {
        return GIDSignIn.sharedInstance().handle(url as URL?, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                                 annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }           
}

答案 5 :(得分:0)

您可以只让Google SDK或Facebook SDK尝试处理,如果该SDK无法处理,则允许另一个SDK尝试:

    @available(iOS 9.0, *)
private func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any])
    -> Bool {
        let handled: Bool = SDKApplicationDelegate.shared.application(application, open: url, options: options)

        if handled { return handled }

        return GIDSignIn.sharedInstance().handle(url,
                                                 sourceApplication:options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                                                 annotation: [:])
}

//deprecated method iOS 8 and older
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    let handled: Bool =  SDKApplicationDelegate.shared.application(application,
                                                                   open: url,
                                                                   sourceApplication: sourceApplication,
                                                                   annotation: annotation)

    if handled { return handled }

    return GIDSignIn.sharedInstance().handle(url,
                                             sourceApplication: sourceApplication,
                                             annotation: annotation)
}

答案 6 :(得分:0)

已接受答案的iOS 9.0及更高版本的Swift版本可能是这样的:

import FacebookCore

[...]

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if (GIDSignIn.sharedInstance().handle(url)) {
            return true
        } else if (ApplicationDelegate.shared.application(app, open: url, options: options)) {
            return true
        }

        return false
    }

尝试使用每个SDK处理URL,第一个识别该URL的URL将结束执行并返回true。如果没有SDK可以处理该URL,则返回false。

我希望它能对某人有所帮助

Xavi

答案 7 :(得分:0)

这可能是最简单的解决方案,

hidden

答案 8 :(得分:-2)

答案 9 :(得分:-4)

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
   sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{


   if( [GPPURLHandler handleURL:url
                  sourceApplication:sourceApplication
                     annotation:annotation])
   {
       return [GPPURLHandler handleURL:url
                     sourceApplication:sourceApplication
                            annotation:annotation];
   }
    else if([[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation])
    {
        return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                              openURL:url
                                                    sourceApplication:sourceApplication
                                                           annotation:annotation];
    }
    return NO;
}