
網絡通訊有時候須要傳遞參數userAgent,iOS中獲取userAgent很簡單.javascript
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgentString = [webView stringByEvaluatingJavaScriptFromString:
@"navigator.userAgent"];
打印信息以下html
userAgentString --> Mozilla/5.0 (iPod touch; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D167java
有時候,你想在後臺線程中獲取userAgent,直接就有以下信息提示:web
bool _WebTryThreadLock(bool), 0x14e6b590: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...網絡
找了一下資料,原來是這麼回事:app
Webviews and the Webkit JavaScript engine are both single-threaded and cannot be used on a background thread.函數
Webview以及Webkit Java腳本引擎,這兩個都是單線程的,不可以在子線程中使用.網站
因此,不要試圖在子線程中獲取userAgent,雖然獲取userAgent耗時間,但第一次獲取後能夠把userAgent值存儲起來,下回直接讀取就好了.ui
附錄:google
stringByEvaluatingJavaScriptFromString使用方法
使用stringByEvaluatingJavaScriptFromString方法,須要等UIWebView中的頁面加載完成以後去調用。咱們在界面上拖放一個UIWebView控件。在Load中將googlemobile加載到這個控件中,代碼以下:
1. - (void)viewDidLoad
2. {
3. [super viewDidLoad];
4. webview.backgroundColor = [UIColor clearColor];
5. webview.scalesPageToFit =YES;
6. webview.delegate =self;
7. NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
8.
9. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
10. [webview loadRequest:request];
11. }
咱們在webViewDidFinishLoad方法中就能夠經過javascript操做界面元素了。
一、獲取當前頁面的url。
1. - (void)webViewDidFinishLoad:(UIWebView *)webView {
2. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
3. }
二、獲取頁面title:
1. - (void)webViewDidFinishLoad:(UIWebView *)webView {
2. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
3.
4. NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];
5. }
三、修改界面元素的值。
1. NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"];
四、表單提交:
1. NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
這樣就實現了在google搜索關鍵字:「朱祁林」的功能。
五、插入js代碼
上面的功能咱們能夠封裝到一個js函數中,將這個函數插入到頁面上執行,代碼以下:
1. [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
2. "script.type = 'text/javascript';"
3. "script.text = \"function myFunction() { "
4. "var field = document.getElementsByName('q')[0];"
5. "field.value='朱祁林';"
6. "document.forms[0].submit();"
7. "}\";"
8. "document.getElementsByTagName('head')[0].appendChild(script);"];
9.
10. [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
看上面的代碼:
a、首先經過js建立一個script的標籤,type爲'text/javascript'。
b、而後在這個標籤中插入一段字符串,這段字符串就是一個函數:myFunction,這個函數實現google自動搜索關鍵字的功能。
c、而後使用stringByEvaluatingJavaScriptFromString執行myFunction函數。
演示:
第一步打開google mobile網站
第二步輸入關鍵字
第三步搜素
總結:這篇文章主要是講解了stringByEvaluatingJavaScriptFromString的用法,它的功能很是的強大,用起來很是簡單,經過它咱們能夠很方便的操做uiwebview中的頁面元素。