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

[英]Retrieve google user from ios extension


I am trying to create a share extension for my application which requires to login to Google from the extension. I've setup the sharing group keychain and am able to write from the main application and read the extension target. But I can't login to Google from the extension because GIDSignIn.sharedInstance().hasAuthInKeychain() always returns false.

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

Is there any way to login to Google from an extension and how do I do that? Any help would be appreciated.

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

3 个解决方案

#1


4  

1. In Bridging-Header.h

import <GoogleSignIn/GoogleSignIn.h>
import <Google/Core.h>

2. In AppDelegate.swift

import Google

In application:didFinishLaunchingWithOptionslaunchOptions: configure the GGLContext object:

在应用程序中: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
}

Then, add a GIDSignInButton view to your app.

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

Lastly, in the view controller, implement the signIn:didSignInForUser: delegate method that will be called when the sign-in button is tapped: when authorizing the app.

最后,在视图控制器中,实现signIn:didSignInForUser:delegate方法,该方法将在点击登录按钮时调用:授权应用程序时。

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

3. Sharing Credentials between apps/extensions

When you sign-in the Google framework will have to use native iOS methods to add the new credentials to the iOS Keychain. Thus they will be using the SecItemAdd(_:_:) method that will add one or more items to a keychain.

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

To access the same keychain item in both the app and the extension, you need to enable the "Keychain Sharing" for both the app and the extension from the Xcode's Capabilities section in your project settings. When you do this, Xcode will probably want to update your app ID and provisioning profiles, because they need to reflect this new capability. You'll probably have to reauthorize the app (Step 2) to get the credentials into the right group.

要在应用程序和扩展程序中访问相同的钥匙串项,您需要在项目设置中的Xcode功能部分为应用程序和扩展程序启用“钥匙串共享”。执行此操作时,Xcode可能希望更新您的应用程序ID和配置文件,因为它们需要反映这一新功能。您可能需要重新授权应用程序(步骤2)才能将凭据输入正确的组。

Apple Documentation clearly states:

Apple文档明确指出:

If you want the new keychain item to be shared among multiple applications, include the kSecAttrAccessGroup key in the attributes dictionary. The value of this key must be the name of a keychain access group to which all of the programs that will share this item belong.

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

When you use Xcode to create an application, Xcode adds an application-identifier entitlement to the application bundle. Keychain Services uses this entitlement to grant the application access to its own keychain items. You can also add a keychain-access-groups entitlement to the application and, in the entitlement property list file, specify an array of keychain access groups to which the application belongs.

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

4. Extra Hints from Google that I haven't mentioned above.

Please see "Google Sign-In for iOS". Here is sample code to use GIDSignIn:

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

  1. Get a reference to the GIDSignIn shared instance: GIDSignIn *signIn = [GIDSignIn sharedInstance];
  2. 获取对GIDSignIn共享实例的引用:GIDSignIn * signIn = [GIDSignIn sharedInstance];
  3. Set the OAuth 2.0 scopes you want to request: [signIn setScopes:[NSArray arrayWithObject:@"https://www.googleapis.com/auth/plus.login"]];
  4. 设置要请求的OAuth 2.0范围:[signIn setScopes:[NSArray arrayWithObject:@“https://www.googleapis.com/auth/plus.login”]];
  5. Call [signIn setDelegate:self];
  6. 调用[signIn setDelegate:self];
  7. Set up delegate method signIn:didSignInForUser:withError:.
  8. 设置委托方法signIn:didSignInForUser:withError:。
  9. Call handleURL on the shared instance from application:openUrl:... in your app delegate.
  10. 从应用程序:openUrl:...在您的app delegate中调用共享实例上的handleURL。
  11. Call signIn on the shared instance;
  12. 在共享实例上调用signIn;

#2


0  

To do this you need to enable Keychain Sharing in your project's "Capabilities" pane. This will allow both the extension and the main app to share the password.

为此,您需要在项目的“功能”窗格中启用Keychain Sharing。这将允许扩展程序和主应用程序共享密码。

Apple's documentation on Keychain Sharing is here.

Apple的Keychain Sharing文档就在这里。

#3


0  

No answer until now. I finally rewrite the Google login by using Aerogear framework. Now I can able to login from both main target and extension target. This also fix this google logout issue.

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


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/08/25/d1e8976a5adbbbc7340343c177ce5e5f.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号