关键代码

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 UIKit
  10.  
  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) in
  20. 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! String
  31. let title = allData["title"] as! String
  32. let ptime = allData["ptime"] as! String
  33. let source = allData["source"] as! String
  34. let imgArr = allData["img"] as! [[String: Any]]
  35. for i in ..<imgArr.count{
  36. let imgItem = imgArr[i]
  37. let ref = imgItem["ref"] as! String
  38. let imgTitle = imgItem["alt"] as! String
  39. let src = imgItem["src"] as! String
  40. 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 NSString
  58. let range = requestString.range(of: "tg:///")
  59. let location = range.location
  60. if(location != NSNotFound){
  61. let method = requestString.substring(from: range.length)
  62. let sel = NSSelectorFromString(method)
  63. self.perform(sel)
  64. }
  65. return true
  66. }
  67.  
  68. func openCamera() {
  69. let photoVC = UIImagePickerController()
  70. photoVC.sourceType = UIImagePickerControllerSourceType.photoLibrary
  71. 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(网易新闻)的更多相关文章

  1. Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)

    在前面的文章中介绍的了如何使用Cordova进行跨平台应用的开发,使用Cordova的话基本上就不需要在写系统原生代码了,只要通过编写html页面和js方法即可. 但在有些特殊情况下,还是是需要htm ...

  2. iOS学习之网易新闻简易Demo

    简易NewsDemo代码分析 界面布局就不多说了.效果图:(自定义了三套Cell,最后一套Cell是页面最下方的"正在加载中..."Cell,图三.)         主要分析工程 ...

  3. Android调用系统相册和拍照的Demo

    最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...

  4. iOS UIWebView 中 js调用OC 打开相册 获取图片, OC调用js 将图片加载到html上

    线上html <!DOCTYPE html> <html> <head> <title>HTML中用JS调用OC方法</title> < ...

  5. Swift—调用系统相册和相机

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  6. IE浏览器中使用js调用cmd命令行demo

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  7. 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...

  8. 基于VUE,VUX组件开发的网易新闻页面搭建过程

    根据妙味课堂上的一个教程练习总结,供自己复习用 一.功能介绍 一个网易新闻客户端的浏览页面,通过网易新闻的api接口实时获取新闻数据,用vux搭建样式框架,以轮播图,文字滚动,图文列表等形式把内容展示 ...

  9. iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文  http://blog.csdn.net/hmt20130412/article/det ...

随机推荐

  1. iOS平台下cookie的使用

    iOS平台下cookie的使用 首先,先介绍下iOS对cookie的操作的两个类: 帖子来源于:http://blog.csdn.net/chun799/article/details/1720690 ...

  2. WebApi2官网学习记录---BSON

    BSON 是轻量级的,能够进行快速查询和高效的解码/编码.BSON方便查询是由于elements的前面都有一个表示长度的字段,所以解释器可以快速跳过这个elements:高效的解码/编码是因为nume ...

  3. HTMl5的sessionStorage和localStorage(转)

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...

  4. Android中多表的SQLite数据库(译)

    原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...

  5. Linux下MySql启动时报错

    /opt/mysql/support-files/mysql.server startmy_print_defaults: [ERROR] Found option without preceding ...

  6. ios学习资料(一)

    IT社区: http://www.cnblogs.com/ http://www.csdn.net/ http://www.51cto.com/ http://www.cocoachina.com/ ...

  7. call()与apply()传参需要注意的一点

    call()与apply()是用来改变函数体内的this指向,第一个参数是调用函数的母对象,他是调用上下文,函数体内通过this来获得对它的引用,换句话说就是第一参数===函数中的this. 但是如下 ...

  8. shell编程-1到100的求和与冒泡排序

    Shell编程 一.  for循环 生成列表 {起始数..结束数} 命令生成列表 `seq [起始数] [步进长度] 结束数 ` for  l in {1..5};do for  l in `seq ...

  9. linux上安装apache以及httpd.conf基本配置

    1.yum安装apache #yum install httpd -y 2.随系统自启动 #chkconfig httpd on 3.开启apache #service httpd start PS: ...

  10. JS 多种变量定义

    对象直接量创建一个对象:  var obj = {x:[1,2],y:23}; 代码跟下面是一样的.  var obj=new Object(); obj.x=new Array(1,2); obj. ...