UIWebview xml文件 [英] UIWebview xml file

查看:127
本文介绍了UIWebview xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的uiwebview中显示一个xml文件。我尝试过使用NSURLRequest,但UIWebview显示为空白。我可以使用下面的代码显示本地xml。有没有办法从定义的网站而不是从本地文件中提取或者下载文件然后显示它?

  NSString * htmlFile = [[NSBundle mainBundle] pathForResource:@locationofType:@xml]; 
NSData * htmlData = [NSData dataWithContentsOfFile:htmlFile];
[aWebView loadData:htmlData MIMEType:@text / texttextEncodingName:@UTF-8baseURL:[NSURL URLWithString:@]];


解决方案

设置 UIWebView 委托和定义 webView:didFailLoadWithError: - webViewDidFinishLoad: ;如果你在那里放一些 NSLog 的跟踪(或者用调试器闯入它们),你可能会理解发生了什么。



原则上,如果您可以显示本地文件,则可以显示来自远程服务器的相同信息。所以我想也许你的服务器没有正确回复( didFail:将帮助你理解)。



编辑:



服务器发送mimetype,我不知道在 UIWebView b
$ b

您可以通过以下方式轻松下载文件:

  NSData * urlData = [NSData dataWithContentsOfURL:url]; 

然后您可以将数据传递到Web视图中。



考虑到 dataWithContentsOfURL:是同步的,所以它会暂停你的UI(但是对于测试来说没关系)。如果一切运行良好,那么你可以在后台线程上运行它,如下所示:

  dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {

NSData * htmlData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(),^ {

[aWebView loadData: htmlData MIMEType:@text / texttextEncodingName:@UTF-8baseURL:[NSURL URLWithString:@]];

}
}

注意完成XML下载后如何返回 UIWebView 通过主线程。


I've been trying to display a xml file in my uiwebview. I've tried using NSURLRequest but the UIWebview shows up blank. I am able to display a local xml using the code below. Is there a way pull this from a defined website rather than from local file or maybe download the file then display it?

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"location" ofType:@"xml"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[aWebView loadData:htmlData MIMEType:@"text/text" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

解决方案

Set your UIWebView delegate and define webView:didFailLoadWithError: and – webViewDidFinishLoad:; if you put some NSLog traces in there (or break into them with the debugger), you might understand what is happening.

In principle, if you can display a local file, you can display the same info coming from a remote server. So I am thinking that maybe your server is not replying correctly (and didFail: will help you understanding that).

EDIT:

The mimetype is sent by the server, and I am not aware of a way to modify it before the UIWebView gets to handle it.

You can easily download a file by way of:

NSData *urlData = [NSData dataWithContentsOfURL:url];

and then you can pass the data into the web view as you are already doing.

Take into account that dataWithContentsOfURL: is sync, so it will halt your UI (but for a test it is ok). If everything works out well, then you can run it on a background thread, that would be something like:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    NSData *htmlData = [NSData dataWithContentsOfURL:url];
    dispatch_async(dispatch_get_main_queue(), ^{

       [aWebView loadData:htmlData MIMEType:@"text/text" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

    }
}

Notice how getting back to the UIWebView once the XML has been downloaded is done through the main thread.

这篇关于UIWebview xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆