通过上一篇文章我们已经分析过了数据,那么现在将要实现详情页面的信息展示。以下我们以Swift方式编码。
创建工程
创建工程,命名iOS-html-part1,因为要访问数据,在TARGETS->Info-Custom iOS Target Progerties 添加App Transport SecuritySettings,设置其属性Allow Arbitray Loads 为Yes,如下图,

我们要实现webView与html的交互,所以我们要创建UIWebView,

接下来,我们要请求数据,通过异步请求,
override func viewDidLoad() {
super.viewDidLoad()
// http://c.m.163.com/nc/article/BSB33M1000234KO7/full.html
// 1.设置url
let url = NSURL(string: "http://c.m.163.com/nc/article/BSB33M1000234KO7/full.html");
// 2.设置请求
let request = NSURLRequest(URL: url!);
// 3.开启异步请求操作
let dataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
if (error == nil){
// 转为json数据,try? 解决throws异常, as!强制转换
let jsonData = try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary;
self.dealNewsDetail(jsonData!);
}
}
// 4.开启请求
dataTask.resume();
}
// 处理拿到的数据并显示
func dealNewsDetail(jsonData: NSDictionary) -> Void {
// 1.取出所有内容
let allData = jsonData["BSB33M1000234KO7"];
// 2.取出body中的内容
let bodyHtml = allData!["body"] as! String;
// 把对应的内容显示daowebView中
webview.loadHTMLString(bodyHtml, baseURL: nil);
}
模拟器运行效果,如下图,即完成

网友评论
Allow Arbitrary Loads,少个字母
我复制黏贴,搞了好久,还是报错,害苦我了