2016-03-30 50 views

回答

0

,如果你想顯示你看這樣

let htmlfilePath = NSBundle.mainBundle().URLForResource("filename", withExtension: "html"); 
    let req = NSURLRequest(URL: htmlfilePath !); 
    webview.loadRequest(req); 

下載文件的本地文件,您可以顯示在應用HTML頁面,而無需下載

let pageUrl = NSURL (string: "http://www.stackoverflow.com"); 
let req = NSURLRequest(URL: url!); 

你可以使用普通的NSURLSession

+0

謝謝您的回答,但我正在尋找一種方式通過下載頁面,然後用它來做到這一點。 – swiftttt

-1

你可以直接下載一個網站到NSDat這個例子中的一個對象(Objective-C);

NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.raywenderlich.com/tutorials"]; 
    NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl]; 

Swift example;

let aString = "http://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfp1/t31.0-8/q88/s720x720/10848858_907722502601079_2834541930671169073_o.jpg" 
    let url = NSURL(string: aString) 
    let data = NSData(contentsOfURL: url!) 

雷在這裏有一個很好的教程;

https://www.raywenderlich.com/14172/how-to-parse-html-on-ios

0

您應該使用NSURLSession dataTaskWithURL異步下載網站資料:

import UIKit 

class ViewController: UIViewController { 

    let stackoverflowLink = "https://stackoverflow.com/questions/36315798/how-to-download-a-page-in-swift2-without-using-third-party-libraries" 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     guard let url = NSURL(string: stackoverflowLink) else { return } 
     print("LOADING URL") 
     NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in 
      guard 
       let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200, 
       let data = data where error == nil, 
       let htmlCode = String(data: data, encoding: NSUTF8StringEncoding) // make sure you use the correct String Encoding 
      else { return } 
      print(htmlCode) 
      print("URL LOADED") 
     }.resume() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
+0

https://www.dropbox.com/s/xp245ik400ow5if/downloadHtml.zip?dl=0 –

相關問題