开发者

How to troubleshoot iOS UIWebView DOM Exception 18?

My team of developers has built a successful iOS universal app w开发者_如何学Gohich synchronizes sophisticated HTML5 web apps to an iPhone / iPad. The apps are loaded into the UIWebView from the local file system. We recently added in-app email using MessageUI.framework.

Everything was going fine until our QA department identified a use case where the instantiation of the MailCompose class causes the UIWebView to throw a SECURITY_ERR: DOM Exception 18 error. This ONLY occurs on iOS 4.2.1. The app functions normally when built against SDK 4.1 and installed on a device running iOS 4.1. This FEELS like either an iOS 4.2 bug, or an HTML5 issue that has appeared in iOS 4.2.

I am looking for techniques to find the cause of the SECURITY_ERR: DOM Exception 18. I know that this error is a same-origin exception of some kind, but it doesn't make sense why it works on 4.1 but not 4.2. And why does the MailCompose class cause the issue?

Any help at all will be greatly appreciated!


may I ask how do you load your html source from the local FS ? Also what does your MailCompose class do, to interact with the document loaded into the UIWebView ?

If you are reading the contents of an html file into a string and then load it into the UIWebView, then you have to change it, and load it using the file:// protocol.

Instead of loading the html source as a string :

NSString *html = [NSString stringWithContentsOfFile:[path stringByAppendingString:@"path to the html file"] encoding:NSUTF8StringEncoding error:NULL]; 
[webView loadHTMLString:html baseURL:baseURL];

Go ahead and load the actual html file via an NSURLRequest (file:// url)

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:[path stringByAppendingString:@"path and file name of the file"]]; 
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL]; 
[webView loadRequest:request];

Doing so, enables access to window.localStorage, window.openDatabase and other objects.

Cheers

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜