基本上,我試圖創建一個應用程序,允許用戶通過谷歌或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
哦,是的,這是有道理的,謝謝你的提示! :)我會看看如何緩存它 –