js調用swift相簿DEMO(網易新聞)

來源:互聯網
上載者:User

標籤:ges   cli   handle   tor   resume   article   分享   檔案中   ret   

關鍵代碼

 1 window.location.href = ‘tg:///openCamera‘ 

css

 1 body{ 2 } 3  4 img{ 5     width:100%; 6 } 7  8 #mainTitle{ 9     text-align:center;10     font-size:20px;11     margin-top:20px;12     margin-bottom:8px;13 }14 15 #subTitle{16     text-align:left;17     font-size:12px;18     color:gray;19 }20 21 .time{22     margin-right:10px;23     margin-bottom:8px;24 }25 26 .all-img{27     text-align:center;28     font-size:14px;29     color:gray;30     margin:8px 0;31 }

js

 1 window.onload = function(){ 2     var allImg = document.getElementsByTagName("img"); 3     for(var i=0; i<allImg.length; i++){ 4         var img = allImg[i]; 5         img.id = i; 6         img.onclick = function(){ 7             window.location.href = ‘tg:///openCamera‘ 8         } 9     }10     var img = document.createElement(‘img‘);11     img.style.cssText = "width:50%";12     img.src = ‘https://avatars0.githubusercontent.com/u/22094559?v=3&s=460‘;13     document.body.appendChild(img);14 }

swift

 1 // 2 //  ViewController.swift 3 //  JSInSwiftDemo 4 // 5 //  Created by targetcloud on 2016/12/13. 6 //  Copyright ? 2016年 targetcloud. All rights reserved. 7 // 8  9 import UIKit10 11 class ViewController: UIViewController {12 13     @IBOutlet weak var webview: UIWebView!14     15     override func viewDidLoad() {16         super.viewDidLoad()17         let url = URL(string: "http://c.m.163.com/nc/article/C85G09IV000189FH/full.html")18         let request = URLRequest(url: url!)19         let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in20             if(error == nil){21                 let jsonData = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]22                 self.dealNewsDetail(jsonData!)23             }24         })25         dataTask.resume()26     }27 28     func dealNewsDetail(_ jsonData: [String:Any])  {29         guard let allData = jsonData["C85G09IV000189FH"] as? [String:Any] else {return}30         var bodyHtml = allData["body"] as! String31         let title = allData["title"] as! String32         let ptime = allData["ptime"] as! String33         let source = allData["source"]  as! String34         let imgArr = allData["img"] as! [[String: Any]]35         for i in 0..<imgArr.count{36             let imgItem = imgArr[i]37             let ref = imgItem["ref"] as! String38             let imgTitle = imgItem["alt"] as! String39             let src = imgItem["src"] as! String40             let imgHtml = "<div class=\"all-img\"><img src=\"\(src)\"><div>\(imgTitle)</div></div>"41             bodyHtml = bodyHtml.replacingOccurrences(of: ref, with: imgHtml)42         }43         let titleHtml = "<div id=\"mainTitle\">\(title)</div>"44         let subTitleHtml = "<div id=\"subTitle\"><span>\(source)</span><span class=\"time\">\(ptime)</span></div>"45         46         let css = Bundle.main.url(forResource: "democss", withExtension: "css")47         let cssHtml = "<link href=\"\(css!)\" rel=\"stylesheet\">"48         49         let js = Bundle.main.url(forResource: "demojs", withExtension: "js")50         let jsHtml = "<script src=\"\(js!)\"></script>"51         52         let html = "<html><head>\(cssHtml)</head><body>\(titleHtml)\(subTitleHtml)\(bodyHtml)\(jsHtml)</body></html>"53         webview.loadHTMLString(html, baseURL: nil)54     }55     56     func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {57         let requestString: NSString = (request.url?.absoluteString)! as NSString58         let range = requestString.range(of: "tg:///")59         let location = range.location60         if(location != NSNotFound){61             let method = requestString.substring(from: range.length)62             let sel = NSSelectorFromString(method)63             self.perform(sel)64         }65         return true66     }67     68     func openCamera() {69         let photoVC = UIImagePickerController()70         photoVC.sourceType = UIImagePickerControllerSourceType.photoLibrary71         self.present(photoVC, animated: true, completion: nil)72     }73 }

注意工程的info.plist中加ATS及Privacy - Photo Library Usage Description

運行效果如下:

圖1 是info.plist配置及運行效果,動態用 img數組替換新聞主體中的<!--IMG#0-->等佔位

圖2是從自己在HTML檔案中插入自己圖片的效果

圖3是從charles中分析的結構

 

完整的DEMO 代碼地址見github

https://github.com/targetcloud

js調用swift相簿DEMO(網易新聞)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.