在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
实现原理: 1.通过UIWebView的stringByEvaluatingJavaScriptFromString方法来触发脚本 2.通过自定义连接来触发oc代码 实现过程 @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://passport.cnblogs.com/user/signin")!)) webView.delegate=self } //连接改变时 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{ var rurl = request.URL?.absoluteString if (rurl!.hasPrefix("ios:")){ var method = rurl!.componentsSeparatedByString("@")[1] if method == "signin_go"{ signin_go() } return false } return true } //加载完成时 func webViewDidFinishLoad(webView: UIWebView){ //注入脚本,这个脚本把登录方法js重写为连接 var jsPath = NSBundle.mainBundle().pathForResource("app", ofType: "js") var jsContent:NSString = NSString (contentsOfFile: jsPath!, encoding: 0, error: nil)! jsContent=jsContent.stringByReplacingOccurrencesOfString("\n", withString: "") webView.stringByEvaluatingJavaScriptFromString(jsContent as String) } func signin_go(){ NSLog("-我执行了signin_go-") } var script = document.createElement('script'); script.type = 'text/javascript'; script.text = ' function signin_go(){ location.href="ios:@signin_go"; } '; document.getElementsByTagName('head')[0].appendChild(script);
转载请注明来源:http://www.cnblogs.com/wuxian/p/4470634.html |
请发表评论