Reputation: 5480
I have a piece of code that had set the user agent for all of my web views:
dispatch_once(&onceToken, ^{
{
__block WKWebView *webView = [[WKWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds] configuration:[WKWebViewConfiguration new]];
[webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if(nil != result && [result isKindOfClass:[NSString class]]) {
NSString *userAgent = result;
if( [userAgent rangeOfString:@"My Special User Agent"].length == 0 )
{
userAgent = [userAgent stringByAppendingString:@" My Special User Agent"];
NSDictionary * dictionary = @{@"UserAgent": userAgent};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.125 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
webView = nil;
});
}];
}
});
However, when I tested it on an iPad Pro (12.9 inch) - 13.2.2, I found that the user agent I am getting is wrong and doesn't contain my special user agent in most cases. Interestingly enough, if I make the web view small enough (about 20% of the screen) it does work.
I found a workaround to set the user agent whenever I create the web view:
_webView.customUserAgent = [[NSUserDefaults standardUserDefaults] stringForKey:@"UserAgent"];
Is this a safe solution (could I be losing some important information in the user agent)? Is there a better solution?
Upvotes: 2
Views: 1821
Reputation: 62
Apple removed Ipad from the userAgent in ios13 by default because
Settings -> Safari -> Request Desktop Website -> All websites. That option is enabled by default.
Upvotes: 1