开发者

Difference between the UIWebView methods loadHTMLString: and loadRequest

开发者 https://www.devze.com 2023-02-25 09:45 出处:网络
I have a UIWebView and I want to load an SVG image into it. The contents of the file is pure SVG i.e. <svg>...</svg>. The file loads fine into normal and Mobile Safari, and also in a UIWeb

I have a UIWebView and I want to load an SVG image into it. The contents of the file is pure SVG i.e. <svg>...</svg>. The file loads fine into normal and Mobile Safari, and also in a UIWebView using loadRequest: by doing the following:

url = [NSURL f开发者_Go百科ileURLWithPath:path];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];

However, if I load the contents of the file into a string, and try to load the HTML string into the webview, nothing displays:

html = [NSString stringWithContentsOfFile:path
                                encoding:NSASCIIStringEncoding
                                   error:nil];
[webView loadHTMLString:html baseURL:nil];

Is there any reason between the above two techniques? Should they not give the same results? The file is pure ASCII, so I don't think there is an encoding issue.

I guess I can get what I need done right now by using a file, but I hate to use the filesystem for non persistent data.

Any help is greatly appreciated!!!

Thanks, Ron


To solve this issue you have to use loadData:MIMEType:textEncodingName:baseURL: instead.

NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
[webView loadData:data MIMEType:@"application/xhtml+xml" textEncodingName:@"utf-8" baseURL:baseUrl];


I am not sure about this but if the baseURL is set to nil, I think the webview does not know from where to load the contents.

Hope this SO question helps u Link to resources inside WebView - iPhone

0

精彩评论

暂无评论...
验证码 换一张
取 消