基本上,我试图创建一个应用程序,允许用户通过谷歌或Facebook登录。我设法创建了整个登录过程,但是当显示Facebook个人资料图片时,我遇到了一个小问题。因此,当用户选择使用Facebook或Google(来自firstViewController.swift)登录时,他们会发送到新的视图控制器(secondViewController.swift),在那里他们可以看到他们当前的Facebook个人资料图片(只有使用FB登录时),他们也可以选择使用按钮注销。同样,当他们使用FB或Google登录时,他们可以终止应用程序,当他们再次打开应用程序时,他们会被带回第二个视图控制器,因为他们没有注销。iOS - NSNotificationCenter延迟
这一切都按预期工作,但是当我在杀死它之后重新打开应用程序(如果我使用FB登录)UIView我用来查看FB配置文件图片显示为空白约2秒,然后它会更改到实际的FB图像,因为它是有意的,我不知道如何摆脱这种延迟! 这是我的代码如下所示:
Appdelegate.swift
func applicationDidBecomeActive(_ application: UIApplication) {
FBSDKAppEvents.activateApp();
GIDSignIn.sharedInstance().signInSilently()
if (GIDSignIn.sharedInstance().hasAuthInKeychain() || (FBSDKAccessToken.current() != nil)){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "homePageVC") as! secondViewController
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = vc
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setFBProfilePictureID"), object: nil)
}
}
firstViewController.swift
@IBAction func handleCustomFBLogin() {
FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, err) in
if err != nil {
print("FB login failed", err!)
return
}
self.performSegue(withIdentifier: "loggedIn", sender: self) //sends user to the second VC
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setFBProfilePictureID"), object: nil) //post notification for the FB profile picture view in homePage VC
}
}
SecondViewController.swift
@IBOutlet weak var fbProfilePicture: FBSDKProfilePictureView! //fb profile pic UIView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(setFBProfilePicture(Notification:)), name: NSNotification.Name(rawValue: "setFBProfilePictureID"), object: nil)
}
func setFBProfilePicture(Notification: NSNotification) {
fbProfilePicture.isHidden = false
}
我相信这是网络调用Facebook API的个人资料图片需要2秒。尝试将fbProfilePicture的背景设置为一种颜色,那么你应该能够看到视图不是隐藏的,但它是被加载的图像,需要2秒。一个想法可能是缓存它? – JMIT
哦,是的,这是有道理的,谢谢你的提示! :)我会看看如何缓存它 –