1

我正在学习用 Swift 编写 ios 应用程序。

但是当我按照 Google Developers 页面上的说明进行操作时。

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

Xcode 显示很多编译错误。

例如,指令上的这段代码需要在AppDelegate中添加:

func application(application: UIApplication,
  openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication,
                                        UIApplicationOpenURLOptionsAnnotationKey: annotation]
    return GIDSignIn.sharedInstance().handleURL(url,
        sourceApplication: sourceApplication,
        annotation: annotation)
}

这将显示

ambiguous reference to member 'subscript'

我需要将此代码更改为:

private func application(application: UIApplication,
                     openURL url: URL, options: [String: AnyObject]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url as URL!,
                                                    sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication.rawValue] as? String,
                                                    annotation: options[UIApplicationOpenURLOptionsKey.annotation.rawValue])
    }

然后它将通过编译器。

另一个编译错误是需要在 AppDelegate 中添加两个代码:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
  withError error: NSError!) {
    if (error == nil) {
      // Perform any operations on signed in user here.
      let userId = user.userID    
      // ...
    } else {
      print("\(error.localizedDescription)")
    }
}
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
  withError error: NSError!) {
    // Perform any operations when the user disconnects from app here.
    // ...
}

调试器告诉我我需要替换函数名

"signIn" 为 "sign"

它会通过编译器。

最后两个编译错误与上行相同。

编译器告诉我,我需要在 UIviewcontroller 代码中将函数“signIn”更改为“sign”。

“但”

尽管我完全按照编译器的指示,并且成功运行了模拟器,但当用户允许应用程序获取用户的数据时,我仍然无法获取数据。

编译器为我展示了这个:

application:openURL:sourceApplication:annotation:not found 的实现。请将处理程序添加到您的 App Delegate 中。类:MyClassName .AppDelegate

所以......

有谁知道为什么应用程序找不到我的处理程序?

我猜那些编译错误是ios更新到10的原因,那么我不知道改变函数名称的动作是否让我无法获取谷歌的数据。

感谢您的回答。:)

谷歌开发者页面: https ://developers.google.com/identity/sign-in/ios/sign-in?ver=swift

4

0 回答 0