问题
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:@""]];
回答1:
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.
回答2:
@sergio method works. Another way to achieve this is to download the file, store it locally then load.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];
NSURL *url = [NSURL URLWithString:@"http://yourlocation.com/file.xml"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
[aWebView loadData:urlData MIMEType:@"text/text" textEncodingName:@"UTF-8" baseURL:nil];
回答3:
Please use below code for load local xml file from document directory in uiwebview
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"demo.xml"];
NSString *content = [NSString stringWithContentsOfFile:dataPath encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"contenct %@",content);
[webView loadHTMLString:[NSString stringWithFormat:@"<html><body>%@</body></html>",content] baseURL:nil];
来源:https://stackoverflow.com/questions/17791317/uiwebview-xml-file