2016-03-30 3 views

Antwort

0

Sie eine HTML-Seite in App

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

wenn Sie eine lokale Datei anzeigen möchten Ihnen zeigen, wie diese

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

zum Download einer Datei anzeigen kann Sie können plain NSURLSession verwenden

+0

Vielen Dank für Ihre Antwort, aber ich bin auf der Suche nach einem Weg, es zu tun, indem Sie die Seite Download und die Verwendung es dann. – swiftttt

-1

Sie können eine Website direkt in eine NSDat herunterladen ein Objekt wie in diesem Beispiel (Objective-C);

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

Schnelle Beispiel;

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!) 

Ray hat ein tolles Tutorial hier;

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

0

sollten Sie NSURLSession dataTaskWithURL verwenden, um Ihre Website-Daten herunterladen asynchron:

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 –

Verwandte Themen