UIWebview xml文件 [英] UIWebview xml file
问题描述
我一直在尝试在我的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,我不知道在 您可以通过以下方式轻松下载文件: 然后您可以将数据传递到Web视图中。 考虑到 注意完成XML下载后如何返回 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?
Set your 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 EDIT: The mimetype is sent by the server, and I am not aware of a way to modify it before the You can easily download a file by way of: and then you can pass the data into the web view as you are already doing. Take into account that Notice how getting back to the 这篇关于UIWebview xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! UIWebView $ c之前修改它的方法$ c> b
$ b
NSData * urlData = [NSData dataWithContentsOfURL:url];
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:@]];
}
}
UIWebView
通过主线程。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:@""]];
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.didFail:
will help you understanding that).UIWebView
gets to handle it.NSData *urlData = [NSData dataWithContentsOfURL:url];
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:@""]];
}
}
UIWebView
once the XML has been downloaded is done through the main thread.