最后用最新的 Google+ Sign SDK 解决了问题,并且该应用程序也得到了 Apple 的批准。我正在发布适用于iOS 9和iOS 8的解决方案。
使用CocoaPods进行集成。
pod 'Google/SignIn'
要开始登录,您必须执行此处开始集成部分中提到的完全相同的步骤
现在在“添加登录”部分,我希望在我的自定义类中使用一些自定义按钮UIViewController来启动登录过程。在 Google 的开发者链接中,他们只在 AppDelegate 中重定向。所以为了避免这种情况,我不会GIDSignInDelegate在我的AppDelegate课堂上使用
我只会在以下两种方法中进行更改AppDelegate
//This is available for iOS 9 and above. So we have to use this method if we are integrating Google Sign In in iOS 9
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject])
//This is available prior iOS 9 and is available for iOS 8,7 etc. This is a deprecated method for iOS 9. You have to override this too if your app supports iOS 8 platform.
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 {
// Fallback on earlier versions
}
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 {
}
有一个用于 Google + 登录的自定义 UIButton,其定义为
@IBAction func googleLoginButtonPressed(sender: AnyObject) {
// Initialize sign-in
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
//assert(configureError == nil, "Error configuring Google services: \(configureError)")
if configureError != nil {
//Handle your error
}else {
GIDSignIn.sharedInstance().shouldFetchBasicProfile = true
GIDSignIn.sharedInstance().clientID = kClientId
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
//This did the trick for iOS 8 and the controller is presented now in iOS 8
//We have to make allowsSignInWithBrowser false also. If we dont write this line and only write the 2nd line, then iOS 8 will not present a webview and again will take your flow outside the app in safari. So we have to write both the lines, Line 1 and Line 2
GIDSignIn.sharedInstance().allowsSignInWithBrowser = false //Line 1
GIDSignIn.sharedInstance().allowsSignInWithWebView = true //Line 2
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) {
// Perform any operations on signed in user here.
let userId = user.userID // For client-side use only!
let idToken = user.authentication.idToken // Safe to send to the server
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!) {
//Perform if user gets disconnected
}
现在这将在 iOS 8 和 9 中运行,而无需将您的应用程序移到 Safari 之外以在 Google + 中登录。