【发布时间】:2016-07-06 13:50:51
【问题描述】:
我正在我的移动 ios swift 应用程序中实现谷歌登录功能,所有操作都由 AppDelegate.swift 处理。我想在登录过程中显示加载指示器,所以在我的UIViewController 中,我在 google 登录按钮上附加了一个非常简单的代码:
class ViewController: UIViewController, GIDSignInUIDelegate {
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
@IBAction func googleSignInButtonAction(sender: AnyObject) {
loadingIndicator.startAnimating()
}
override func viewDidLoad() {
super.viewDidLoad()
loadingIndicator.hidesWhenStopped = true
}
现在,当用户点击登录按钮时,它会自动显示旋转指示器并打开 Safari 视图以询问登录凭据。当用户输入它们时,他会再次被重定向到 UIViewController(现在旋转指示器已打开 - 这很棒),整个过程在 AppDelegate 中发生:
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
if (error != nil) {
print("Looks like we have a sign in error: \(error)")
}else {
//here I'm sending the google token to my backend server and in case of
//success I'm redirecting user to the protected panel -
//main window of my app:
let sb = UIStoryboard(name: "Main", bundle: nil)
if let tabBarVC = sb.instantiateViewControllerWithIdentifier("TabController") as? TabController {
self.window!.rootViewController = tabBarVC
}
上面的代码发生在AppDelegate,所以我的问题是——如何从那个地方参考ViewController中的加载指示器并在成功时停止旋转?
【问题讨论】:
-
我可能会首先提出不展示微调器的理由。
-
事情是有时记录部分需要几秒钟的时间(尤其是当互联网连接很差时)——在这种情况下,用户仍然盯着记录屏幕,当根本没有微调器时,他可能很困惑。即使是我,作为应用程序的作者,有时也会盯着屏幕思考到底发生了什么,然后我意识到哦,这只是这里的互联网很差......所以我想添加一个加载指示器,以便用户在等待期间得到通知和娱乐......
-
发布一个 NSNotification 并让你的视图控制器订阅它
-
你为什么把这段代码放在 AppDelegate 中?您可以将它放在视图控制器或单独的类中。无论如何,从上面的代码中,我看到带有微调器的视图控制器在登录时被替换。那你为什么关心微调器?
-
@Jelly 我将代码放在 AppDelegate 中,因为这是 google 建议的官方方式,请查看 developers.google.com/identity/sign-in/ios/… 无论如何,我关心微调器,因为用户在 safari 视图中可能会出现这种情况( i.stack.imgur.com/1ejAR.png )点击拒绝,然后他回到微调器已经运行的同一个视图控制器:) 在这种情况下微调器应该消失了,因此我想从 AppDelegate 隐藏它
标签: ios swift uiviewcontroller appdelegate google-signin