你不應該使用NSData contentsOfURL初始化器來同步下載數據。無法保證它會成功。您需要使用NSURLSession方法dataTaskWithURL異步下載您的數據,因爲我已經在您的最後question處向您建議。關於你的新問題,已經回答here。結合這兩個答案,你應該能夠完成你想要的。更多信息在波紋管代碼::評論
import UIKit
class ViewController: UIViewController {
@IBOutlet var textview2: UITextView!
// declare your activityIndicator
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
// you will need a new view for your activity indicator and a label for your message
let messageFrame = UIView()
let strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 160, height: 50))
func displayActivityIndicator() {
strLabel.text = "Loading file"
strLabel.textColor = UIColor.whiteColor()
messageFrame.layer.cornerRadius = 15
messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
messageFrame.addSubview(activityIndicator)
messageFrame.addSubview(strLabel)
view.addSubview(messageFrame)
}
override func viewDidAppear(animated: Bool) {
// set your message frame (you can also position it using textview2.frame
messageFrame.frame = CGRect(x: view.frame.midX - 80, y: view.frame.midY - 25 , width: 160, height: 50)
// call your activity indicator method
displayActivityIndicator()
// call your method to download your data asynchronously
getRTFDataFromLink("https://dl.dropboxusercontent.com/u/87285547/test.rtf", textview2)
}
func getRTFDataFromLink(link: String,_ textView: UITextView) {
// make sure your link is valid NSURL using guard
guard let rtfURL = NSURL(string: link) else { return }
// creata a data task for your url
NSURLSession.sharedSession().dataTaskWithURL(rtfURL) {
(data, response, error) in
// use guard to make sure you get a valid response from the server and your data it is not nil and you got no errors otherwise return
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let data = data where error == nil
else { return }
// you need to use dispatch async to update the UI
dispatch_async(dispatch_get_main_queue(), {() -> Void in
// stop animating the activity indicator and remove the messageFrame from the view
self.activityIndicator.stopAnimating()
self.messageFrame.removeFromSuperview()
// NSAttributedString data initialiser throws an error so you need to implement Swift2 Do Try Catch error handling
do {
textView.attributedText = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType], documentAttributes: nil)
textView.editable = false
} catch let error as NSError {
print(error.localizedDescription)
}
})
}.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
https://www.dropbox.com/s/nt99imgsums2db8/async%20textField%20activity.zip?dl=0 –
我怎麼能有出多個網址複製代碼爲每個鏈接和textview謝謝 –
檢查我的編輯,你只需要添加另一個參數到textView對象,並通過它時,你調用該方法 –