我目前正在進行登錄測試,必須重新登錄才能登錄。按登錄按鈕向服務器發送請求後,活動指示燈將顯示在屏幕上,直到活動指示燈被停止並且活動指示器被停止在下一個頁面中,我想打開另一個用於執行活動指示器的線程,但我不知道如何實現,請幫忙! 此代碼從http://www.kaleidosblog.com/how-to-create-a-login-screen-page-in-swift-3-authenticate-an-user-and-keep-the-session-active修改。帶活動指示燈的登錄功能
class ViewController: UIViewController {
let login_url = "http://www.kaleidosblog.com/tutorial/login/api/Login"
let checksession_url = "http://www.kaleidosblog.com/tutorial/login/api/CheckSession"
@IBOutlet var username_input: UITextField!
@IBOutlet var password_input: UITextField!
@IBOutlet var login_button: UIButton!
var login_session:String = ""
var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
override func viewDidLoad() {
super.viewDidLoad()
username_input.text = "[email protected]"
password_input.text = "test"
activityIndicator.hidesWhenStopped = true;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray;
activityIndicator.center = view.center;
let preferences = UserDefaults.standard
if preferences.object(forKey: "session") != nil
{
login_session = preferences.object(forKey: "session") as! String
check_session()
}
}
@IBAction func DoLogin(_ sender: AnyObject) {
login_now(username:username_input.text!, password: password_input.text!)
}
@IBAction func backToMain(_ segue:UIStoryboardSegue){
let preferences = UserDefaults.standard
preferences.removeObject(forKey: "session")
self.dismiss(animated: true, completion: nil)
}
func login_now(username:String, password:String)
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(username, forKey: "username")
post_data.setValue(password, forKey: "password")
let url:URL = URL(string: login_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let data_block = server_response["data"] as? NSDictionary
{
if let session_data = data_block["session"] as? String
{
self.login_session = session_data
let preferences = UserDefaults.standard
preferences.set(session_data, forKey: "session")
DispatchQueue.main.async {
self.view.addSubview(self.activityIndicator)
}
}
}
})
task.resume()
LoginDone()
}
func check_session()
{
let post_data: NSDictionary = NSMutableDictionary()
post_data.setValue(login_session, forKey: "session")
let url:URL = URL(string: checksession_url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in post_data
{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let server_response = json as? NSDictionary else
{
return
}
if let response_code = server_response["response_code"] as? Int
{
if(response_code == 200)
{
DispatchQueue.main.async {
self.view.addSubview(self.activityIndicator)
}
}
else
{
}
}
})
task.resume()
}
func LoginDone()
{
self.performSegue(withIdentifier: "gotomenu", sender: nil)
}
func startActivityIndicator(){
activityIndicator.startAnimating()
}
func stopActivityIndicator() {
activityIndicator.stopAnimating()
}
}
類似http://stackoverflow.com/questions/38457750/use-activity-indicator-in-many-vc- without-duplicating-code-swift/ –