无法处理谷歌签到Swift 3

时间:2016-10-19 16:21:01

标签: ios xcode firebase oauth

我是iOS新手,在Swift 3中遇到app委托URL处理问题,我真的可以使用一些指针。

以下代码在Swift 2.3中运行得非常好:

func application(application: UIApplication,
  openURL url: NSURL, options: [String: AnyObject]) -> Bool {
    return GIDSignIn.sharedInstance().handleURL(url,
        sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
        annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}


func application(application: UIApplication,
  openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication,
                                        UIApplicationOpenURLOptionsAnnotationKey: annotation]
    return GIDSignIn.sharedInstance().handleURL(url,
        sourceApplication: sourceApplication,
        annotation: annotation)
}

当我直接从Firebase网站粘贴swift 2.3代码时,Xcode会提示我许多方法已经改变。特别是options对象。

在Swift 2.3中,我可以使用options[UIApplicationOpenURLOptionsSourceApplicationKey]访问源应用程序,使用options[UIApplicationOpenURLOptionsAnnotationKey]

访问注释

Xcode提示我将其更改为UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as StringUIApplicationOpenURLOptionsKey.annotation._rawValue as String

然而,当我进行建议的更改时,Google会返回一个网址,但没有任何反应。而不是重定向到应用程序,它继续进入本地谷歌网站,对我来说:www.google.co.nz

完整的Swift 3版本我看起来像这样:

func application(application: UIApplication,
                 openURL url: NSURL,
                 options: [String: AnyObject]) -> Bool {
    print(UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String)
    print(UIApplicationOpenURLOptionsKey.annotation._rawValue as String)

    return GIDSignIn.sharedInstance().handle(url as URL!,
                                             sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String] as! String!,
                                             annotation: options[UIApplicationOpenURLOptionsKey.annotation._rawValue as String])
}

func application(application: UIApplication,
             openURL url: NSURL,
             sourceApplication: String?,
             annotation: AnyObject?) -> Bool {
    var options: [String: AnyObject] = [UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String: sourceApplication as AnyObject,
                                        UIApplicationOpenURLOptionsKey.annotation._rawValue as String: annotation!]

    print(UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String)
    print(UIApplicationOpenURLOptionsKey.annotation._rawValue as String)

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

5 个答案:

答案 0 :(得分:18)

Xcode 8 Swift 3

如果您使用多个网址计划以及Google登录,请按照以下方式使用:

func application(_ app: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    if(url.scheme!.isEqual("fbXXXXXXXXXXX")) {
        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])
    }
}

答案 1 :(得分:2)

Swift 4,Xcode 9

如果您使用多个URLSchemes用于 facebook Google 登录:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
   let fbSignIn     = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
   let googleSignIn = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)

   return fbSignIn || googleSignIn
}

答案 2 :(得分:1)

用于快速5 Xcode 10.3(最新的Google登录)

<?php

 if(isset($_POST['submit']))
 {
   $input = $_POST['submit'];
   print exec("sh /var/www/html/script.sh $input");
 }
?>

<form action="" method="POST">
<input type="text" name="submit" value="">
</form>

要打开“按钮”上的登录屏幕,请点击

@available(iOS 9.0, *)
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        let googleDidHandle = GIDSignIn.sharedInstance().handle(url)

        return googleDidHandle
    }

    func application(_ application: UIApplication,open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let googleDidHandle = GIDSignIn.sharedInstance().handle(url)

        return googleDidHandle
    }

答案 3 :(得分:0)

尝试在AppDelegate中的“signIn”函数中执行任何操作。

Google向您发送一些数据后:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
            withError error: NSError!) {

    if (error == nil) {
        // Perform any operations on signed in user here.
        let userId = user.userID
        // Perform some operation to change the scene.
        // ........

    }
    else {
        print("\(error.localizedDescription)")
    }
}

也许下面的代码可以给你一些想法:

let myStoryBoard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)

let protectedPage = myStoryBoard.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController

let protectedPageNav = UINavigationController(rootViewController: protectedPage)

let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

appDelegate.window?.rootViewController = protectedPageNav

???我参考了YouTube的视频“使用Google帐户登录按钮.Swift中的示例。视频2”,由“Sergey Kargopolov”提供。

和...

我有一个与此类似的问题并没有解决它......

祝你好运! :)

答案 4 :(得分:0)

虽然n.by.n的答案还可以,但我在另一个帖子中看到了更好的答案(Swift 2.3):

if GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) {
    return true
} else if something else {
    return true
} else {
    return false 
}