pod 'Google/SignIn'
、私は中にいくつかのカスタムボタンが欲しいですUIViewController
の私のカスタムクラスは、ログインプロセスを開始します。 Googleの開発者リンクでは、AppDelegateでのみリダイレクトされます。だから私のAppDelegate
のクラスで私はGIDSignInDelegate
を使用されることはありません、これを避けるために
私だけ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
次の2つの方法で変更を作ることになります
次のように定義は次のようになります。
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
を実装してください定義今 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
}
でグーグル+ログインのためのデリゲートメソッドの実装は今これはで動作します
@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()
}
}
あるグーグル+のログイン用のカスタムUIButtonがあり
class LoginViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
}
iOS 8と9の両方で、Google +のログイン目的でSafariの外部にアプリを移動する必要はありません。
[Google + iPhone APIにログインしてアプリを離れることなく共有]の可能な複製(http://stackoverflow.com/questions/15281386/google-iphone-api-sign-in-and-share-without-leaving-アプリ) – JAL
@ JAL cmon man。それは2013年の投稿でした。物事は大きく変わりました。私の質問を読んで、SFSafariView –
という単語を探してみてください。**あなたはリンクされた質問を読んでみませんか? 'SFSafariViewController'はiOS 9以上でのみ利用できます。 iOS 8以降の場合、あなたの答えはリンクされた質問にあります。 – JAL