In iOS development, object and JavaScript are sometimes used to call each other, with detailed procedures such as the following:
1. Running JavaScript code in object, this is relatively simple, Apple provides a very good way
-(nsstring *) stringbyevaluatingjavascriptfromstring: (nsstring *) script
2. The data returned to object during JavaScript run or the object method is called. It needs to be used at this time.
The UIWebView address redirection feature. The main code such as the following:
(1) Create UIWebView
WebView = [[UIWebView alloc] initWithFrame:self.view.bounds]; Webview.delegate = self; [Self.view Addsubview:webview]; [Self loadwebpagewithstring:_url];-(void) loadwebpagewithstring: (nsstring*) urlstring{ nsurl *url =[NSURL Urlwithstring:urlstring]; Nsurlrequest *request =[nsurlrequest Requestwithurl:url]; [WebView loadrequest:request];}
(2) RealizeUIWebView Method
#pragma mark-uiwebviewdelegate-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype{nsstring *urlstring = [[Request URL] absolutestring]; Debuglog (@ "urlstring =%@", urlstring); NSString *prefix = @ "Myvideo"; if ([urlstring Hasprefix:prefix]) {nsstring *endstring = [URLString substringfromindex:7]; Debuglog (@ "Last urlstring =%@", endstring); return NO; } return YES; -(void) Webviewdidstartload: (UIWebView *) webview{}-(void) Webviewdidfinishload: (UIWebView *) webview{[self excuteJa Vascript];} -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nserror *) error{}-(void) excutejavascript{NSString *js = @ "var video = document.getElementsByTagName (' video ') [0];settimeout (getvideo,1000); function Getvideo () {video = document.getElementsByTagName (' video ') [0];if (video==undefined) {setTimeout (Getvideo, 1000);} else{Video.pause (); if (video.src== ") {var video = document.getElementsByTagName (' source ') [0];} document.location = ' Myvideo ' + video.getattribute (' src '); Injectedobject.playvideo (Video.getattribute (' src '));}} "; [WebView Stringbyevaluatingjavascriptfromstring:js];}
A description such as the following:
(1) When UIWebView loading the page, that is, the time to run to the Webviewdidfinishload agent. Run a section of JavaScript code. The function of this code is to get the video address in the Web page, "document.location = ' myvideo ' + video.getattribute (' src ') in the Code; "This is especially important,document.location is to do address redirection, run out of this JavaScript code." Then it will run .
-(BOOL) WebView: (uiwebview *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) Navigationtype This proxy method, which captures the address given by Document.location Myvideo ' + video.getattribute (' src ');. You can remove the ' Myvideo ' head. You will be able to get what you need. where ' Myvideo ' is your custom head. is to facilitate parsing of subsequent data.
IOS object and JavaScript call each other