UIWebView在网页加载时显示错误,Safari显示完美

发布于 2024-10-24 22:11:27 字数 1170 浏览 1 评论 0原文

问题是网站在 UIWebView 中无法完全加载,但在 Safari 中可以正常加载。

这是我在 UIWebView 中加载网站时遇到的错误:

警告: cos_before_render(/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob) [function.cos-before-render]:失败 打开流:没有这样的文件或 目录在 /home/user/ctown/cti_bin/wbn/cos_init.inc 在第 731 行

警告:cos_before_render() [function.include]:打开失败 '/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob' 纳入 (include_path='.:/home/user/ctown/cti_bin/phplot:/usr/local/lib/php') 在 /home/user/ctown/cti_bin/wbn/cos_init.inc 在线 731

致命错误:调用未定义 函数: json_encode() 中 /home/user/ctown/cti_bin/wbn/cos_init.inc 在线 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];
}

我尝试了下面链接的问题,但是我无法修改网站的后端!

UIWebView Xhmtl 解析错误,但 safari 没有

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/ctown/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2024-10-31 22:11:27

错误不在您的代码中! UIWebView,特别是在模拟器中,使用如下用户代理:

Mozilla/5.0(iPhone Simulator;U;CPU iPhone OS 4_2,如 Mac OS X;en-us)AppleWebKit/533.17.9(KHTML,如 Gecko)Mobile /8C134

当您调用该 url 时,它会导致您看到的错误。这是服务器端错误。

您可以使用此代码更改您的用户代理,将其放在应用程序启动阶段的某个位置,这样它只设置一次:

NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Safari/528.16", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];

有了这个,我就可以打开您的页面。看起来该网站为 iPhone 返回了不同的标记,因此上面代码的结果将显示该网站,对于小显示屏来说有点大。

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.

小清晰的声音 2024-10-31 22:11:27

UIWebView 和 Safari 没有相同的用户代理。

UIWebView and Safari does not have the same user agent.

囚我心虐我身 2024-10-31 22:11:27

这个答案不会解决您的问题(请参阅尼克的答案),但您应该在执行其他操作之前放置 [super viewDidLoad]; 。所以:

- (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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文