UIWebView沒有提供設(shè)置UserAgent的接口,但是有一個辦法可以間接的設(shè)置。
NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:value, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
通過設(shè)置NSUserDefaults中UserAgent的值來修改,但是這種設(shè)置方法有一個限制,需要在UIWebView的loadRequest之前調(diào)用才能生效(加載PDF比較特殊)。這是Cordova源碼中關(guān)于這個問題的描述
Setting the UserAgent must occur before a UIWebView is instantiated.
It is read per instantiation, so it does not affect previously created views.
Except! When a PDF is loaded, all currently active UIWebViews reload their
User-Agent from the NSUserDefaults some time after the DidFinishLoad of the PDF bah!
CDVUserAgentUtil
在多WebView的情況下,如果每個WebView都有不同的UserAgent,就會產(chǎn)生數(shù)據(jù)競爭的問題,大家都要修改NSUserDefaults中UserAgent的值,于是需要對資源加鎖來保證每個WebView都設(shè)置預(yù)期的UserAgent。在Cordova中,專門有一個類CDVUserAgentUtil來實現(xiàn)這個功能。
CDVUserAgentUtil.h文件中定義了四個方法
// 獲取UIWebView默認(rèn)的UserAgent
+ (NSString*)originalUserAgent;
// 獲取鎖
+ (void)acquireLock:(void (^)(NSInteger lockToken))block;
// 釋放鎖
+ (void)releaseLock:(NSInteger*)lockToken;
// 設(shè)置UIWebView的UserAgent
+ (void)setUserAgent:(NSString*)value lockToken:(NSInteger)lockToken;
加鎖
每次加鎖成功會返回一個NSInteger類型的token,在釋放鎖的時候需要把token傳入。token會不斷遞增,保證每次加鎖返回的token都不回重復(fù)。加鎖的實現(xiàn)代碼如下:
// CDVUserAgentUtil.m
+ (void)acquireLock:(void (^)(NSInteger lockToken))block
{
if (gCurrentLockToken == 0) {
gCurrentLockToken = ++gNextLockToken;
VerboseLog(@"Gave lock %d", gCurrentLockToken);
block(gCurrentLockToken);
} else {
if (gPendingSetUserAgentBlocks == nil) {
gPendingSetUserAgentBlocks = [[NSMutableArray alloc] initWithCapacity:4];
}
VerboseLog(@"Waiting for lock");
[gPendingSetUserAgentBlocks addObject:block];
}
}
調(diào)用acquireLock:,首先會判斷gCurrentLockToken是否等于0
- 如果是0說明沒有模塊正在修改
UserAgent,能夠成功獲取到鎖,gCurrentLockToken遞增,標(biāo)致當(dāng)前有模塊正在修改UserAgent,并回調(diào)block,返回gCurrentLockToken - 如果不為0說明當(dāng)前有模塊正在修改
UserAgent,將block回調(diào)存在一個隊列gPendingSetUserAgentBlocks中
釋放鎖
釋放鎖需要傳入token,釋放鎖代碼如下:
+ (void)releaseLock:(NSInteger*)lockToken
{
if (*lockToken == 0) {
return;
}
NSAssert(gCurrentLockToken == *lockToken, @"Got token %ld, expected %ld", (long)*lockToken, (long)gCurrentLockToken);
VerboseLog(@"Released lock %d", *lockToken);
if ([gPendingSetUserAgentBlocks count] > 0) {
void (^block)() = [gPendingSetUserAgentBlocks objectAtIndex:0];
[gPendingSetUserAgentBlocks removeObjectAtIndex:0];
gCurrentLockToken = ++gNextLockToken;
NSLog(@"Gave lock %ld", (long)gCurrentLockToken);
block(gCurrentLockToken);
} else {
gCurrentLockToken = 0;
}
*lockToken = 0;
}
- 如果要釋放的
lockToken為0,說明還沒加過鎖,就調(diào)用釋放了,直接返回 - 從隊列
gPendingSetUserAgentBlocks中取出最早加入的block,從隊列中移除 -
gCurrentLockToken遞增生成新token,回調(diào)block - 如果隊列
gPendingSetUserAgentBlocks釋放完成,說明釋放鎖的調(diào)用次數(shù)>加鎖的次數(shù),不做操作,然后把gCurrentLockToken置為0
設(shè)置UserAgent
在Cordova實際運(yùn)用中,操作鎖的時機(jī):
加鎖時機(jī):CDVViewController加載完畢,在viewDidLoad里調(diào)用
釋放鎖時機(jī):
-
UIWebView的webViewDidFinishLoad:回調(diào) -
UIWebView的webView:didFailLoadWithError:回調(diào) -
CDVViewController的dealloc -
CDVViewController的viewDidUnload
加鎖代碼,省略了不相關(guān)代碼
// CDVViewController.m
- (void)viewDidLoad
{
[CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
_userAgentLockToken = lockToken;
[CDVUserAgentUtil setUserAgent:self.userAgent lockToken:lockToken];
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[self.webViewEngine loadRequest:appReq];
}];
}
釋放鎖代碼,這里只看正常邏輯,在網(wǎng)頁加載完成回調(diào)webViewDidFinishLoad:中釋放邏輯。不考慮異常情況,省略了不相關(guān)代碼。
// CDVUIWebViewNavigationDelegate.m
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
NSLog(@"Finished load of: %@", theWebView.request.URL);
CDVViewController* vc = (CDVViewController*)self.enginePlugin.viewController;
// It's safe to release the lock even if this is just a sub-frame that's finished loading.
[CDVUserAgentUtil releaseLock:vc.userAgentLockToken];
}
在webViewDidFinishLoad:回調(diào)時,UserAgent已經(jīng)設(shè)置成功,所以可以釋放鎖,讓其它WebView操作UserDefault了
歡迎關(guān)注我的博客