Keisuke Uemura
Keisuke Uemura

Reputation: 111

iPad Pro "HTTP_USER_AGENT" of iOS UIWebView

I tested my existing iOS app on the new iPad Pro. And I found out that HTTP_USER_AGENT text of UIWebview is ...

Mozilla/5.0 (iPhone; CPU iPhone OS9_1 like Mac OS X) AppleWebKit/601.1.46(KHTML, like Gecko)Mobile/13B143 

So,web side program react as if this device is iPhone. How can I distinguish between iPad Pro and iPhone in my server side program?

Upvotes: 6

Views: 3751

Answers (2)

Matias
Matias

Reputation: 551

I had the same problem, and resolved it using WKWebView instead of UIWebView. This new implementation for showing web content is the one recommended by Apple, as it seems that UIWebView is going to be deprecated.

One thing about this new object, is that the method for evaluating a JavaScript is asynchronous. Therefore, if you want to use a synchronous method to fetch the userAgent, you should create your own category of WKWebView as explained here.

The code should be something like this

@interface WKWebView(SynchronousEvaluateJavaScript)
- (NSString *)stringByEvaluatingJavaScript:(NSString *)script;
@end

@implementation WKWebView(SynchronousEvaluateJavaScript)

- (NSString *)stringByEvaluatingJavaScript:(NSString *)script
{
    __block NSString *resultString = nil;

    [self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
        if (error == nil) {
            if (result != nil) {
                resultString = [NSString stringWithFormat:@"%@", result];
            }
        } else {
            NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
        }
    }];

    while (resultString == nil)
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

    return resultString;
}

@end

And the invocation is quite the same than before:

WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero];
defaultUserAgent = [webView stringByEvaluatingJavaScript:@"navigator.userAgent"];

Upvotes: 0

Keisuke Uemura
Keisuke Uemura

Reputation: 111

By adding Launch Screen.stoyboard, this problem will be solved.

Upvotes: 2

Related Questions