1

I have a UIWebView which is adapted to a mobile style of a forum I have. In Safari, it looks and works great.

However, inside my UIWebView there is a major problem.

When quoting or editing a post, HTML is displayed inside of BBcode. Furthermore, posting code results in ignored line breaks. This problem will render my app completely unusable.

The webview is loaded like this:

  //display the webview
    NSString *fullURL = @"http://www.mysite.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
    [_webTest loadRequest:requestObj];

This is how it looks inside of mobile Safari (correctly)

enter image description here

And finally, how it looks inside of UIWebView (incorrectly)

enter image description here

What could possible be changing the way it renders between mobile safari vs UIWebView? I thought the point was their supposed to look the same!

john cs
  • 2,220
  • 5
  • 32
  • 50

1 Answers1

2

This is because mobile safari and a webview have different user agent values.
Check out this SO post which is about a similar issue: Change User Agent in UIWebView (iPhone SDK)

Code from accepted solution:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType {
    NSMutableURLRequest *request = (NSMutableURLRequest *)req;

    if ([request respondsToSelector:@selector(setValue:forHTTPHeaderField:)]) {
        [request setValue:[NSString stringWithFormat:@"%@ Safari/528.16", [request valueForHTTPHeaderField:@"User-Agent"]] forHTTPHeaderField:@"User_Agent"];
    }
    return YES; 
}
Community
  • 1
  • 1
propstm
  • 3,461
  • 5
  • 28
  • 41
  • i changed the user agent but unfortunately the output is still the same. – john cs Jan 14 '13 at 21:03
  • try checking your webview and mobile safari against http://whatsmyuseragent.com/. The code I'd posted may be slightly out of date. – propstm Jan 14 '13 at 21:04
  • actually, the code doesnt seem to be changing my user agent but i will look into this, thanks for the tip – john cs Jan 14 '13 at 21:04
  • Thank you propstrm - The instructions here got it working to future people: http://www.mphweb.com/en/blog/easily-set-user-agent-uiwebview – john cs Jan 14 '13 at 21:19