การสร้างฟังก์ชันลงชื่อเข้าใช้ใน Swift สำหรับนักพัฒนา iOS
12 นาที
บทเรียนการเข้าสู่ระบบและการลงทะเบียนผู้ใช้โดยใช้ swift บทนำ ส่วนนี้อธิบายวิธีการสร้างแอปด้วยการลงทะเบียนผู้ใช้ที่ง่ายโดยใช้ ฟีเจอร์หลักของ parse server https //www back4app com/product/parse server ผ่าน back4app ในทุกช่วงเวลา คุณสามารถเข้าถึงโปรเจกต์ที่สร้างขึ้นทั้งหมดด้วยบทเรียนนี้ได้ที่ ที่เก็บ github https //github com/templates back4app/ios install sdk ในการทำให้การเริ่มต้นนี้เสร็จสมบูรณ์ คุณต้องการ xcode https //developer apple com/xcode/ แอปที่สร้างขึ้นที่ back4app ติดตาม บทเรียนการสร้างแอป parse ใหม่ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอป parse ที่ back4app แอป ios ที่เชื่อมต่อกับ back4app หมายเหตุ ติดตาม บทเรียนการติดตั้ง parse sdk (swift) https //www back4app com/docs/ios/parse swift sdk เพื่อสร้างโปรเจกต์ xcode ที่เชื่อมต่อกับ back4app บัญชีนักพัฒนาของ apple ที่ต้องชำระเงิน เริ่มกันเถอะ! ตามขั้นตอนถัดไป คุณจะสามารถสร้างแอปที่สามารถลงชื่อเข้าใช้ ลงทะเบียน และออกจากระบบที่ฐานข้อมูล back4app ได้ 1 ตั้งค่าและสร้าง ui สำหรับการลงทะเบียนและการเข้าสู่ระบบของคุณ ไปที่ xcode เข้าถึงโฟลเดอร์หลักจากโปรเจกต์แล้วเปิดไฟล์ viewcontroller swift เพื่อแก้ไข ใน viewcontroller swift ให้แน่ใจว่าคุณได้รวมโมดูล parse โดยการรวมมันที่ด้านบนของไฟล์ 1 import parse 3\ ไปที่ main storyboard ลาก uitextfields สี่ตัวไปยัง viewcontroller ใน storyboard หลัก จัดตำแหน่ง textfield ให้อยู่กลางและวางสองตัวที่ด้านบนและสองตัวที่ด้านล่างของ view controller ลาก uibuttons สองตัวไปยัง view และวางไว้ด้านล่างของ textfields ลาก loader indicators อีกหนึ่งตัวไปยังแต่ละปุ่ม ตั้งค่าข้อความของปุ่มด้านบนให้เขียนว่า ลงชื่อเข้าใช้ ตั้งค่าข้อความของปุ่มด้านล่างให้เขียนว่า ลงทะเบียน ตั้งค่าฟิลด์ข้อความให้เขียนว่า ชื่อผู้ใช้ และ รหัสผ่าน 4\ ต่อไปเราจะเชื่อมต่อ uitextfields ของคุณใน storyboard กับ properties ใน view controller ของคุณ เพิ่ม properties ต่อไปนี้ที่ด้านบนของ viewcontroller swift จากนั้นไปที่ storyboard ของคุณและคลิกขวาที่ uitextfield แต่ละตัวและคลิกที่ reference outlet จากนั้นลากเส้นกลับไปที่ไอคอน viewcontroller และตั้งค่าให้ตรงกับฟิลด์ที่เหมาะสม signinusernamefield เชื่อมต่อกับฟิลด์ชื่อผู้ใช้ในการลงชื่อเข้าใช้ เป็นต้น… มันควรจะดูเหมือนแบบนี้ 1 import uikit 2 import parse 3 4 class viewcontroller uiviewcontroller { 5 6 @iboutlet weak var txtusernamesignin uitextfield! 7 @iboutlet weak var txtpasswordsignin uitextfield! 8 @iboutlet weak var indicatorlogin uiactivityindicatorview! 9 10 @iboutlet weak var txtusernamesignup uitextfield! 11 @iboutlet weak var txtpasswordsignup uitextfield! 12 @iboutlet weak var indicatorsignup uiactivityindicatorview! 13 14 override func viewdidload() { 15 super viewdidload() 16 // do any additional setup after loading the view 17 } 18 19 @ibaction func signin( sender any) { 20 //todo 21 } 22 23 @ibaction func signup( sender any) { 24 //todo 25 } 26 27 } 2 สร้างฟังก์ชันการลงชื่อเข้าใช้ เพิ่มโค้ดต่อไปนี้ภายในฟังก์ชันการลงชื่อเข้าใช้ 1 @ibaction func signin( sender any) { 2 pfuser loginwithusername(inbackground self txtusernamesignin text!, password self txtpasswordsignin text!) { 3 (user pfuser?, error error?) > void in 4 if user != nil { 5 self displayalert(withtitle "login successful", message "") 6 } else { 7 self displayalert(withtitle "error", message error! localizeddescription) 8 } 9 } 10 } 3 สร้างฟังก์ชันการลงทะเบียน เพิ่มโค้ดต่อไปนี้ภายในฟังก์ชันการลงทะเบียน 1 @ibaction func signup( sender any) { 2 let user = pfuser() 3 user username = self txtusernamesignup text 4 user password = self txtpasswordsignup text 5 6 self indicatorsignup startanimating() 7 user signupinbackground {(succeeded bool, error error?) > void in 8 self indicatorsignup stopanimating() 9 if let error = error { 10 self displayalert(withtitle "error", message error localizeddescription) 11 } else { 12 self displayalert(withtitle "success", message "account has been successfully created") 13 } 14 } 15 } 4 ออกจากระบบ เมื่อเข้าสู่ระบบ จะสร้างวัตถุ session ซึ่งชี้ไปที่ผู้ใช้ที่เข้าสู่ระบบ หากการเข้าสู่ระบบสำเร็จ parseuser currentuser() จะคืนค่าผู้ใช้เป็นวัตถุ และวัตถุ session จะถูกสร้างในแดชบอร์ด มิฉะนั้น หากชื่อผู้ใช้ที่ต้องการไม่มีอยู่ หรือรหัสผ่านไม่ถูกต้อง จะคืนค่า null ในการออกจากระบบให้ทำตามขั้นตอนด้านล่าง ไปที่ main storyboard, ลาก uibutton ที่เรียกว่า “logout”, และเพิ่ม action ระหว่าง uibutton นี้และ viewcontroller swift เพิ่มโค้ดต่อไปนี้ในฟังก์ชันนี้ 1 @ibaction func logout( sender any) { 2 pfuser logout() 3 } 5 โค้ดแอปพลิเคชัน 1 import uikit 2 import parse 3 4 class viewcontroller uiviewcontroller { 5 6 @iboutlet weak var txtusernamesignin uitextfield! 7 @iboutlet weak var txtpasswordsignin uitextfield! 8 @iboutlet weak var indicatorsignin uiactivityindicatorview! 9 10 @iboutlet weak var txtusernamesignup uitextfield! 11 @iboutlet weak var txtpasswordsignup uitextfield! 12 @iboutlet weak var indicatorsignup uiactivityindicatorview! 13 14 @iboutlet weak var btnlogout uibutton! 15 16 override func viewdidload() { 17 super viewdidload() 18 } 19 20 @ibaction func signin( sender any) { 21 pfuser loginwithusername(inbackground self txtusernamesignin text!, password self txtpasswordsignin text!) { 22 (user pfuser?, error error?) > void in 23 if user != nil { 24 self displayalert(withtitle "login successful", message "") 25 } else { 26 self displayalert(withtitle "error", message error! localizeddescription) 27 } 28 } 29 } 30 31 @ibaction func signup( sender any) { 32 let user = pfuser() 33 user username = self txtusernamesignup text 34 user password = self txtpasswordsignup text 35 36 self indicatorsignup startanimating() 37 user signupinbackground {(succeeded bool, error error?) > void in 38 self indicatorsignup stopanimating() 39 if let error = error { 40 self displayalert(withtitle "error", message error localizeddescription) 41 } else { 42 self displayalert(withtitle "success", message "account has been successfully created") 43 } 44 } 45 } 46 47 @ibaction func logout( sender any) { 48 pfuser logout() 49 } 50 51 func displayalert(withtitle title string, message string) { 52 let alert = uialertcontroller(title title, message message, preferredstyle alert) 53 let okaction = uialertaction(title "ok", style default) 54 alert addaction(okaction) 55 self present(alert, animated true) 56 } 57 58 } ส่วนติดต่อผู้ใช้ของแอปพลิเคชันจะมีลักษณะคล้ายกับนี้ 6 ทดสอบแอปของคุณ เรียกใช้แอปของคุณและสร้างผู้ใช้สองสามคน ลองเข้าสู่ระบบอีกครั้งหลังจากลงทะเบียนพวกเขา เข้าสู่ระบบที่ เว็บไซต์ back4app https //www back4app com/ ค้นหาแอปของคุณและคลิกที่ \<font color="#2166ae">แดชบอร์ด\</font> > \<font color="#2166ae">คอร์\</font> > \<font color="#2166ae">เบราว์เซอร์\</font> > \<font color="#2166ae">ผู้ใช้\</font> ลองเข้าสู่ระบบและออกจากระบบด้วยผู้ใช้เดียวกันและลงชื่อเข้าใช้ใหม่ ในจุดนี้ คุณควรเห็นผู้ใช้ของคุณตามที่แสดงด้านล่าง หมายเหตุ โดยใช้รหัสที่แสดงข้างต้น ทุกครั้งที่คุณเข้าสู่ระบบด้วยผู้ใช้ จะมีการเปิด \<font color="#2166ae">เซสชัน\</font> ใน \<font color="#2166ae">แดชบอร์ด\</font> , แต่เมื่อผู้ใช้ออกจากระบบ \<font color="#2166ae">เซสชัน\</font> นั้นจะสิ้นสุดลง นอกจากนี้ ทุกครั้งที่มีการพยายามเข้าสู่ระบบหรือสมัครสมาชิกที่ไม่สำเร็จ \<font color="#2166ae">เซสชัน\</font> ที่เปิดใน parse server \<font color="#2166ae">แดชบอร์ด\</font> จะถูกลบ เสร็จสิ้น! ในขั้นตอนนี้ คุณสามารถเข้าสู่ระบบ ลงทะเบียน หรือออกจากแอปของคุณโดยใช้ฟีเจอร์หลักของ parse server ผ่าน back4app!