ios - 在运行时更改客户端 ID,Google 登录

标签 ios swift google-signin clientid

我正在开发 iOS 应用程序,该应用程序应该使用我们网站上的 API。 为了连接到它,用户应该从设备登录到他的谷歌帐户,然后我们自动连接到 API。我们使用https://developers.google.com/identity/sign-in/ios/start-integrating 获取授权

问题在于存在多个网站,并且每个网站都有不同的客户端 ID。

我尝试更改以编程方式存储 CLIENT_ID 的 .plist,但它不起作用:

@IBAction func signInButtonClicked(sender: AnyObject) {
    let signInPlistDirectory = *pathToGoogleService-Info.plist*
    let signInPlist = NSMutableDictionary(contentsOfFile: signInPlistDirectory)!
    if ((webNodeUrl.text?.hasSuffix(".testing-sites.internal")) != nil) {
        signInPlist["CLIENT_ID"] = signInPlist["TESTING_CLIENT_ID"]
    } else {
        signInPlist["CLIENT_ID"] = signInPlist["PROD_CLIENT_ID"]
    }
    signInPlist.writeToFile(signInPlistDirectory, atomically: true)
}

也许有人知道,这可能吗?

最佳答案

文档内容如下:

If you didn't add additional services when you created the configuration file, you don't need to copy it to your project. However, keep the configuration file, because it contains information that you need to set up your Xcode project.

如果您没有使用其他服务,那么您应该能够摆脱困境,而不必费心处理 plist(并不是说我确定您对问题中的示例做了什么)。

在呈现登录表单之前,您可以尝试如下操作:

let manager = GIDSignIn.sharedInstance()
manager.clientId = "CLIENT_ID_YOU_WANT_TO_USE"
manager.delegate = self
manager.uiDelegate = self
manager.shouldFetchBasicProfile = true
manager.scopes = [
    "https://www.googleapis.com/auth/plus.me"
]

manager.signIn()

我还没有尝试在同一运行时期间将 clientId 值设置为多个客户端标识符,并且没有记录它是否有效,因为它是一个非常独特的用例,但它是值得的一枪。

还请记住,您必须在构建时将所有 URL 方案添加到您的项目中,没有其他方法。

<小时/>

根据您的问题,您说您想要使用多个不同的网站(具有不同的客户端 ID),但是在代码示例中,您似乎只想在不同的开发环境(产品和测试)之间切换。如果是这种情况,那么我建议您看看 this answer 。它提供了一些有关管理多个环境的有用技巧。

关于ios - 在运行时更改客户端 ID,Google 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39233878/

相关文章:

ios - If-None-Match 和 NSURLConnection

ios - NSBinaryStoreType 有什么优点?

java - android - Google 登录无法正常工作(没有崩溃或异常)

javascript - 服务器端的 Google 登录

ios - Google+ 邀请 - iOS SDK/API - 是否有类似于 Facebook 邀请的实用方法来邀请 Google Plus 用户

ios - 隐藏UILabel而不占用任何空间

iOS swift : How to merge two shapes into a solid color shape using UIBezierPath?

swift - 如何在 Swift 中生成大范围的随机数?

iOS - 当 pic1 被 pic2 触摸时,如何让游戏结束?

android - 实现 Google 登录,并变得奇怪 "unfortunately, MyApp has stopped"