日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用UIWebView获取userAgent需要注意的地方

發布時間:2025/7/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用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需要注意的地方的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。