class BasicViewController: UIViewController {
@IBOutlet weak var sampleTextView: UITextView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
startSampleRequest()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//------------------------------------------------------------------------------------
// action setting
//------------------------------------------------------------------------------------
extension BasicViewController {
func startSampleRequest() {
guard let url = URL.init(string: "http://localhost:3001") else { return }
// guard let url = URL.init(string: "https://www.baidu.com/") else { return }
let session = URLSession.shared
let task = session.dataTask(with: url) { (data, response, error) in
if error != nil {
print("error: \(String(describing: error))")
return
}
guard let data = data else { return }
DispatchQueue.main.async {
sleep(2) // 为了测试效果, 暂停2秒
let text = String.init(data: data, encoding: String.Encoding.utf8)
self.toggleActivityIndicator(target: self.activityIndicator)
self.sampleTextView.text = text
}
}
task.resume()
}
}
//------------------------------------------------------------------------------------
// style setting
//------------------------------------------------------------------------------------
extension BasicViewController {
private func toggleActivityIndicator(target: UIActivityIndicatorView) {
if target.isAnimating {
target.stopAnimating()
} else {
target.startAnimating()
}
}
}