从ios扩展程序中检索Google用户 [英] Retrieve google user from ios extension

查看:133
本文介绍了从ios扩展程序中检索Google用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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,我该怎么做?任何帮助,将不胜感激。

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

推荐答案

1。在Bridging-Header.h中



1. In Bridging-Header.h

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



2。在AppDelegate.swift



2. In AppDelegate.swift

import Google

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

然后,在您的应用中添加 GIDSignInButton 视图。

Then, add a GIDSignInButton view to your app.

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

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.

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



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



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

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.

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

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.

Apple文档明确指出:


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

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.

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

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.



4。 Google未提及的额外提示。



请参阅Google登录iOS
以下是使用的示例代码 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 来自应用程序的共享实例:openUrl: ...在您的应用代表中。

  6. 在共享实例上调用 signIn ;

  1. Get a reference to the GIDSignIn shared instance: GIDSignIn *signIn = [GIDSignIn sharedInstance];
  2. Set the OAuth 2.0 scopes you want to request: [signIn setScopes:[NSArray arrayWithObject:@"https://www.googleapis.com/auth/plus.login"]];
  3. Call [signIn setDelegate:self];
  4. Set up delegate method signIn:didSignInForUser:withError:.
  5. Call handleURL on the shared instance from application:openUrl:... in your app delegate.
  6. Call signIn on the shared instance;

这篇关于从ios扩展程序中检索Google用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆