課程筆記文集地址:Udemy課程:The Complete iOS 9 Developer Course - Build 18 Apps
Section 8 主要的內(nèi)容是克隆 Instagram:107 - 128課赏陵。
本節(jié)課主要講解如何使用 LeanCloud 實(shí)現(xiàn)注冊(cè)饼齿、登錄的功能。請(qǐng)按照 101 課創(chuàng)建工程蝙搔,然后繼續(xù)下面的操作缕溉。
一、布局 Storyboard
拖入控件吃型,設(shè)置 AutoLayout 約束证鸥,創(chuàng)建 Outlet 和 Action 連接。如下圖:
二勤晚、創(chuàng)建變量
在類里創(chuàng)建兩個(gè)變量:
var signupActive = true
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
加上之前的 Outlet 連接枉层,一共有如下變量:
@IBOutlet var username: UITextField!
@IBOutlet var password: UITextField!
@IBOutlet var signupButton: UIButton!
@IBOutlet var registeredText: UILabel!
@IBOutlet var loginButton: UIButton!
var signupActive = true
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
三、點(diǎn)擊注冊(cè)登錄按鈕
1赐写、沒有輸入信息的情況下點(diǎn)擊注冊(cè)按鈕要有提示:
@IBAction func signUp(sender: AnyObject) {
if username.text == "" || password.text == "" {
// 出現(xiàn)提示信息
displayAlert("Error in form", message: "Please enter a username and password")
} else {
//這里開始實(shí)現(xiàn)注冊(cè)的功能
}
}
這里這個(gè)提示框使用了一個(gè)單獨(dú)的方法鸟蜡,傳入不同的參數(shù),一次就能搞定這個(gè)頁(yè)面出現(xiàn)的所有的提示:
func displayAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})))
self.presentViewController(alert, animated: true, completion: nil)
}
2挺邀、點(diǎn)擊注冊(cè)后要出現(xiàn)一個(gè)旋轉(zhuǎn)提示揉忘,表示正好和服務(wù)器進(jìn)行交互
@IBAction func signUp(sender: AnyObject) {
if username.text == "" || password.text == "" {
displayAlert("Error in form", message: "Please enter a username and password")
} else {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
//禁止其他的交互行為跳座,禁止用戶的點(diǎn)擊等操作
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
}
}
3、注冊(cè)的實(shí)現(xiàn)方法
var errorMessage = "Please try again later"
let user = AVUser()
user.username = username.text
user.password = password.text
user.signUpInBackgroundWithBlock({ (success, error) -> Void in
// 讓旋轉(zhuǎn)圖標(biāo)不再旋轉(zhuǎn)
self.activityIndicator.stopAnimating()
// 允許用戶操作
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if error == nil {
// 注冊(cè)成功
else {
// 失敗的原因可能有多種,常見的是用戶名已經(jīng)存在。
if let errorString = error!.userInfo["error"] as? String {
errorMessage = errorString
}
self.displayAlert("Failed SignUp", message: errorMessage)
}
})
4.登錄的實(shí)現(xiàn)方法
AVUser.logInWithUsernameInBackground(username.text!, password: password.text!, block: { (user, error) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if user != nil {
// 登錄成功莹捡!
} else {
if let errorString = error!.userInfo["error"] as? String {
errorMessage = errorString
}
self.displayAlert("Failed Login", message: errorMessage)
}
})