Platform
Appleでサインアップ
14 分
appleでサインインするチュートリアル はじめに appleでサインインを使用すると、ユーザーはapple idを使用してアプリにサインインできます。 この機能はios 13以降、parse 3 5以降で利用可能です。 前提条件 このチュートリアルを完了するには、次のものが必要です: back4appで作成されたアプリ 「 新しいアプリを作成するチュートリアル 」を参照して、back4appでアプリを作成する方法を学んでください。 back4appアプリのサブドメインを設定する 「 webホスティングとライブクエリの有効化 」を参照して、back4appでサブドメインを作成する方法を学んでください。 「 apple developerアカウント 」を持っていること。 1 新しいback4appアプリを作成する まず、back4appで既存のアプリが作成されていることを確認する必要があります。ただし、新しいユーザーの場合は、 このチュートリアル https //www back4app com/docs/get started/new parse app を確認して、アプリを作成する方法を学んでください。 2 xcodeプロジェクトにappleでサインイン機能を追加する xcodeプロジェクトで、ターゲット(1)をクリックし、サインインと機能タブ(2)に移動します。 + 機能 + 機能 ボタン(3)をクリックして、 appleでサインイン appleでサインイン 機能(4)を追加します。 その際、 バンドル識別子 バンドル識別子 (5)を選択し、その情報を保持してください。後で必要になります。 3 新しいサービスidを作成する あなたの apple developerアカウント https //developer apple com/ にログインし、 識別子 識別子 セクションに移動します。作成した バンドル識別子 バンドル識別子 がそこにあるか確認してください。 バンドル識別子をクリックし、下にスクロールします。 appleでサインイン appleでサインイン が選択されているか確認します。 クリック 編集 編集 をクリックして、 プライマリapp idとして有効にする プライマリapp idとして有効にする が選択されていることを確認してください。 すべてが正しければ、保存して終了します。 4 appleのためのparse authを設定する back4appのウェブサイトにアクセスし、ログインしてからアプリを見つけます。その後、 サーバー設定 サーバー設定 をクリックし、 appleログイン appleログイン ブロックを探して、 設定 設定 を選択します。 appleログイン appleログイン セクションはこのようになります 今、あなたは下のフィールドに バンドルid バンドルid を貼り付けて、保存するためのボタンをクリックするだけです。 apple loginの統合中に問題が発生した場合は、チャットで私たちのチームに連絡してください! 5 オプション1 テンプレートをダウンロード appleでのサインインを機能させるにはいくつかのコーディングが必要ですので、私たちは このテンプレート https //github com/templates back4app/parsesigninwithapple を作成しました。これをダウンロードして、 バンドル識別子 バンドル識別子 、 アプリid アプリid 、および クライアントキー クライアントキー を変更できます。 コードは完全に文書化されているので、良い出発点です。 このドキュメントを読みたい場合は、次のステップに進んでください。 6 オプション 2 手動でコードを書く ビュー内にauthenticationservicesフレームワークを追加し、pfuserauthenticationdelegateを処理するauthdelegateを作成します 1 import authenticationservices 2 3 class authdelegate\ nsobject, pfuserauthenticationdelegate { 4 func restoreauthentication(withauthdata authdata \[string string]?) > bool { 5 return true 6 } 7 8 func restoreauthenticationwithauthdata(authdata \[string string]?) > bool { 9 return true 10 } 11 } 7 viewcontrollerのためのデリゲートを実装する viewcontrollerのためにasauthorizationcontrollerdelegateとasauthorizationcontrollerpresentationcontextprovidingを実装します 1 class viewcontroller uiviewcontroller, asauthorizationcontrollerdelegate, asauthorizationcontrollerpresentationcontextproviding 8 appleでサインインボタンを追加する viewdidappearはそれにとって良い場所です。他の場所を選択する場合は、一度だけ呼び出すことを忘れないでください 1 override func viewdidappear( animated bool) { 2 super viewdidappear(animated) 3 4 // sign in with apple button 5 let signinwithapplebutton = asauthorizationappleidbutton() 6 7 // set this so the button will use auto layout constraint 8 signinwithapplebutton translatesautoresizingmaskintoconstraints = false 9 10 // add the button to the view controller root view 11 self view\ addsubview(signinwithapplebutton) 12 13 // set constraint 14 nslayoutconstraint activate(\[ 15 signinwithapplebutton leadinganchor constraint(equalto view\ safearealayoutguide leadinganchor, constant 50 0), 16 signinwithapplebutton trailinganchor constraint(equalto view\ safearealayoutguide trailinganchor, constant 50 0), 17 signinwithapplebutton bottomanchor constraint(equalto view\ safearealayoutguide bottomanchor, constant 70 0), 18 signinwithapplebutton heightanchor constraint(equaltoconstant 50 0) 19 ]) 20 21 // the function that will be executed when user tap the button 22 signinwithapplebutton addtarget(self, action #selector(applesignintapped), for touchupinside) 23 } 最後の行のapplesignintappedもviewcontrollerクラス内で定義する必要があります 1 // this is the function that will be executed when user taps the button 2 @objc func applesignintapped() { 3 let provider = asauthorizationappleidprovider() 4 let request = provider createrequest() 5 // request full name and email from the user's apple id 6 request requestedscopes = \[ fullname, email] 7 8 // pass the request to the initializer of the controller 9 let authcontroller = asauthorizationcontroller(authorizationrequests \[request]) 10 11 // similar to delegate, this will ask the view controller 12 // which window to present the asauthorizationcontroller 13 authcontroller presentationcontextprovider = self 14 15 // delegate functions will be called when user data is 16 // successfully retrieved or error occured 17 authcontroller delegate = self 18 19 // show the sign in with apple dialog 20 authcontroller performrequests() 21 } 9 presentationcontextprovider presentationcontextprovider (asauthorizationcontrollerpresentationcontextproviding) は、どのウィンドウで認証ダイアログを表示するかを尋ねます。同じウィンドウに表示するため、self view\ windowを返す必要があります 1 func presentationanchor(for controller asauthorizationcontroller) > aspresentationanchor { 2 // return the current view window 3 return self view\ window! 4 } 10 デリゲート asauthorizationcontrollerdelegate の処理 デリゲートが呼び出されたときに処理しなければならないいくつかのオプションがあるので、それらのオプションを明確に処理するためのコードを追加しましょう 1 func authorizationcontroller(controller asauthorizationcontroller, didcompletewitherror error error) { 2 print("authorization error") 3 guard let error = error as? asauthorizationerror else { 4 return 5 } 6 7 switch error code { 8 case canceled 9 // user press "cancel" during the login prompt 10 print("canceled") 11 case unknown 12 // user didn't login their apple id on the device 13 print("unknown") 14 case invalidresponse 15 // invalid response received from the login 16 print("invalid respone") 17 case nothandled 18 // authorization request not handled, maybe internet failure during login 19 print("not handled") 20 case failed 21 // authorization failed 22 print("failed") 23 @unknown default 24 print("default") 25 } 26 } 11 didcompletewithauthorization のためのデリゲートの処理 認証に成功した場合、承認された情報を取得できます 1 func authorizationcontroller(controller asauthorizationcontroller, didcompletewithauthorization authorization asauthorization) { 2 3 if let appleidcredential = authorization credential as? asauthorizationappleidcredential { 4 // unique id for each user, this uniqueid will always be returned 5 let userid = appleidcredential user 6 print("userid " + userid) 7 8 // if needed, save it to user defaults by uncommenting the line below 9 //userdefaults standard set(appleidcredential user, forkey "userid") 10 11 // optional, might be nil 12 let email = appleidcredential email 13 print("email " + (email ?? "no email") ) 14 15 // optional, might be nil 16 let givenname = appleidcredential fullname? givenname 17 print("given name " + (givenname ?? "no given name") ) 18 19 // optional, might be nil 20 let familyname = appleidcredential fullname? familyname 21 print("family name " + (familyname ?? "no family name") ) 22 23 // optional, might be nil 24 let nickname = appleidcredential fullname? nickname 25 print("nick name " + (nickname ?? "no nick name") ) 26 / 27 useful for server side, the app can send identitytoken and authorizationcode 28 to the server for verification purpose 29 / 30 var identitytoken string? 31 if let token = appleidcredential identitytoken { 32 identitytoken = string(bytes token, encoding utf8) 33 print("identity token " + (identitytoken ?? "no identity token")) 34 } 35 36 var authorizationcode string? 37 if let code = appleidcredential authorizationcode { 38 authorizationcode = string(bytes code, encoding utf8) 39 print("authorization code " + (authorizationcode ?? "no auth code") ) 40 } 41 42 // do what you want with the data here 43 44 } 45 } ここはparseにログインするためのコードを追加できる場所です。したがって、 ここでデータを使ってやりたいことをします ここでデータを使ってやりたいことをします というコメントのすぐ後に、追加しましょう 1 pfuser loginwithauthtype(inbackground "apple", authdata \["token" string(identitytoken!), "id" userid]) continuewith { task > any? in 2 if ((task error) != nil){ 3 //dispatchqueue main async { 4 print("could not login \nplease try again ") 5 print("error with parse login after siwa \\(task error! localizeddescription)") 6 //} 7 return task 8 } 9 print("successfuly signed in with apple") 10 return nil 11 } もちろん、parseフレームワークを追加します 1 import parse