iOS
...
Users
iOSパースSwiftSDKでAppleでサインイン方法を実装する
11 分
appleでサインイン はじめに iosアプリにサードパーティのサインイン方法を統合する際には、appleでのサインインオプションを追加の選択肢として追加することが必須です。この目的のために、appleは authenticationservices フレームワークを導入しました。このフレームワークを使用すると、開発者は任意のxcodeプロジェクトに appleでサインイン ボタンをシームレスに統合できます。 この リポジトリ https //github com/templates back4app/ios sign in with apple では、実装しているさまざまなサインイン方法をテストできるシンプルなxcodeテンプレートを提供しています。この例はすでに ログインガイド https //www back4app com/docs/ios/parse swift sdk/users/user log in で紹介されています。プロジェクトの詳細については、再度確認できます。 前提条件 このクイックスタートを完了するには、次のものが必要です: 最新のxcodeのバージョン。 非個人開発チームを持つapple開発者アカウント。 back4appで作成されたアプリ。 次の 新しいparseアプリのチュートリアル をフォローして、 back4app でparseアプリを作成する方法を学びます。 注意: 次の parse sdk (swift)のインストールチュートリアル をフォローして、 back4app に接続されたxcodeプロジェクトを作成します。 目標 authenticationservices フレームワークと parseswift sdk を使用して、ユーザーサインイン機能を統合します。 1 appleでのサインインの設定 xcodeプロジェクトが リンクされている back4app アプリケーションに接続されたら、appleでのサインイン機能を追加します。これを行うには、プロジェクトナビゲーターからプロジェクトを選択し、ターゲットセクションに移動します。メインターゲットを選択し、 署名と機能 タブに移動し、 + 機能 ボタンをクリックして、 appleでのサインイン 機能を追加します。 これがiosアプリで「 sign in with apple 」メソッドを統合するために必要な唯一の設定です。 2 parseswiftとauthenticationservicesフレームワークを使用する appleでのサインインフローは3つのステージで完了できます。しかし、その前に、「 sign in with apple 」フローを表示するために使用するボタンを追加して設定しましょう。 logincontroller logincontroller クラスにこのボタンを追加します 1 // logincontroller swift file 2 import authenticationservices 3 4 5 class logincontroller uiviewcontroller { 6 7 8 private let signinwithapplebutton uibutton = { 9 let button = uibutton(type system) 10 button setimage(uiimage(named "appleicon"), 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 signinwithapplebutton addtarget(self, action #selector(handlesigninwithapple), for touchupinside) 22 } 23 } 24 25 // mark sign in with apple section 26 extension logincontroller asauthorizationcontrollerdelegate, asauthorizationcontrollerpresentationcontextproviding { 27 @objc fileprivate func handlesigninwithapple() { 28 // todo here we will implement the sign in procedure 29 } 30 31 // asauthorizationcontrollerdelegate 32 33 func authorizationcontroller(controller asauthorizationcontroller, didcompletewithauthorization authorization asauthorization) { 34 // todo handle the sign in result 35 } 36 37 func authorizationcontroller(controller asauthorizationcontroller, didcompletewitherror error error) { 38 showmessage(title "error", message error localizeddescription) 39 } 40 41 // asauthorizationcontrollerpresentationcontextproviding 42 43 func presentationanchor(for controller asauthorizationcontroller) > aspresentationanchor { 44 guard let window = view\ window else { fatalerror("no uiwindow found!") } 45 return window 46 } 47 } 次のことに注意してください。 logincontroller logincontroller は2つの新しいプロトコルに準拠しています asauthorizationcontrollerdelegate asauthorizationcontrollerdelegate と asauthorizationcontrollerpresentationcontextproviding asauthorizationcontrollerpresentationcontextproviding 。最初のプロトコルは、サインインの結果を logincontroller logincontroller クラスに委譲することを可能にします。2つ目のプロトコルは、サインインシートが表示される uiwindow uiwindow を決定するためのものです。 私たちは今、 handlesigninwithapple() handlesigninwithapple() メソッドでフローを実装します。 最初の段階では、リクエストとフォームを準備します。リクエストは asauthorizationappleidrequest asauthorizationappleidrequest クラスによって構築されます。このクラスのインスタンスを asauthorizationappleidprovider asauthorizationappleidprovider プロバイダーから取得し、フォームは asauthorizationcontrolle asauthorizationcontrolle クラスによって取得します。リクエストのインスタンスを取得したら、興味のあるスコープを提供する必要があります。これまでのところ、appleはユーザーのフルネームとメールアドレスへのアクセスのみを提供しています。したがって、リクエストを作成する標準的な方法は次のとおりです 1 @objc fileprivate func handlesigninwithapple() { 2 let provider = asauthorizationappleidprovider() 3 let request asauthorizationappleidrequest = provider createrequest() 4 request requestedscopes = \[ fullname, email] 5 6 } このリクエストにより、 asauthorizationcontroller asauthorizationcontroller コントローラーを構築します。このコントローラーは、ユーザーが認証し、サインインプロセスを完了するための対応する権限を与えるシートを表示する役割を担っています 1 @objc fileprivate func handlesigninwithapple() { 2 // as requested by apple, we set up the necessary objects to implement the sign in with apple flow 3 // see https //help apple com/developer account/#/devde676e696 for more details 4 let provider = asauthorizationappleidprovider() 5 let request asauthorizationappleidrequest = provider createrequest() 6 request requestedscopes = \[ fullname, email] 7 8 let authorizationcontroller = asauthorizationcontroller(authorizationrequests \[request]) 9 authorizationcontroller delegate = self // here self is a reference to logincontroller 10 authorizationcontroller presentationcontextprovider = self // here self is a reference to logincontroller 11 12 // presents the sign in with apple sheet and the result will be handled by the asauthorizationcontrollerdelegate delegate 13 authorizationcontroller performrequests() 14 } 最後のステージでは、サインインの結果を処理します。これはデリゲートメソッドを介して返されます authorizationcontroller(controller\ didcompletewithauthorization ) authorizationcontroller(controller\ didcompletewithauthorization ) 。このメソッドの最後の引数は、 asauthorization asauthorization クラスで、apple idと資格情報に関するすべての必要な情報が含まれています。このステージでは、 user user オブジェクトを関連付け、 back4app back4app アプリケーションにログインを行います。この user user オブジェクトは次の構造を持っています(詳細については ログインガイド を参照してください) 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 } 今、私たちは user user オブジェクトを asauthorization asauthorization 結果に含まれるデータから作成します。これは parseapple parseapple オブジェクトをインスタンス化することによって達成します( user apple user apple )そして login(user\ identitytoken ) login(user\ identitytoken ) メソッドを呼び出します 1 // mark sign in with apple section 2 extension logincontroller asauthorizationcontrollerdelegate, asauthorizationcontrollerpresentationcontextproviding { 3 4 // asauthorizationcontrollerdelegate 5 6 func authorizationcontroller(controller asauthorizationcontroller, didcompletewithauthorization authorization asauthorization) { 7 // we cast the (asauthorization) authorization object to an asauthorizationappleidcredential object 8 guard let credential = authorization credential as? asauthorizationappleidcredential else { 9 return showmessage(title "sign in with apple", message "invalid credential") 10 } 11 12 guard let identitytoken = credential identitytoken else { 13 return showmessage(title "sign in with apple", message "token not found") 14 } 15 16 // we log in the user with the token generated by apple 17 user apple login(user credential user, identitytoken identitytoken) { \[weak self] result in 18 switch result { 19 case success(let user) 20 // after the login succeeded, we send the user to the home screen 21 // additionally, you can complete the user information with the data provided by apple 22 let homecontroller = homecontroller() 23 homecontroller user = user 24 25 self? navigationcontroller? pushviewcontroller(homecontroller, animated true) 26 case failure(let error) 27 self? showmessage(title "error", message error message) 28 } 29 } 30 } 31 } 3 ユーザーのサインインとセッションの作成の確認 googleでのサインインが成功したことを確認するために、あなたの back4app アプリケーションダッシュボードを見て、新しい ユーザー ユーザー がfacebookの authdata authdata パラメータを含んでいるのを確認できます。 ダッシュボードで有効なセッションが作成されたことを確認できます。そのセッションには、対応する ユーザー ユーザー オブジェクトへのポインタが含まれています。 4 既存のユーザーをapple idにリンクする iosアプリが back4app プラットフォームの既存ユーザーにapple idを関連付ける必要がある場合、 parseapple\<user> parseapple\<user> オブジェクトは、次のメソッドを実装しています。 link(user\ identitytoken\ completion ) link(user\ identitytoken\ completion ) ここで、 user user 値と、 identitytoken identitytoken を、 asauthorizationappleidcredential asauthorizationappleidcredential 認証情報から渡します。 1 let credential asauthorizationappleidcredential 2 3 guard let identitytoken = credentials identitytoken else { 4 return showmessage(title "sign in with apple", message "token not found") 5 } 6 7 user apple link(user credential user, identitytoken identitytoken){ result in 8 switch result { 9 case success(let user) 10 // linking succeeded, user object now is linked to the corresponding apple id 11 case failure(let error) 12 // linking failed, handle the error 13 } 14 } 5 アプリを実行する この リポジトリ にアクセスして、サンプルプロジェクトをダウンロードできます。プロジェクトを実行する前に、開発者アカウントに関連付けられたプロビジョニングプロファイルを設定してください。 結論 このガイドの最後に、appleでサインインを使用して、既存の back4app ユーザーにサインインまたはリンクする方法を学びました。