UIWebView shows error when webpage is loaded, Safari shows perfectly
The problem is that the website doesn't fully load in a UIWebView, but it loads normally in Safari.
This is the error I get when loading the website in a UIWebView:
Warning: cos_before_render(/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob) [function.cos-before-render]: failed to open stream: No such file or directory in /home/user/ctown/cti_bin/wbn/cos_init.inc on line 731
Warning: cos_before_render() [function.include]: Failed opening '/home/user/c开发者_如何学Pythontown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob' for inclusion (include_path='.:/home/user/ctown/cti_bin/phplot:/usr/local/lib/php') in /home/user/ctown/cti_bin/wbn/cos_init.inc on line 731
Fatal error: Call to undefined function: json_encode() in /home/user/ctown/cti_bin/wbn/cos_init.inc on line 737
- (void)viewDidLoad {
NSString *urlAddress = @"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
[super viewDidLoad];
}
I tried the question linked below, however I can't modify the backend of the website!
UIWebView Xhmtl parse error but safari don't
The error is not within your code! The UIWebView, especially from within the simulator uses a user agent something like this:
Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C134
When you call the url provided it results in the error you are seeing. It's a server side error.
You can alter your user agent by using this code, put it somewhere in the startup phase of your app so it's only set once:
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Safari/528.16", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];
With this I was able to open your page. It looks like the site returns different markup for the iphone, so the result with the code above will show the website, which is a bit large for the small display.
UIWebView and Safari does not have the same user agent.
This answer won't fix your issue (see Nick's answer), but you should put [super viewDidLoad];
before you do anything else. So:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = @"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
}
精彩评论