最终,我们使用最新的Google+ Sign SDK解决了问题,并且该应用程序也已被Apple批准。我将为 iOS 9和iOS 8发布解决方案。
请使用CocoaPods进行集成。
pod 'Google/SignIn'
要开始登录,您必须按照“开始集成”部分此处提到的完全相同的步骤进行操作。
现在,在“添加登录”部分中,我想在UIViewController
的自定义类中添加一些自定义按钮以启动登录过程。在Google的开发者链接中,他们只重定向到AppDelegate。所以为了避免这种情况,我将不会在我的AppDelegate
类中使用GIDSignInDelegate
。
我将仅对AppDelegate
的以下两个方法进行更改。
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject])
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
因此,定义如下:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if #available(iOS 9.0, *) {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey] as? String)
} else {
}
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,sourceApplication: sourceApplication,annotation: annotation)
}
现在我们来到了自定义的UIViewController
类,即LoginViewController
,实现GIDSignInDelegate
和GIDSignInUIDelegate
class LoginViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
}
有一个自定义的UIButton用于Google +登录,其定义为:
@IBAction func googleLoginButtonPressed(sender: AnyObject) {
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
if configureError != nil {
}else {
GIDSignIn.sharedInstance().shouldFetchBasicProfile = true
GIDSignIn.sharedInstance().clientID = kClientId
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().allowsSignInWithBrowser = false
GIDSignIn.sharedInstance().allowsSignInWithWebView = true
GIDSignIn.sharedInstance().signIn()
}
}
现在Google+登录已经实现了委托方法
func signIn(signIn: GIDSignIn!, dismissViewController viewController: UIViewController!) {
self.dismissViewControllerAnimated(true) { () -> Void in
}
}
func signIn(signIn: GIDSignIn!, presentViewController viewController: UIViewController!) {
self.presentViewController(viewController, animated: true) { () -> Void in
}
}
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
if (error == nil) {
let userId = user.userID
let idToken = user.authentication.idToken
let fullName = user.profile.name
let givenName = user.profile.givenName
let familyName = user.profile.familyName
let email = user.profile.email
} else {
print("\(error.localizedDescription)")
}
}
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user: GIDGoogleUser!, withError error: NSError!) {
}
现在,您可以在不将应用移动到Safari上进行Google+登录目的的情况下,在iOS 8和9中使用此功能。
SFSafariViewController
仅适用于 iOS 9 及以上版本。对于 iOS 8 及以下版本,您的答案在链接的问题中。 - JALSFSafariViewController
的答案。这也是我发布的整个问题,即如何使用最新的 Google SDK 解决此问题。根据新的 SDK,我们现在不再使用GTMOAuth2ViewControllerTouch
。在 G+ 登录 SDK 的 API 参考列表中,我们甚至没有 GPPSignIn。我还提到了 2015 年 5 月推出的 Google SDK 失败的情况。 - Rajan Maheshwari