2016-02-04 96 views
3

我正在使用此tutorial實施通過谷歌應用程序登錄。Google登錄後不允許重定向到應用程序

  1. 我通過cocoapods安裝了GoogleSignIn。 pod 'GoogleSignIn', '~> 2.4.0'
  2. 添加了GSignIn-Bridging-Header.h#import <GoogleSignIn/GoogleSignIn.h>裏面。
  3. 創建URL類型:enter image description here

  4. GIDSignInButton類創建一個視圖

  5. 添加代碼:

    class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate { 
    override func viewDidLoad() { 
        super.viewDidLoad() 
        GIDSignIn.sharedInstance().delegate = self 
        GIDSignIn.sharedInstance().uiDelegate = self 
        GIDSignIn.sharedInstance().clientID = "KEY" 
    } 
    
    func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { 
    if let err = error { 
        print(error) 
    } 
    else { 
        print(GIDSignIn.sharedInstance().currentUser.profile.name) 
        print(GIDSignIn.sharedInstance().currentUser.profile.email) 
        self.performSegueWithIdentifier("idSegueContent", sender: self) 
        } 
    } 
    
    
    func signIn(signIn: GIDSignIn!, didDisconnectWithUser user: GIDGoogleUser!, withError error: NSError!) { 
    
    } 
    } 
    
  6. 但是當我點擊允許

enter image description here

它將我重定向到google.com,而不是應用程序。

+0

https://developers.google.com/identity/sign-in/ios/start ?ver = swift 使用此 –

+0

嗨,你有任何錯誤? – Birendra

+0

在控制檯中沒有錯誤 – Arti

回答

2

在AppDelegate中,您需要添加的

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool 
{ 
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) 
} 

谷歌通過URL發回信息化的實施,上面的方法抓住了這個信息,併發送回應用。

+1

你能告訴我FBSDKApplicationDelegate是如何重定向谷歌登錄,當我們用Facebook登錄實施時它是錯誤的作爲用戶也接受了你的回答。 – karan

+0

FBSDK在登錄完成後重定向回你的應用程序,然後應該調用sourceApplication委託方法,然後你應該檢查e url.scheme並返回true或false。 –

+0

did not瞭解它如何工作?你能詳細說明嗎? – Simmy

2

我終於在一天的工作後解決了這個問題。 @ayman Ibrahim基本上是對的,但我認爲答案有點寬泛。 Swift/Objective-C和Google SDK的不同版本在AppDelegate中調用不同版本的canOpenURL方法。我對此並不是100%確定的,並希望得到確認或更正。這是爲我工作的那種方法的版本。我使用谷歌的SDK V4.0起,瞄準的iOS 9.1,並使用雨燕3.0:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
    let checkFB = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) 
    let checkGoogle = GIDSignIn.sharedInstance().handle(url as URL!,sourceApplication: sourceApplication,annotation: annotation) 
    return checkGoogle || checkFB 
} 

我也是用的FBSDK,所以忽略該行。

+0

另外,出於調試目的,可以儘可能多地定義該方法的版本,並使用斷點或打印來確定哪一個被調用。 – dylanthelion

+0

它工作完美,謝謝:) –

0

我確實找到了另一個答案。就我而言,在AppDelegate中我沒有實現2種委託方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

首先是委託方法,不應該實行了,它是由第二種方法所取代。但不知何故,我還是同時使用,只是添加

return [[GIDSignIn sharedInstance] handleURL:url 
            sourceApplication:sourceApplication 
              annotation:annotation]; 

在第二個,然後再以某種方式,它只是回調第一種方法則無法瀏覽到我的應用程序了。

所以我要發佈我的情況的人誰可以得到這個錯誤,我花了太長時間,找出

相關問題