IOS object and JavaScript call each other

Source: Internet
Author: User

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

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.