gpt4 book ai didi

ios - 将 HTML 字符串下载到 iOS 中的 NSString 或 Swift String 变量中

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:35 25 4
gpt4 key购买 nike

Alternative titles

  • How to download a web page data in Objective-C on iPhone?
  • How to read entire content of an html resource file into an NSString *
  • How to download a web page data in Swift?
  • Saving HTML pages in an NSString

如何获取网页数据并将数据放入字符串中?

类似于:

NSString *webPageBody = GetWebPageData("www.mysite.com/webpage.html");

webPageBody 中,我想看到“html 我的网页 html”。

最佳答案

swift 3.0

guard let url = URL(string: "http://www.example.com") else {
return
}

do {
let html = try String(contentsOf: url)
}
catch {
//handle `error` here
}

swift 2.3

guard let url = NSURL(string: "http://www.example.com") else {
return
}

do {
let html = try String(contentsOfURL: url)
}
catch {
//handle `error` here
}

objective-C

NSString *url = @"http://www.example.com";
NSURL *urlRequest = [NSURL URLWithString:url];
NSError *err = nil;

NSString *html = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:&err];

if(err)
{
//Handle
}

关于ios - 将 HTML 字符串下载到 iOS 中的 NSString 或 Swift String 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1867002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com