iOS native如何擷取web介面的title、image等

來源:互聯網
上載者:User

標籤:

 轉載地址:http://blog.csdn.net/justinjing0612/article/details/34127597

最近幾年都很流行hybrid開發模式,這樣就少不了native和Html介面的一些互動,或者方法的調用。

         的分享火熱我想也說大部門app模仿的一個典範。今天我們就說分享的一個細節。

我們瀏覽朋友的朋友圈後,如果想分享到自己朋友圈,那我們就需要知道webview 的title 和 image,問題來了,我們怎麼知道呢?

分享介面

以前我沒有接觸過的時候感覺好神奇啊,好強大啊,這個也可以,但是最近無意才發現很簡單,就是一句話二句話的問問題。

具體廢話不多說了。直接先看代碼吧。

 

[html] view plaincopy 
  1. self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];  
  2.        NSLog(@"title===%@",self.title);  
  3.    NSString *articleImageUrl = [_webView stringByEvaluatingJavaScriptFromString:@"document.images[0].src"];  
  4.        NSLog(@"imageurl===%@",articleImageUrl);  

 

 

在控制台再看看我們的輸出結果:

 

[html] view plaincopy 
  1. 2014-06-24 19:31:19.182 DemoApp[734:60b] title===百度一下  
  2. 2014-06-24 19:31:19.183 DemoApp[734:60b] imageurl===http://m.baidu.com/static/index/logo_index2.png  

 

哈哈,大功告成。就這麼簡單。

下面是一些類似的功能:

1、擷取web介面所有的html:

 

[html] view plaincopy 
  1. NSString *lJs = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];  


2、去除某個屬性:

 

 

[html] view plaincopy 
  1. [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.images[%d].removeAttribute("width")", i]];  


3、擷取當前頁面的url:

 

 

[html] view plaincopy 
    1. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];   

iOS native如何擷取web介面的title、image等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.