[Swift]Group Orders訂飲料 #2 Firebase SDK, Facebook SDK

新版本加上了使用者登入的功能。可以透過Firebase建立帳號、登入,也可以使用Facebook登入。

Firebase

Firebase setup

按照官方的教學一步一步設定,就可以成功安裝。

要注意的是,提供下載的google info plist檔案是每個firebase專案專屬的,裡面包含了一組API Key,要是從其他專案複製這個檔案會出現問題。

message = “API key not valid. Please pass a valid API key"

Register

firebase在create user時只能設定password和email,想要在進入表單後自動填入使用者名稱,就必須要為它設定display name。因此在create user的closure中使用createProfileChangeRequest()來update user 資料。

註冊完成後要登入並轉跳到下一頁,需要花較長的時間,因此在按下submit button時加入了indicator並有旋轉的動畫,這會在viewWillDisappear中消失。

Login

Error

若使用者輸入的資料有問題,會跳出Alert,顯示firebase出現的localised description。

Facebook Login

根據Firebase及Facebook官方的指引在做Facebook login button時遇到了很多問題。雖然網路上非常多中、英文的Facebook login教學文章,但因為版本、語言的差異,沒有哪個人的版本是可以直接複製貼上的。像是Facebook官方提供的custom login button documentation只有Objective-C版本,沒有Swift,只好看Apple官方古老的Objective-C documentation,以及Udemy的課程iOS 12 & Xcode 10 — Complete Swift 4.2 & Objective-C Course來惡補一下基礎概念,再參考許多人的寫法,終於寫出了一個能夠成功登入的版本。

Issue 1

其中一個遇到的問題是顯示要將FacebookAutoLogAppEventsEnabled及FacebookAdvertiserIDCollectionEnabled設定成True。在info.plist中進行設定即可。

Please set a value for FacebookAutoLogAppEventsEnabled. Set the flag to TRUE if you want to collect app install, app launch and in-app purchase events automatically.You haven’t set a value for FacebookAdvertiserIDCollectionEnabled. Set the flag to TRUE if you want to collect Advertiser ID for better advertising and analytics results.

Issue 2

另一個問題是在試著用Facebook login登入時,因為模擬器之前已經登入了別的app的Facebook test user帳號,而顯示了User is not allowed to see the application

因為test user是每個app專案專屬的,一旦模擬器中的Facebook已經以另一個專案的test user登入,就會出現這樣的錯誤。這時開啟模擬器的Safari從Facebook網頁登出,就可以正常登入了。

References:

Firebase login

Facebook login

--

--

Penny Ng
彼得潘的 Swift iOS / Flutter App 開發教室

A passionate self-taught  iOS developer. Write to record my learning and share the knowledge