网易新闻详情页

通过请求html页面,自己解析数据,自己拼接html,自定义css和js文件,可以修改页面上的所有样式。实现点击图片浏览,保存。
DEMO地址:https://github.com/zdq1179169386/WangYiNews

import UIKit
var images = [SKPhoto]();
class ViewController: UIViewController ,UIWebViewDelegate,SKPhotoBrowserDelegate{
@IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
    super.viewDidLoad()
这些链接是通过抓包网易新闻客户端获取到的。
//        http://c.3g.163.com/nc/article/BMU8KHQD00964J4O/full.html
//        http://c.m.163.com/nc/article/BUOAKQT700237VUV/full.html
//        http://c.m.163.com/nc/article/BUCHPIS100963VRO/full.html
    let url = NSURL(string: "http://c.m.163.com/nc/article/BUCHPIS100963VRO/full.html");
    
    let request = NSURLRequest(URL: url!);
    
    let dataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
        
        if(error == nil)
        {
            let json = try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments);
//                print(json);
            self.dealNewsDetail(json as! NSDictionary);
        }
    }
    dataTask.resume();
    
}
func dealNewsDetail(json:NSDictionary) -> Void {
//        这里的key BUCHPIS100963VRO,根据上面的url决定
    let alldata = json["BUCHPIS100963VRO"];
    
    var body = alldata!["body"] as! String;
    
    let title = alldata!["title"] as! String;
    
    
    let ptime = alldata!["ptime"] as! String;
    
    let source = alldata!["source"] as! String;
//        [[String : AnyObject]] 数组包含字典
    let img = alldata!["img"] as! [[String : AnyObject]];
    
    for i in 0..
\(imgTitle)
"; body = body.stringByReplacingOccurrencesOfString(ref, withString: imgHtml); } let titleHtml = "
\(title)
"; let subTitleHtml = "
\(ptime)\(source)
" let css = NSBundle.mainBundle().URLForResource("newsDetail", withExtension: "css"); let cssHtml = ""; let js = NSBundle.mainBundle().URLForResource("newDetail", withExtension: "js"); let jsHtml = ""; let html = "\(cssHtml)\(titleHtml)\(subTitleHtml)\(body)\(jsHtml)" webView.loadHTMLString(html, baseURL: nil); } func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { let requestStr : String = (request.URL?.absoluteString)!; // print(requestStr); if ((requestStr.rangeOfString("wy://")) != nil) { let str = requestStr.substringFromIndex(requestStr.rangeOfString("wy://")!.endIndex); let method = str.componentsSeparatedByString("+"); let sel = NSSelectorFromString(method.first!); self.performSelector(sel, withObject: method.last); } return true; } func webViewDidFinishLoad(webView: UIWebView) { let getImages = "function getImages(){var imgs = document.getElementsByTagName(\"img\");var allStr = \"\";for(var i = 0;i Void { let browser = SKPhotoBrowser(photos: images) browser.initializePageIndex(Int(imgUrl)!) presentViewController(browser, animated: true, completion: {}) } }

你可能感兴趣的:(网易新闻详情页)