js调用swift相册DEMO(网易新闻)

关键代码

window.location.href = 'tg:///openCamera'

css

 body{
} img{
width:100%;
} #mainTitle{
text-align:center;
font-size:20px;
margin-top:20px;
margin-bottom:8px;
} #subTitle{
text-align:left;
font-size:12px;
color:gray;
} .time{
margin-right:10px;
margin-bottom:8px;
} .all-img{
text-align:center;
font-size:14px;
color:gray;
margin:8px 0;
}

js

 window.onload = function(){
var allImg = document.getElementsByTagName("img");
for(var i=0; i<allImg.length; i++){
var img = allImg[i];
img.id = i;
img.onclick = function(){
window.location.href = 'tg:///openCamera'
}
}
var img = document.createElement('img');
img.style.cssText = "width:50%";
img.src = 'https://avatars0.githubusercontent.com/u/22094559?v=3&s=460';
document.body.appendChild(img);
}

swift

 //
// ViewController.swift
// JSInSwiftDemo
//
// Created by targetcloud on 2016/12/13.
// Copyright © 2016年 targetcloud. All rights reserved.
// import UIKit class ViewController: UIViewController { @IBOutlet weak var webview: UIWebView! override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://c.m.163.com/nc/article/C85G09IV000189FH/full.html")
let request = URLRequest(url: url!)
let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if(error == nil){
let jsonData = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
self.dealNewsDetail(jsonData!)
}
})
dataTask.resume()
} func dealNewsDetail(_ jsonData: [String:Any]) {
guard let allData = jsonData["C85G09IV000189FH"] as? [String:Any] else {return}
var bodyHtml = allData["body"] as! String
let title = allData["title"] as! String
let ptime = allData["ptime"] as! String
let source = allData["source"] as! String
let imgArr = allData["img"] as! [[String: Any]]
for i in ..<imgArr.count{
let imgItem = imgArr[i]
let ref = imgItem["ref"] as! String
let imgTitle = imgItem["alt"] as! String
let src = imgItem["src"] as! String
let imgHtml = "<div class=\"all-img\"><img src=\"\(src)\"><div>\(imgTitle)</div></div>"
bodyHtml = bodyHtml.replacingOccurrences(of: ref, with: imgHtml)
}
let titleHtml = "<div id=\"mainTitle\">\(title)</div>"
let subTitleHtml = "<div id=\"subTitle\"><span>\(source)</span><span class=\"time\">\(ptime)</span></div>" let css = Bundle.main.url(forResource: "democss", withExtension: "css")
let cssHtml = "<link href=\"\(css!)\" rel=\"stylesheet\">" let js = Bundle.main.url(forResource: "demojs", withExtension: "js")
let jsHtml = "<script src=\"\(js!)\"></script>" let html = "<html><head>\(cssHtml)</head><body>\(titleHtml)\(subTitleHtml)\(bodyHtml)\(jsHtml)</body></html>"
webview.loadHTMLString(html, baseURL: nil)
} func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let requestString: NSString = (request.url?.absoluteString)! as NSString
let range = requestString.range(of: "tg:///")
let location = range.location
if(location != NSNotFound){
let method = requestString.substring(from: range.length)
let sel = NSSelectorFromString(method)
self.perform(sel)
}
return true
} func openCamera() {
let photoVC = UIImagePickerController()
photoVC.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(photoVC, animated: true, completion: nil)
}
}

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

运行效果如下:

js调用swift相册DEMO(网易新闻)

图1 是info.plist配置及运行效果,动态用 img数组替换新闻主体中的<!--IMG#0-->等占位

js调用swift相册DEMO(网易新闻)

图2是从自己在HTML文件中插入自己图片的效果

js调用swift相册DEMO(网易新闻)

图3是从charles中分析的结构

完整的DEMO 代码地址见github

https://github.com/targetcloud

上一篇:JavaScript高级用法二之内置对象


下一篇:Pip 安装 出现UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in