从ios扩展程序中检索Google用户

sah*_*108 20 ios ios8-share-extension google-signin share-extension

我正在尝试为我的应用程序创建共享扩展,这需要从扩展程序登录Google.我已经设置了共享组密钥链,并且能够从主应用程序写入并读取扩展目标.但我无法从扩展程序登录Google,因为GIDSignIn.sharedInstance().hasAuthInKeychain()始终返回false.

有没有办法从扩展程序登录Google,我该怎么做?任何帮助,将不胜感激.

Tec*_*eko 5

1. 在 Bridging-Header.h 中

import <GoogleSignIn/GoogleSignIn.h>
import <Google/Core.h>
Run Code Online (Sandbox Code Playgroud)

2. 在 AppDelegate.swift 中

import Google
Run Code Online (Sandbox Code Playgroud)

在application:didFinishLaunchingWithOptionslaunchOptions:配置GGLContext对象中:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
        GIDSignIn.sharedInstance().clientID = "client id"
        GIDSignIn.sharedInstance.shouldFetchBasicProfile = true
        GIDSignIn.sharedInstance().delegate = self
}
Run Code Online (Sandbox Code Playgroud)

然后,GIDSignInButton向您的应用程序添加视图。

最后,在视图控制器中,实现signIn:didSignInForUser:将在登录按钮tapped:授权应用程序时调用的委托方法。

- (void)signIn:(GIDSignIn *)signIn
    didSignInForUser:(GIDGoogleUser *)user
           withError:(NSError *)error {
  // Perform any operations on signed in user here.
  // ...
}
Run Code Online (Sandbox Code Playgroud)

3. 在应用程序/扩展程序之间共享凭据

当您登录时,Google 框架必须使用本机 iOS 方法将新凭据添加到 iOS 钥匙串。因此,他们将使用SecItemAdd(_:_:)将一个或多个项目添加到钥匙串的方法。

要在应用程序和扩展程序中访问相同的钥匙串项目,您需要从项目设置中Xcode 的功能部分为应用程序和扩展程序启用“钥匙串共享” 。当您这样做时,Xcode 可能想要更新您的应用程序 ID 和配置文件,因为它们需要反映这个新功能。您可能需要重新授权应用程序(第 2 步)才能将凭据放入正确的组中。

Apple 文档明确指出:

如果您希望在多个应用程序之间共享新的钥匙串项,请在属性字典中包含 kSecAttrAccessGroup 键。此密钥的值必须是将共享此项目的所有程序所属的钥匙串访问组的名称。

当您使用 Xcode 创建应用程序时,Xcode 会向应用程序包添加 应用程序标识符权利。Keychain Services 使用此权利授予应用程序访问其自己的钥匙串项目的权限。您还可以 向应用程序添加钥匙串访问组权利,并在权利属性列表文件中指定应用程序所属的钥匙串访问组数组。

4. 上面没有提到的来自 Google 的额外提示。

请参阅“适用于 iOS 的 Google 登录”。这是要使用的示例代码GIDSignIn:

  1. 获取对GIDSignIn共享实例的引用:GIDSignIn *signIn = [GIDSignIn sharedInstance];
  2. 设置您要请求的 OAuth 2.0 范围: [signIn setScopes:[NSArray arrayWithObject:@"https://www.googleapis.com/auth/plus.login"]];
  3. 称呼 [signIn setDelegate:self];
  4. 设置委托方法signIn:didSignInForUser:withError:。
  5. 在您的应用程序委托中handleURL从application:openUrl:...调用共享实例。
  6. 调用signIn共享实例;


sah*_*108 0

直到现在还没有答案。我最终使用Aerogear 框架重写了 Google 登录。现在我可以从主目标和扩展目标登录。这也解决了谷歌注销问题。