Implementation principle:
1. Trigger the script by UIWebView's Stringbyevaluatingjavascriptfromstring method
2. Trigger OC Code with a custom connection
Implementation process
@IBOutlet weakvarwebview:uiwebview!Overridefunc viewdidload () {super.viewdidload () webview.loadrequest (Nsurlrequest (Url:nsurl (string:"Http://passport.cnblogs.com/user/signin")!) ) WebView.Delegate=Self }//when the connection changesFunc WebView (Webview:uiwebview, Shouldstartloadwithrequest request:nsurlrequest, Navigationtype: Uiwebviewnavigationtype)bool{varRurl = Request. Url?. absolutestringif(rurl!. Hasprefix ("iOS:")){ varmethod = Rurl!.componentsseparatedbystring ("@")[1] ifmethod = ="Signin_go"{signin_go ()}return false } return true } //when loading is completefunc webviewdidfinishload (webview:uiwebview) {//inject script, this script to the login method JS rewrite as a connection varJspath = Nsbundle.mainbundle (). Pathforresource ("app", OfType:"JS") varjscontent:nsstring = NSString (contentsoffile:jspath!, encoding:0, Error:nil)!jscontent=jscontent.stringbyreplacingoccurrencesofstring ("\ n", withstring:"") webview.stringbyevaluatingjavascriptfromstring (jscontent asString)} Func Signin_go () {NSLog ("-I executed the signin_go-.") }
var script = document.createelement ( script " = " text/javascript " ;script.text = '
Reprint please indicate source: http://www.cnblogs.com/wuxian/p/4470634.html
Step-by-step learning of Swift (four) play turn UIWebView