iOS
...
Users
ParseSwift SDKでのiOSユーザーログインとログアウト実装ガイド
9 分
ユーザーログインとログアウト はじめに 「 ユーザー登録ガイド https //www back4app com/docs/ios/parse swift sdk/users/user registration 」では、 back4app プラットフォームと parseswift sdk parseswift sdk を使用して、iosアプリにサインアップオプションを統合する方法を学びました。ユーザーがアプリに正常にサインアップすると、ログインとログアウトのアクションはアプリのフローにおける重要な機能です。 「 parseswift sdk parseswift sdk 」を使用すると、これらの機能をあらゆるiosアプリにシームレスに統合できます。 前提条件 このクイックスタートを完了するには、次のものが必要です: xcode。 back4appで作成されたアプリ。 「 新しいparseアプリのチュートリアル 」に従って、 back4app でparseアプリを作成する方法を学びます。 注意: 「 parse sdk(swift)インストールチュートリアル 」に従って、 back4app に接続されたxcodeプロジェクトを作成します。 目標 「 parseswift sdk 」と back4app プラットフォームを使用して、ユーザーログインとログアウト機能を実装します。 1 ログインとログアウト機能の設定 ログイン機能を実装する前に、ユーザーを表すオブジェクトを作成する必要があります。簡単のために、同じ user user 構造体(これは parseuser parseuser プロトコルに準拠しています)を再利用します。これは ユーザー登録ガイド https //www back4app com/docs/ios/parse swift sdk/users/user registration import foundation import parseswift struct user parseuser { var username string? var email string? var emailverified bool? var password string? var age int? } ユーザー登録ガイドに従い、少なくとも1人のユーザーを登録して、このガイドの例として使用することをお勧めします。 サインアッププロセスと同様に、ログインにはユーザーが自分の username と password を入力するフォームが必要です。その後、対応するメソッドを使用してログインリクエストを実行します。これは parseswift sdk parseswift sdk によって提供されます。次に、 back4app がリクエストを処理し、ログイン情報を含むレスポンスを返します。エラーが発生した場合、レスポンスはこのエラーを特定し処理するための情報を返します。 ログアウトプロセスは簡単です。 parseswift sdk parseswift sdk は、これを1行のコードで実装できるようにします。 2 アプリの設定 あなたが 接続した https //www back4app com/docs/ios/parse swift sdk/install sdk xcodeプロジェクトを back4app アプリケーションに接続したら、次のステップはアプリのユーザーインターフェースを設定することです。 ログインプロセスのために、対応する入力フィールドとログインボタンを含むシンプルなコントローラーを実装します このフォームを担当するクラスは logincontroller と呼ばれ、uiviewcontroller のサブクラスです。このコントローラーに統合する主要なコンポーネントは、2 つの uitextfield と 1 つの uibutton です。以下のスニペットは logincontroller クラスの実装を示しています 1 import uikit 2 import parseswift 3 4 class logincontroller uiviewcontroller { 5 private let usernametextfield uitextfield = { 6 let textfield = uitextfield() 7 textfield borderstyle = roundedrect 8 textfield placeholder = "username " 9 textfield autocapitalizationtype = none 10 textfield textalignment = center 11 return textfield 12 }() 13 14 private let passwordtextfield uitextfield = { 15 let textfield = uitextfield() 16 textfield borderstyle = roundedrect 17 textfield issecuretextentry = true 18 textfield placeholder = "password " 19 textfield textalignment = center 20 return textfield 21 }() 22 23 private let loginbutton uibutton = { 24 let button = uibutton(type roundedrect) 25 button settitle("log in", for normal) 26 return button 27 }() 28 29 override func viewdidload() { 30 super viewdidload() 31 32 navigationitem title = "back4app log in" 33 34 // lays out the login form 35 let stackview = uistackview(arrangedsubviews \[usernametextfield, passwordtextfield, loginbutton]) 36 stackview\ translatesautoresizingmaskintoconstraints = false 37 stackview\ spacing = 8 38 stackview\ axis = vertical 39 stackview\ distribution = fillequally 40 41 let stackviewheight = cgfloat(stackview\ arrangedsubviews count) (44 + stackview\ spacing) stackview\ spacing 42 43 view\ addsubview(stackview) 44 stackview\ centerxanchor constraint(equalto view\ safearealayoutguide centerxanchor) isactive = true 45 stackview\ centeryanchor constraint(equalto view\ safearealayoutguide centeryanchor) isactive = true 46 stackview\ widthanchor constraint(equalto view\ safearealayoutguide widthanchor, multiplier 0 7) isactive = true 47 stackview\ heightanchor constraint(equaltoconstant stackviewheight) isactive = true 48 49 // adds the method that will be called when the user taps the login button 50 loginbutton addtarget(self, action #selector(handlelogin), for touchupinside) 51 52 // if the user is already logged in, we redirect them to the homecontroller 53 guard let user = user current else { return } 54 let homecontroller = homecontroller() 55 homecontroller user = user 56 57 navigationcontroller? pushviewcontroller(homecontroller, animated true) 58 } 59 60 /// called when the user taps on the loginbutton button 61 @objc private func handlelogin() { 62 guard let username = usernametextfield text, !username isempty, 63 let password = passwordtextfield text, !password isempty else { 64 // shows an alert with the appropriate title and message 65 return showmessage(title "error", message "invalid credentials ") 66 } 67 68 login(with username, password password) 69 } 70 71 /// logs in the user and presents the app's home screen (homecontroller) 72 /// parameters 73 /// username user's username 74 /// password user's password 75 private func login(with username string, password string) { 76 // todo here we will implement the login process 77 } 78 } さらに、ヘルパー関数 showmessage(title\ message ) showmessage(title\ message ) は uiviewcontroller uiviewcontroller の拡張で実装されています。 1 extension uiviewcontroller { 2 3 /// presents an alert with a title, a message and a back button 4 /// parameters 5 /// title title for the alert 6 /// message shor message for the alert 7 func showmessage(title string, message string) { 8 let alertcontroller = uialertcontroller(title title, message message, preferredstyle alert) 9 10 alertcontroller addaction(uialertaction(title "back", style cancel)) 11 12 present(alertcontroller, animated true) 13 } 14 } ログアウトプロセスのために、ホームコントローラーにボタンを挿入します。すなわち、 homecontroller homecontroller このビューコントローラーには、ログアウトボタンとユーザー名を表示するラベルのみが含まれます このビューコントローラーの実装は簡単です 1 import uikit 2 import parseswift 3 4 class homecontroller uiviewcontroller { 5 6 /// when set, it updates the usernamelabel's text with the user's username 7 var user user? { 8 didset { 9 usernamelabel text = "hello \\(user? username ?? "n/a")!" 10 } 11 } 12 13 private let usernamelabel uilabel = { 14 let label = uilabel() 15 label textalignment = center 16 label font = boldsystemfont(ofsize 18) 17 label translatesautoresizingmaskintoconstraints = false 18 return label 19 }() 20 21 private let logoutbutton uibutton = { 22 let button = uibutton(type roundedrect) 23 button settitle("log out", for normal) 24 button translatesautoresizingmaskintoconstraints = false 25 return button 26 }() 27 28 override func viewdidload() { 29 super viewdidload() 30 31 // sets up the layout (usernamelabel and logoutbutton) 32 view\ backgroundcolor = systembackground 33 navigationitem hidesbackbutton = true 34 navigationitem title = "back4app" 35 view\ addsubview(usernamelabel) 36 view\ addsubview(logoutbutton) 37 38 usernamelabel topanchor constraint(equalto view\ safearealayoutguide topanchor, constant 8) isactive = true 39 usernamelabel centerxanchor constraint(equalto view\ safearealayoutguide centerxanchor) isactive = true 40 41 logoutbutton bottomanchor constraint(equalto view\ safearealayoutguide bottomanchor, constant 8) isactive = true 42 logoutbutton centerxanchor constraint(equalto view\ safearealayoutguide centerxanchor) isactive = true 43 44 // adds the method that will be called when the user taps the logout button 45 logoutbutton addtarget(self, action #selector(handlelogout), for touchupinside) 46 } 47 48 /// called when the user taps the logout button 49 @objc private func handlelogout() { 50 // todo here we will implement the logout process 51 } 52 } 3 ログインリクエスト 私たちは今、 login(with\ password) メソッドを logincontroller クラスに実装します。 parseuser プロトコルは、 user オブジェクトに静的メソッド login(username\ password) を提供します。このメソッドは、あなたの back4app アプリケーションにログインリクエストを準備して送信します。使用ケースに応じて、 login( ) メソッドの多くの実装のいずれかを使用できます。私たちは今、 login(with\ password) メソッドを logincontroller で完成させます。 1 class homecontroller uiviewcontroller { 2 3 4 /// logs in the user and presents the app's home screen (homecontroller) 5 /// parameters 6 /// username user's username 7 /// password user's password 8 private func login(with username string, password string) { 9 // warning use only one of the following implementations, the synchronous or asynchronous option 10 11 // logs in the user synchronously, it throws a parseerror error if something happened 12 // this should be executed in a background thread! 13 do { 14 let loggedinuser = try user login(username username, password password) 15 16 // after the login success we send the user to the home screen 17 let homecontroller = homecontroller() 18 homecontroller user = loggedinuser 19 20 navigationcontroller? pushviewcontroller(homecontroller, animated true) 21 } catch let error as parseerror { 22 showmessage(title "error", message "failed to log in \\(error message)") 23 } catch { 24 showmessage(title "error", message "failed to log in \\(error localizeddescription)") 25 } 26 27 // logs in the user asynchronously 28 user login(username username, password password) { \[weak self] result in // handle the result (of type result\<user, parseerror>) 29 switch result { 30 case success(let loggedinuser) 31 self? usernametextfield text = nil 32 self? passwordtextfield text = nil 33 34 // after the login success we send the user to the home screen 35 let homecontroller = homecontroller() 36 homecontroller user = loggedinuser 37 38 self? navigationcontroller? pushviewcontroller(homecontroller, animated true) 39 case failure(let error) 40 self? showmessage(title "error", message "failed to log in \\(error message)") 41 } 42 } 43 } 44 } 4 ログアウトリクエスト ログアウトリクエストはログインリクエストと同じくらい簡単です。再度、 parseuser プロトコルは user に静的メソッド logout( ) を提供します。このメソッドを呼び出すことで、現在のユーザー( user current を介してアクセス)をログアウトさせます。ユーザーがホーム画面にあるログアウトボタンをタップしたときにこのメソッドを呼び出します。つまり、 handlelogout() メソッド内で、 homecontroller クラスに次のように追加します 1 class homecontroller uiviewcontroller { 2 3 4 /// called when the user taps the logout button 5 @objc private func handlelogout() { 6 // warning use only one of the following implementations, the synchronous or asynchronous option 7 8 // logs out the user synchronously, it throws a parseerror error if something happened 9 // this should be executed in a background thread! 10 do { 11 try user logout() 12 13 // after the logout succeeded we dismiss the home screen 14 navigationcontroller? popviewcontroller(animated true) 15 } catch let error as parseerror { 16 showmessage(title "error", message "failed to log out \\(error message)") 17 } catch { 18 showmessage(title "error", message "failed to log out \\(error localizeddescription)") 19 } 20 21 // logs out the user asynchronously 22 user logout { \[weak self] result in // handle the result (of type result\<void, parseerror>) 23 switch result { 24 case success 25 // after the logout succeeded we dismiss the home screen 26 self? navigationcontroller? popviewcontroller(animated true) 27 case failure(let error) 28 self? showmessage(title "error", message "failed to log out \\(error message)") 29 } 30 } 31 } 32 } 5 アプリを実行する! この リポジトリ https //github com/templates back4app/ios user log in and log out , ここには、上記で説明したログインおよびログアウトプロセスを含むxcodeプロジェクトがあります。アプリを実行する前に、xcodeプロジェクトをあなたの back4app back4app アプリケーションに接続していることを確認してください。 結論 この back4app と parseswift sdk parseswift sdk は、iosアプリにログインおよびログアウト機能を迅速に統合することを可能にします。あなたの back4app アプリケーションをxcodeプロジェクトに接続した後、ログイン(またはログアウト)プロセスは単一のメソッドを呼び出すだけで済みます。