利用UIWebView获取userAgent需要注意的地方
網絡通信有時候需要傳遞參數userAgent,iOS中獲取userAgent很簡單.
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];NSString *userAgentString = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];打印信息如下
userAgentString --> Mozilla/5.0 (iPod touch; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D167
有時候,你想在后臺線程中獲取userAgent,直接就有如下信息提示:
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...
找了一下資料,原來是這么回事:
Webviews and the Webkit JavaScript engine are both single-threaded and cannot be used on a background thread.
Webview以及Webkit Java腳本引擎,這兩個都是單線程的,不能夠在子線程中使用.
?
所以,不要試圖在子線程中獲取userAgent,雖然獲取userAgent耗時間,但第一次獲取后可以把userAgent值存儲起來,下回直接讀取就行了.
?
?
附錄:
stringByEvaluatingJavaScriptFromString使用方法 http://it.100xuexi.com/view/otdetail/20120511/abfdd193-50d1-43da-b49f-5411c014ad9b.html 使用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操作界面元素了。 1、獲取當前頁面的url。 1. - (void)webViewDidFinishLoad:(UIWebView *)webView { 2. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"]; 3. } 2、獲取頁面title: 1. - (void)webViewDidFinishLoad:(UIWebView *)webView { 2. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"]; 3. 4. NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"]; 5. } 3、修改界面元素的值。 1. NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"]; 4、表單提交: 1. NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "]; 這樣就實現了在google搜索關鍵字:“朱祁林”的功能。 5、插入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中的頁面元素。?
?
總結
以上是生活随笔為你收集整理的利用UIWebView获取userAgent需要注意的地方的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 游戏程序员的学习资料
- 下一篇: numpy中的ogrid