xcode 在 Swift 3 中处理 Google 登录时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40136785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble handling Google sign in Swift 3
提问by Clement
I am new to iOS and am having trouble with app delegate URL handling in Swift 3, and I could really use some pointers.
我是 iOS 新手,在 Swift 3 中处理应用程序委托 URL 时遇到问题,我真的可以使用一些指针。
The below code works perfectly fine in Swift 2.3:
以下代码在 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)
}
When I paste the swift 2.3 code straight from the Firebase website, Xcode prompts me that a lot of the methods have changed. In particular, the options
object.
当我直接从 Firebase 网站粘贴 swift 2.3 代码时,Xcode 提示我很多方法已经改变。尤其是options
对象。
In Swift 2.3, I can access the source application using options[UIApplicationOpenURLOptionsSourceApplicationKey]
and the annotation using options[UIApplicationOpenURLOptionsAnnotationKey]
在 Swift 2.3 中,我可以使用访问源应用程序options[UIApplicationOpenURLOptionsSourceApplicationKey]
和使用注释options[UIApplicationOpenURLOptionsAnnotationKey]
Xcode prompts me to change it to UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String
and UIApplicationOpenURLOptionsKey.annotation._rawValue as String
Xcode 提示我将其更改为UIApplicationOpenURLOptionsKey.sourceApplication._rawValue as String
和UIApplicationOpenURLOptionsKey.annotation._rawValue as String
Yet when I make the suggested changes, Google returns a URL, but nothing happens. Instead of redirecting to the app, it proceeds to enter the local google site, for me: www.google.co.nz
然而,当我进行建议的更改时,Google 会返回一个 URL,但没有任何反应。它没有重定向到应用程序,而是继续进入本地谷歌站点,对我来说:www.google.co.nz
The full Swift 3 version I have looks like this:
我拥有的完整 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)
}
回答by n.by.n
Xcode 8 Swift 3
Xcode 8 斯威夫特 3
If you are using multiple URL Schemes along with Google Sign In, use it like this:
如果您将多个 URL Schemes 与 Google Sign In 一起使用,请像这样使用它:
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])
}
}
回答by Aashish
Swift 4, Xcode 9
斯威夫特 4,Xcode 9
If you are using multiple URLSchemes for facebookand GoogleSignIn:
如果您为facebook和GoogleSignIn使用多个 URLSchemes :
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
}
回答by Hardik Thakkar
For swift 5 Xcode 10.3 (Latest google signin)
对于 swift 5 Xcode 10.3(最新的谷歌登录)
@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
}
To open signIn screen on Button click
要在按钮上打开登录屏幕,请单击
@IBAction func btnGoogleSigninClick(_ sender: AnyObject) {
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance()?.restorePreviousSignIn()
GIDSignIn.sharedInstance().signIn()
}
回答by seeyoulater
Try to perform any operations at function "signIn" in AppDelegate.
尝试在 AppDelegate 中的函数“signIn”处执行任何操作。
After Google sends you some data:
在 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)")
}
}
Maybe the code below can give you some idea:
也许下面的代码可以给你一些想法:
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
??? I reference from YouTube's video "Sign-in with Google Account button. Example in Swift. Video 2", that are provided from "Sergey Kargopolov".
???我参考了 YouTube 的视频“使用 Google 帐户按钮登录。Swift 中的示例。视频 2”,由“Sergey Kargopolov”提供。
and...
和...
I have a question similar to this and didn't solve it...
我有一个类似的问题,但没有解决它...
Good luck! :)
祝你好运!:)
回答by Warpzit
While n.by.n answer is okay I saw a better answer in another thread (Swift 2.3):
虽然 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
}