IOS Object和javaScript相互調用

來源:互聯網
上載者:User

標籤:javascript   ios   object-c   

在IOS開發中有時會用到Object和javaScript相互調用,具體步驟如下:

1. Object中執行javascript代碼,這個比較簡單,蘋果提供了很好的方法

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

2. javascript執行過程中返回給Object的資料或者調用Object方法,這個時候就需要用到

UIWebView的地址重新導向功能,主要代碼如下:

(1)建立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)實現UIWebView方法

#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 excuteJavaScript];}- (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];}

說明如下:

(1)在UIWebView載入完網頁的時候,即執行到webViewDidFinishLoad代理的時候,執行一段javascript代碼,這段代碼的作用是擷取網頁中的視頻地址,代碼中“ document.location = ‘myvideo‘+ video.getAttribute(‘src‘); ”這段特別重要,document.location是為了做地址重新導向用的,執行完這段javascript代碼,接著就會執行

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 這個代理方法,其中就會捕獲到document.location給出的地址即‘myvideo‘+ video.getAttribute(‘src‘); 。你可以去掉‘myvideo‘頭,就可以得到你需要的東西了,其中‘myvideo‘是你自己定義的一個頭,是為了便於解析後面的資料。







IOS Object和javaScript相互調用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.