JS Call Swift Album demo (NetEase News)

Source: Internet
Author: User

Key code

1 window.location.href = ' Tg:///opencamera '

Css

1 Body{2}3 4 img{5 width:100%;6}7 8 #mainTitle{9 text-align:Center;Ten font-size:20px; One Margin-top:20px; A Margin-bottom:8px; -} -  the #subTitle{ - text-align: Left; - font-size:12px; - Color:Gray; +} -  + . Time{ A Margin-right:10px; at Margin-bottom:8px; -} -  - . All-img{ - text-align:Center; - font-size:14px; in Color:Gray; - margin:8px 0; to}

Js

1Window.onload =function(){2     varallimg = document.getElementsByTagName ("img");3      for(vari=0; i<allimg.length; i++){4         varIMG =Allimg[i];5Img.id =i;6Img.onclick =function(){7window.location.href = ' Tg:///opencamera '8         }9     }Ten     varimg = document.createelement (' img ')); OneImg.style.cssText = "width:50%"; AIMG.SRC = ' https://avatars0.githubusercontent.com/u/22094559?v=3&s=460 '; - Document.body.appendChild (IMG); -}

Swift

1 //2 //Viewcontroller.swift3 //Jsinswiftdemo4 //5 //Created by Targetcloud on 2016/12/13.6 //Copyright 2016 Targetcloud. All rights reserved.7 //8 9 Import UIKitTen  One classViewcontroller:uiviewcontroller { A  -@IBOutlet weak var webview:uiwebview! -      the     Overridefunc 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)inch -             if(Error = =Nil) { +Let Jsondata =Try? Jsonserialization.jsonobject (with:data!, options:JSONSerialization.ReadingOptions.allowFragments) as![String:any] ASelf.dealnewsdetail (jsondata!) at             } -         }) - Datatask.resume () -     } -  - Func Dealnewsdetail (_ Jsondata: [String:any]) { inGuard Let AllData = jsondata["C85G09IV000189FH"] as? [String:any]Else{return} -var bodyhtml = alldata["Body"] as!String toLet title = alldata["title"] as!String +Let Ptime = alldata["Ptime"] as!String -Let Source = alldata["Source"] as!String theLet Imgarr = alldata["img"] as![[String:any]] *          forIinch 0.. <imgarr.count{ $Let Imgitem =Imgarr[i]Panax NotoginsengLetref= imgitem["ref"] as!String -Let Imgtitle = imgitem["alt"] as!String theLet src = imgitem["src"] as!String +Let imghtml ="<div class=\ "all-img\" ><div>\ (imgtitle) </div></div>" Abodyhtml = Bodyhtml.replacingoccurrences (of:ref, with:imghtml) the         } +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\ ">" -          theLet JS = Bundle.main.url (forresource:"Demojs", Withextension:"JS") -Let jshtml ="<script src=\ "\ (js!) \ "></script>"Wuyi          theLet HTML ="" - webview.loadhtmlstring (HTML, Baseurl:nil) Wu     } -      AboutFunc WebView (_ Webview:uiwebview, Shouldstartloadwith request:urlrequest, Navigationtype:uiwebviewnavigationtype)- >Bool { $Let requeststring:nsstring = (request.url?. absolutestring)! asNSString -Let range = Requeststring.range (of:"tg:///") -Let location =range.location -         if(Location! =nsnotfound) { ALet method =requeststring.substring (from:range.length) +Let sel =nsselectorfromstring (method) the self.perform (SEL) -         } $         return true the     } the      the func Opencamera () { theLet PHOTOVC =Uiimagepickercontroller () -Photovc.sourcetype =uiimagepickercontrollersourcetype.photolibrary inSelf.present (PHOTOVC, animated:true, Completion:nil) the     } the}

Note the info.plist ATS and Privacy-photo Library Usage in the project Description

The results are as follows:

Figure 1 is the info.plist configuration and operation effect, dynamically replace the <!--img#0--> in the news body with the IMG array

Figure 2 is the effect of inserting your own picture in an HTML file

Figure 3 is the structure analyzed from Charles

Full demo code address at GitHub

Https://github.com/targetcloud

JS Call Swift album demo (NetEase News)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.