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
运行效果如下:
图1 是info.plist配置及运行效果,动态用 img数组替换新闻主体中的<!--IMG#0-->等占位
图2是从自己在HTML文件中插入自己图片的效果
图3是从charles中分析的结构
完整的DEMO 代码地址见github
https://github.com/targetcloud
js调用swift相册DEMO(网易新闻)的更多相关文章
- Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)
在前面的文章中介绍的了如何使用Cordova进行跨平台应用的开发,使用Cordova的话基本上就不需要在写系统原生代码了,只要通过编写html页面和js方法即可. 但在有些特殊情况下,还是是需要htm ...
- iOS学习之网易新闻简易Demo
简易NewsDemo代码分析 界面布局就不多说了.效果图:(自定义了三套Cell,最后一套Cell是页面最下方的"正在加载中..."Cell,图三.) 主要分析工程 ...
- Android调用系统相册和拍照的Demo
最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...
- iOS UIWebView 中 js调用OC 打开相册 获取图片, OC调用js 将图片加载到html上
线上html <!DOCTYPE html> <html> <head> <title>HTML中用JS调用OC方法</title> < ...
- Swift—调用系统相册和相机
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...
- IE浏览器中使用js调用cmd命令行demo
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...
- 基于VUE,VUX组件开发的网易新闻页面搭建过程
根据妙味课堂上的一个教程练习总结,供自己复习用 一.功能介绍 一个网易新闻客户端的浏览页面,通过网易新闻的api接口实时获取新闻数据,用vux搭建样式框架,以轮播图,文字滚动,图文列表等形式把内容展示 ...
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文 http://blog.csdn.net/hmt20130412/article/det ...
随机推荐
- vmplayer中的fedora20无法进入图形界面
新装了台fedora20的虚拟机,安装升级了一些东西后.启动时过了fedora的logo画面后就是黑屏. 也没提示不论什么错误,好在shell还能进去.查看/var/log/anaconda/sysl ...
- Ubuntu下访问SSH
ssh程序分为有客户端程序openssh-client和服务端程序openssh-server.如果需要ssh登陆到别的电脑,需要安装openssh-client,该程序ubuntu是默认安装的.而如 ...
- Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下
在用用 Eclipse进行Java Web开发时,web应用中引用的jar须要复制到WEB-INF下的lib目录下,否则常常出现ClassNotFound异常. 通过以下方法,能够不用手动拷贝jar包 ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- [CSAPP笔记][第六章存储器层次结构]
第六章 存储器层次结构 在简单模型中,存储器系统是一个线性的字节数组,CPU能够在一个常数访问每个存储器位置. 虽然是一个行之有效的模型,但没有反应现代系统实际工作方式. 实际上,存储器系统(memo ...
- KVO详解
NSKeyValueObserving非正式协议定义了一种机制,它允许对象去监听其它对象的某个属性的修改. 我们可以监听一个对象的属性,包括简单属性,一对一的关系,和一对多的关系.一对多关系的监听者会 ...
- let关键字
作用: 与var类似, 用于声明一个变量特点: 只在块作用域内有效 不能重复声明 不会预处理, 不存在提升应用: 循环遍历加监听 //应用实例 <body> <button>测 ...
- 【Solr初探】Solr安装,启动,查询,索引
1. 安装&启动 官网:http://lucene.apache.org/solr/ 下载源代码,解压,进入根目录(我把solr放在/usr/local/solr下) 在/usr/local/ ...
- hdu Find a way
算法:广搜: Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Le ...
- C语言中的memset函数和数组指针
代码: #include <iostream> #include <cstring> using namespace std; int main(){ ] = {}; mems ...