iOS
...
Users
ลงชื่อเข้าใช้งานและออกโดยใช้ ParseSwift SDK สำหรับ iOS
9 นาที
การเข้าสู่ระบบและออกจากระบบของผู้ใช้ บทนำ ใน https //www back4app com/docs/ios/parse swift sdk/users/user registration เราได้เรียนรู้วิธีการรวมตัวเลือกการลงทะเบียนเข้าไปในแอป ios โดยใช้ back4app แพลตฟอร์มและ parseswift sdk parseswift sdk เมื่อผู้ใช้ลงทะเบียนสำเร็จในแอปของคุณ การเข้าสู่ระบบและออกจากระบบเป็นฟีเจอร์ที่สำคัญภายในกระบวนการของแอป parseswift sdk parseswift sdk จะช่วยให้เราสามารถรวมฟีเจอร์เหล่านี้เข้ากับแอป ios ได้อย่างราบรื่น ข้อกำหนดเบื้องต้น ในการทำให้การเริ่มต้นนี้เสร็จสมบูรณ์ คุณต้องการ xcode แอปที่สร้างขึ้นที่ back4app ติดตาม https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอป parse ที่ back4app หมายเหตุ ติดตาม https //www back4app com/docs/ios/parse swift sdk เพื่อสร้างโปรเจกต์ xcode ที่เชื่อมต่อกับ back4app เป้าหมาย เพื่อดำเนินการฟีเจอร์การเข้าสู่ระบบและออกจากระบบของผู้ใช้โดยใช้ parseswift sdk และ back4app แพลตฟอร์ม 1 การตั้งค่าฟีเจอร์การเข้าสู่ระบบและออกจากระบบ ก่อนเริ่มต้นการใช้งานฟังก์ชันการเข้าสู่ระบบใด ๆ เราต้องสร้างอ็อบเจ็กต์ที่จะเป็นตัวแทนของผู้ใช้ สำหรับความเรียบง่าย เราจะใช้โครงสร้างเดียวกัน ผู้ใช้ ผู้ใช้ (ซึ่งสอดคล้องกับ 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? } เราขอแนะนำให้ติดตามคู่มือการลงทะเบียนผู้ใช้และลงทะเบียนผู้ใช้อย่างน้อยหนึ่งคนเพื่อใช้เป็นตัวอย่างสำหรับคู่มือนี้ คล้ายกับกระบวนการลงทะเบียน การเข้าสู่ระบบต้องการแบบฟอร์มที่ผู้ใช้กรอก ชื่อผู้ใช้ และ รหัสผ่าน จากนั้นเราจะทำการร้องขอการเข้าสู่ระบบโดยใช้วิธีการที่เกี่ยวข้องที่ให้โดย parseswift sdk parseswift sdk ในทางกลับกัน back4app จะประมวลผลคำขอและส่งคืนการตอบสนองที่มีข้อมูลการเข้าสู่ระบบ เมื่อเกิดข้อผิดพลาด การตอบสนองจะส่งคืนข้อมูลเพื่อระบุและจัดการกับข้อผิดพลาดนี้ กระบวนการออกจากระบบนั้นตรงไปตรงมา parseswift sdk parseswift sdk ช่วยให้เราสามารถดำเนินการในบรรทัดเดียวของโค้ด 2 การตั้งค่าแอป เมื่อคุณ https //www back4app com/docs/ios/parse swift sdk/install sdk โครงการ xcode ของคุณกับ แอปพลิเคชัน back4app , ขั้นตอนถัดไปคือการตั้งค่าหน้าต่างผู้ใช้ของแอป สำหรับกระบวนการเข้าสู่ระบบ เราจะสร้างตัวควบคุมที่เรียบง่ายซึ่งมีฟิลด์ข้อมูลที่เกี่ยวข้องและปุ่มเข้าสู่ระบบ คลาสที่รับผิดชอบแบบฟอร์มนี้เรียกว่า logincontroller และเป็นซับคลาสของ uiviewcontroller ส่วนประกอบหลักที่ต้องรวมเข้ากับตัวควบคุมนี้คือ uitextfield สองตัวและ 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 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 คำขอเข้าสู่ระบบ เราจะดำเนินการเพื่อ implement login(with\ password) เมธอดใน logincontroller คลาส เมธอด parseuser โปรโตคอลให้ user อ็อบเจ็กต์มีเมธอดสแตติก login(username\ password) เมธอดนี้เตรียมและส่งคำขอเข้าสู่ระบบไปยัง back4app แอปพลิเคชันของคุณ ขึ้นอยู่กับกรณีการใช้งาน สามารถใช้หนึ่งในหลาย ๆ การ implement ของ 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 ) จะออกจากระบบจาก back4app แอปพลิเคชันของคุณ เราจะเรียกใช้วิธีนี้เมื่อผู้ใช้แตะปุ่มออกจากระบบที่ตั้งอยู่บนหน้าจอหลัก นั่นคือใน 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 ขั้นตอนการเข้าสู่ระบบ (หรือออกจากระบบ) จะต้องเรียกใช้เพียงแค่เมธอดเดียว