33

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

一切正常,但是,在:

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

我应该决定调用 Facebook 回调或 Google 回调

如果用户有应用程序,这很容易,比我由 sourceApplication 决定,但是当没有时(没有链接本机 Facebook 帐户,没有 FB 应用程序,没有 GooglePlus 应用程序),它链接到浏览器 :( 我不知道它是否是来自 Facebook 或 Google

有没有办法决定调用什么?喜欢

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

    }

}
4

10 回答 10

39

我们不需要显式检查 URL,下面的代码可以做到:

- (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;
}
于 2015-03-25T13:19:26.960 回答
9

对于 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
}

或者,如果您的目标是 9.0+,您可以使用另一个 'open url:' 方法。SDK 也包含一个方法。

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

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

于 2017-04-27T08:37:19.443 回答
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

基本上这要做的是检查 url 前缀,然后如果 url 前缀是 ur google+ bundle id 则调用 google+ 方法,如果不是,它将调用 fb 方法。希望这可以帮助

于 2014-01-31T06:04:29.933 回答
5

iOS9 已弃用方法“application:openURL:sourceApplication:annotation:”。所以现在你可以使用like。

- (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;
}
于 2017-05-30T07:56:04.510 回答
3

已接受答案的 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,第一个识别它的 SDK 结束执行返回 true。如果没有 SDK 可以处理 URL,则返回 false。

我希望它可以帮助某人,

哈维

于 2019-09-12T08:23:36.287 回答
1

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

import GoogleSignIn
import FBSDKCoreKit

    func application(_ application: UIApplication,
                 open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    if GIDSignIn.sharedInstance().handle(url) {
        return true
    } else if ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
        return true
    }
    
    return false
}

@available(iOS 9.0, *)
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, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        ) {
        return true
    }
    return false
}
于 2020-09-08T12:57:38.587 回答
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])
    }           
}
于 2018-09-13T01:41:02.437 回答
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)
}
于 2018-12-13T02:39:20.000 回答
-2

你会想要使用[[UIApplication sharedApplication] canOpenURL:]

于 2013-06-19T18:04:14.277 回答
-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;
}
于 2015-08-28T09:26:11.853 回答