简体   繁体   English

如何在不使用第三方库的情况下在 swift2 中下载页面?

[英]How to download a page in swift2 without using third party libraries?

I am trying to show the downloaded HTML page in the app.我正在尝试在应用程序中显示下载的 HTML 页面。 Please explain the simplest way to do so without using any third party libraries.请解释不使用任何第三方库的最简单方法。

cheers,干杯,

You can show a html page in app without downloading无需下载即可在应用中显示 html 页面

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

if you want to show a local file you show like this如果你想显示一个本地文件,你可以这样显示

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

to download a file you can use plain NSURLSession下载一个文件,你可以使用普通的 NSURLSession

You should use NSURLSession dataTaskWithURL to download your website data asynchronously:您应该使用 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.
    }
}

You can download a web site directly into an NSData object as in this example (Objective-C);你可以直接下载一个网站到一个 NSData 对象中,就像这个例子(Objective-C)一样;

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

Swift example; Swift 示例;

    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 has a great tutorial here; Ray 在这里有一个很棒的教程;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速绘制图表,无需使用像core-plot这样的第三方库 - Plot graph in swift without using third party libraries like core-plot 有没有其他方法可以使用自定义标头和正文在不使用第三方库的情况下发送HTTP“ POST”请求? - Is there a different way how to send a HTTP “POST” request without using third party libraries using custom header and body? 如何在不使用任何第三方库的情况下跟踪来自iOS中所有设备的生产应用程序错误/崩溃 - How to track production app bugs/crashes from all devices in iOS without using any third party libraries 如何在没有任何第三方库的情况下在Swift 3.0中使用Alamofire解析JSON - How to parse JSON using Alamofire in Swift 3.0 without any third-party library 如何在 iOS 应用程序中使用 GIF,而不使用 UIKit 在 Swift 5 中的任何第三方库? - How to use GIFs inside an iOS App without any Third Party Library in Swift 5 using UIKit? 如何在iOS中正确链接第三方库 - How to link third party libraries properly in iOS 使用 Swift Package 管理器添加自动第三方库确认和许可证 - Automatic Third Party Libraries Acknowledgements and Licences added with Swift Package Manager 如何在不使用第三方库的情况下在Facebook上共享链接? - how to share a link on facebook without using third party library? 在swift2上管理页面导航 - managing page navigation at swift2 在没有 package.swift 的情况下安装第三方 Swift ZEFE90A8E604A7C840E88D03AD6 - Install a third party without package.swift with the Swift package manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM