Android WebView userAgent 設(shè)置為桌面UA實例
最近一個大屏項目中使用到支付寶掃碼支付,但是webview加載掃碼支付鏈接時會自動跳轉(zhuǎn)到移動版頁面,網(wǎng)上查找怎么設(shè)置,沒找到解決方案。于是自己隨便試了下
webview.getSettings().setUserAgentString("PC");
或
webview.getSettings().setUserAgentString("電腦");
竟然真的可以。
userAgent可以設(shè)置瀏覽器標(biāo)識,Android/iphone/ipod/ipad/PC等,這個應(yīng)該有做類似模糊搜索一樣,傳相近的值就可以;它就會自動加載桌面版頁面或移動版頁面。前提是這些頁面要有桌面版頁面和移動版頁面,并且做了ua判斷跳轉(zhuǎn)相應(yīng)頁面。如果傳的ua識別不出來將自動加載桌面版頁面。
補充知識:自定義webView的userAgent
user-Agent 用戶代理,是指瀏覽器,它的信息包括硬件平臺、系統(tǒng)軟件、應(yīng)用軟件和用戶個人偏好。用戶代理的能力和偏好可以認(rèn)為是元數(shù)據(jù)或用戶代理的硬件和軟件的特性和描述。通過自定義user-Agent ,我們可以給特定的瀏覽器讀取特定的一些消息。
UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectZero]; NSString * oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSLog(@"old agent :%@", oldAgent); //add my info to the new agent NSString * newAgent = [oldAgent stringByAppendingString:@" SuGrand/2.4.7 ch_appstore"]; // or updata my info to the new agent // NSString * newAgent = [NSString stringWithFormat:@"Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H141"]; NSLog(@"new agent :%@", newAgent); //regist the new agent NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dic];
這樣,WebView在請求時的user-Agent 就是我們設(shè)置的這個了,如果需要在WebView 使用過程中再次變更user-Agent,則需要再通過這種方式修改user-Agent, 然后再重新實例化一個WebView。
__weak typeof(self) weakSelf = self;
[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
NSLog(@"old agent :%@", result);
NSString *userAgent = result;
NSString *newUserAgent = [userAgent stringByAppendingString:@" Appended Custom User Agent"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
strongSelf.webView = [[WKWebView alloc] initWithFrame:strongSelf.view.bounds];
// After this point the web view will use a custom appended user agent
[strongSelf.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
NSLog(@"new agent :%@", result);
}];
}];
以上這篇Android WebView userAgent 設(shè)置為桌面UA實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android 中 WebView 的基本用法詳解
- 詳解Android WebView監(jiān)聽console錯誤信息
- Android webView如何輸出自定義網(wǎng)頁
- 在Android環(huán)境下WebView中攔截所有請求并替換URL示例詳解
- 解決Android webview設(shè)置cookie和cookie丟失的問題
- Android中webView加載H5綁定cookie實例
- Android WebView通過動態(tài)的修改js去攔截post請求參數(shù)實例
- Android webview注入JS代碼 修改網(wǎng)頁內(nèi)容操作
- Android實現(xiàn)WebView點擊攔截跳轉(zhuǎn)原生
- Android WebView攔截iframe標(biāo)簽內(nèi)部跳轉(zhuǎn)教程
- Android中的webview監(jiān)聽每次URL變化實例
- Android 解決WebView多進程崩潰的方法
相關(guān)文章
Android App中實現(xiàn)相冊瀑布流展示的實例分享
這篇文章主要介紹了Android App中實現(xiàn)相冊瀑布流展示的實例分享,例子中利用到了緩存LruCache類的相關(guān)算法來解決大量加載問題,需要的朋友可以參考下2016-04-04
Android ImageButton自定義按鈕的按下效果的代碼實現(xiàn)方法分享
這篇文章主要介紹了Android ImageButton自定義按鈕的按下效果的代碼實現(xiàn)方法,需要的朋友可以參考下2014-02-02
總結(jié)Android App內(nèi)存優(yōu)化之圖片優(yōu)化
網(wǎng)上有很多大拿分享的關(guān)于Android性能優(yōu)化的文章,主要是通過各種工具分析,使用合理的技巧優(yōu)化APP的體驗,提升APP的流暢度,但關(guān)于內(nèi)存優(yōu)化的文章很少有看到。下面是我在實踐過程中使用的一些方法,很多都是不太成熟的項目,只是將其作為一種處理方式分享給大家。2016-08-08
實例解析Android系統(tǒng)中的ContentProvider組件用法
這篇文章主要介紹了Android系統(tǒng)中的ContentProvider組件用法,舉例講解了ContentProvider傳遞數(shù)據(jù)及監(jiān)聽ContentProvider數(shù)據(jù)改變的方法,十分詳細(xì),需要的朋友可以參考下2016-04-04
Android使用SwipeListView實現(xiàn)類似QQ的滑動刪除效果
這篇文章主要介紹了Android使用SwipeListView實現(xiàn)類似QQ的滑動刪除效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08

