ios 擷取或修改網頁上的內容,ios擷取修改網頁

來源:互聯網
上載者:User

ios 擷取或修改網頁上的內容,ios擷取修改網頁

UIWebView是iOS最常用的SDK之一,它有一個stringByEvaluatingJavaScriptFromString方法可以將javascript嵌     入頁面中,通過這個方法我們可以在iOS中與UIWebView中的網頁元素互動。

stringByEvaluatingJavaScriptFromString

    使用stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的頁面載入完成之後去調用。我們在界     面上拖放一個UIWebView控制項。在Load中將google mobile載入到這個控制項中,代碼如下:

- (void)viewDidLoad {     [super viewDidLoad];     webview.backgroundColor = [UIColor clearColor];      webview.scalesPageToFit =YES;    webview.delegate =self;   NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];    NSURLRequest *request =  [[NSURLRequest alloc] initWithURL:url];    [webview loadRequest:request];   }

我們在webViewDidFinishLoad方法中就可以通過javascript操作介面元素了。

1、擷取當前頁面的url。

- (void)webViewDidFinishLoad:(UIWebView *)webView {       NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];  

2、擷取頁面title:

NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];    

3、修改介面元素的值。

NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"]; 

4、表單提交:

NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];  

5、擷取所有的html

NSString *allHtml = @"document.documentElement.innerHTML";NSString *allHtmlInfo = [webView stringByEvaluatingJavaScriptFromString:allHtml];

6、擷取網頁的一個值

NSString *htmlNum = @"document.getElementById('title').innerText";NSString *numHtmlInfo = [webView stringByEvaluatingJavaScriptFromString:htmlNum];

7、插入js代碼

上面的功能我們可以封裝到一個js函數中,將這個函數插入到頁面上執行,代碼如下:

if ([title compare: @"Google"]==NSOrderedSame ) {        [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement_x('script');"         "script.type = 'text/javascript';"         "script.text = "function myFunction() { "         "var field = document.getElementsByName('q')[0];"         "field.value='朱祁林';"         "document.forms[0].submit();"         "}";"         "document.getElementsByTagName_r('head')[0].appendChild(script);"];               [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];    }  

看上面的代碼:

a、首先通過js建立一個script的標籤,type為'text/javascript'。

b、然後在這個標籤中插入一段字串,這段字串就是一個函數:myFunction,這個函數實現google自動搜尋索引鍵的功能。

c、然後使用stringByEvaluatingJavaScriptFromString執行myFunction函數。

相關文章

聯繫我們

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