iOS
...
Users
Facebookアカウントでのユーザーサインイン実装ガイド
14 分
facebookでサインイン イントロダクション 前回の ガイド https //www back4app com/docs/ios/parse swift sdk/users/sign in with google では、ユーザーがgoogleアカウントを使用して back4app アプリケーションにサインインする方法を学びました。これに続いて、facebookアカウントを使用するサインインの代替手段を追加することができます。これを実現するために、まずfacebookの開発者ページにアクセスし、xcodeプロジェクトにその機能を統合するための要件を設定します。facebookは、開発者がさまざまなアプリの中で facebookでサインイン オプションを統合するためのsdkを提供しています。 この リポジトリ では、実装しているさまざまなサインイン方法をテストできるシンプルなxcodeテンプレートを提供しています。この例はすでに ログインガイド https //www back4app com/docs/ios/parse swift sdk/users/user log in で紹介されており、プロジェクトの詳細については再度確認できます。 前提条件 このクイックスタートを完了するには、次のものが必要です: 最新のxcodeのバージョン。 back4appで作成されたアプリ。 新しいparseアプリのチュートリアル をフォローして、 back4app でparseアプリを作成する方法を学びます。 注意: parse sdk(swift)インストールチュートリアル をフォローして、 back4app に接続されたxcodeプロジェクトを作成します。 目標 facebookのsdkを使用してユーザーサインイン機能を統合することと parseswift 1 facebookのsdkの設定 xcodeプロジェクトが リンクされている back4appの アプリケーションに追加されたら、サインインフローを実装するためにfacebookのsdkを追加します。この例では、 swift package manager (spm)を使用してfacebook loginの依存関係を追加します。xcodeプロジェクトで、 ファイル>パッケージを追加… に移動し、検索バーで https //github com/facebook/facebook ios sdk https //github com/facebook/facebook ios sdk を探します。 結果に facebook ios sdk が表示されたら、 パッケージを追加 ボタンをクリックします。 次に、あなたの facebook 開発者アカウントの アプリ セクションに移動し、新しいアプリを作成します。アプリの種類を入力し、次のページに進みます。残りの情報を入力し、「 アプリを作成 」をクリックします。あなたの アプリ ページにいる間に、新しく作成したアプリを見つけ、その app id をコピーし、名前をクリックしてダッシュボードに入ります。右上には、あなたの facebook 開発者アカウントに関連付けられた client id が表示されています。この2つのidは、次の設定に必要です。 facebook がサインイン機能を設定するために必要な次の設定は、xcode の info plist ファイルにいくつかのキーと値のデータを入力することです。より正確には、あなたの xcode プロジェクトナビゲーターに移動し、info plist を見つけて ソースコード として開きます。次の値を追加します 1 cfbundleurltypes 2 3 4 cfbundletyperole 5 editor 6 cfbundleurlschemes 7 	 8 fbapp id 9 10 11 12 facebookappid 13 app id 14 facebookclienttoken 15 client token 16 lsapplicationqueriesschemes 17 18 fbapi 19 fb messenger share api 20 「 app id 」の文字列を、facebookで新しく作成したアプリに関連付けられた「 app id 」に置き換えます。「 client token 」の文字列は、「 dashboard>settings>advanced>security>client token 」にあるクライアントトークンに置き換える必要があります。 次に、「 keychain sharing 」機能をxcodeプロジェクトに追加する必要があります。これを行うには、プロジェクトナビゲーターからプロジェクトを選択し、ターゲットセクションに移動します。ターゲットを選択し、「 signing & capabilities 」タブに移動し、「 + capability 」ボタンをクリックして、「 keychain sharing 」機能を追加します 「 appdelegate 」で、application( didfinishlaunchingwithoptions )デリゲートメソッドに次の行を追加します(まず「 facebookcore 」フレームワークをインポートしてください) 1 import facebookcore 2 3 @main 4 class appdelegate uiresponder, uiapplicationdelegate { 5 func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]?) > bool { 6 7 8 applicationdelegate shared application(application, didfinishlaunchingwithoptions launchoptions) 9 return true 10 } 11 12 13 } 最後に、「 scenedelegate 」で、受信したurlコンテキストを処理するために次のコードを追加します 1 import facebookcore 2 3 class scenedelegate uiresponder, uiwindowscenedelegate { 4 5 6 func scene( scene uiscene, openurlcontexts urlcontexts set\<uiopenurlcontext>) { 7 guard let url = urlcontexts first? url else { 8 return 9 } 10 11 applicationdelegate shared application( 12 uiapplication shared, 13 open url, 14 sourceapplication nil, 15 annotation \[uiapplication openurloptionskey annotation] 16 ) 17 } 18 } 2 parseswiftでのfacebookログインの使用 facebookloginがxcodeプロジェクトに正常に統合されたので、facebookでのサインイン機能を実装します。「 プロジェクトの例 」では、logincontrollerがさまざまなサインインオプションの処理と表示を担当します。次に、signinwithfacebookbuttonアクションを設定します 1 // logincontroller swift file 2 import facebooklogin 3 4 5 class logincontroller uiviewcontroller { 6 7 8 private let signinwithfacebookbutton uibutton = { 9 let button = uibutton(type system) 10 button setimage(uiimage(named "facebookicon"), for normal) 11 button imageview? contentmode = scaleaspectfit 12 return button 13 }() 14 15 override func viewdidload() { 16 super viewdidload() 17 // 18 // layout configuration 19 // 20 21 signinwithfacebookbutton addtarget(self, action #selector(handlesigninwithfacebook), for touchupinside) 22 } 23 } 24 25 // mark sign in with facebook section 26 extension logincontroller { 27 @objc fileprivate func handlesigninwithfacebook() { 28 // todo here we will implement the sign in procedure 29 } 30 } facebookからサインインフォームを表示するために、facebooklogin sdkは、 signin(with\ presenting\ callback) メソッドを使用して設定し、表示することを可能にします。 loginmanager クラスから。このメソッドには、facebookから収集したいデータに関連する string 値を含む配列を渡す必要があります。一般的な値は public profile と email です。 一方、2番目のパラメータは、facebookがユーザーの資格情報( loginmanagerloginresult オブジェクトに埋め込まれている)を返すコールバッククロージャーであり、認証が失敗した場合はエラーを返します。 1 // mark sign in with facebook section 2 extension logincontroller { 3 @objc fileprivate func handlesigninwithfacebook() { 4 let loginmanager = loginmanager() 5 6 // method provided by the facebook sdk see https //developers facebook com/docs/facebook login/ios/ for more details 7 loginmanager login(permissions \["public profile", "email"], from self) { \[weak self] result, error in 8 if let error = error { 9 self? showmessage(title "error", message error localizeddescription) 10 return 11 } else if let result = result, result iscancelled { 12 self? showmessage(title "alert", message "sign in cancelled") 13 return 14 } 15 16 // once facebook successfully signs in the user, we retrieve the information related to the sign in process via the result token object, an accesstoken class type 17 guard let accesstoken = result? token else { fatalerror("this dhould never hapen ") } 18 19 // todo sign in the user on your back4app application with the accesstoken 20 } 21 } 22 } 次に、この資格情報を使用してユーザーが back4app プラットフォームにサインインします。ユーザーを表すオブジェクトは次の構造体です(詳細については ログインガイド https //www back4app com/docs/ios/parse swift sdk/users/user log in を参照してください): 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } したがって、facebookから返された資格情報には、サインインプロセスを完了するために使用されるaccesstokenとユーザーのidが含まれています。より正確には、parsefacebook\<user>オブジェクトをインスタンス化し、login(userid\ authenticationtoken\ completion )メソッドを呼び出します 1 // mark sign in with facebook section 2 extension logincontroller { 3 @objc fileprivate func handlesigninwithfacebook() { 4 let loginmanager = loginmanager() 5 6 // method provided by the facebook sdk see https //developers facebook com/docs/facebook login/ios/ for more details 7 loginmanager login(permissions \["public profile", "email"], from self) { \[weak self] result, error in 8 if let error = error { 9 self? showmessage(title "error", message error localizeddescription) 10 return 11 } else if let result = result, result iscancelled { 12 self? showmessage(title "alert", message "sign in cancelled") 13 return 14 } 15 16 // once facebook successfully signed in the user, we retrieve the information related to the sign in process via the result token object, an accesstoken class type 17 guard let accesstoken = result? token else { fatalerror("this dhould never hapen ") } 18 19 // with the accesstoken returned by facebook, you need to sign in the user on your back4app application 20 user facebook login(userid accesstoken userid, accesstoken accesstoken tokenstring) { \[weak self] result in 21 // returns the user object asociated to the facebook user returned by facebook 22 switch result { 23 case success(let user) 24 // after the login succeeded, we send the user to the home screen 25 // additionally, you can complete the user information with the data provided by facebook 26 let homecontroller = homecontroller() 27 homecontroller user = user 28 29 self? navigationcontroller? pushviewcontroller(homecontroller, animated true) 30 case failure(let error) 31 // handle the error if the login process failed 32 self? showmessage(title "failed to sign in", message error message) 33 } 34 } 35 } 36 } 37 } 3 ユーザーサインインの検証とセッションの作成 googleサインインが機能したことを確認するには、あなたの back4app アプリケーションダッシュボードを見て、facebookのauthdataパラメータを含む新しいユーザーを確認できます。 ダッシュボードで、対応するuserオブジェクトへのポインタを含む有効なセッションが作成されたことを確認することもできます。 4 既存のユーザーをfacebookアカウントにリンクする iosアプリが既存のユーザーにfacebookアカウントを関連付ける必要がある場合、 back4app プラットフォームでは、 parsefacebook\<user> オブジェクトが link(id\ accesstoken\ completion ) メソッドを実装しており、ここでfacebookアカウントの userid とセッションに関連付けられた accesstoken を渡します。 1 let facebookuserid string // the userid of the facebook account to link to 2 let accesstoken string = accesstoken current! tokenstring // the access token of the currently signed in facebook user 3 4 user facebook link(userid facebookuserid, accesstoken accesstoken) { result in 5 switch result { 6 case success(let user) 7 // linking succeeded, the user is now linked to the corresponding facebook account 8 case failure(let error) 9 // linking failed, handle the error 10 } 11 } 5 サインアウト サインアウトプロセスは、user signout()メソッドを呼び出す標準的な方法と異なりません(以前のガイドで詳述)。ただし、ユーザーがfacebookアカウントでサインインしている場合、一貫性のために、現在のfacebookユーザーもサインアウトする必要があります。これを実現するには、user signout()とともに次のメソッドを呼び出します。 現在のユーザーがリンクされたfacebookアカウントを持っているかどうかを確認するには、 user current? authdata 辞書を見て確認できます。 6 アプリを実行する この リポジトリ にアクセスして、サンプルプロジェクトをダウンロードできます。プロジェクトを実行する前に、開発者アカウントに関連付けられたプロビジョニングプロファイルを設定してください。 結論 このガイドの最後に、facebookでサインインまたは既存の back4app ユーザーをiosでリンクする方法を学びました。次のガイドでは、別のサインイン方法を続けます。