0

I am trying to run the following code in my app (ref: iPhone Dev: UIWebView baseUrl to resources in Documents folder not App bundle)

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"<h1>Hello this is a test</h1><img src="sample.jpg" alt="" width="100" height="100" />";
[webView loadHTMLString:HTMLData baseURL:
[NSURL URLWithString: 
[NSString stringWithFormat:@"file:/%@//",imagePath]
]];

It works perfectly well in a separate project. But when I try it in my existing project, I get the following on the console, followed b a blank webView :

 *** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSRangeException> *** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]

Any ideas on where shall I look for. in my code?

Community
  • 1
  • 1
Amarsh
  • 11,214
  • 18
  • 53
  • 78

2 Answers2

1

Do this:

 NSString *path = [[NSBundle mainBundle] bundlePath];
 NSURL *baseURL = [NSURL fileURLWithPath:path];
 NSString *strHTML = @"<html><h1>Hello this is a test</h1><img src="sample.jpg" alt="" width="100" height="100" /></html>";
 [webView loadHTMLString:strHTML baseURL:baseURL];
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • 1
    This error occurred for me when Crittercism was enabled. Sending the bundlePath as the baseURL fixed the issue for me. – Paul Scott Sep 24 '13 at 00:40
-4

So sorry ... there was a problem in - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

My fault, was carried away with the weird webkit message

Amarsh
  • 11,214
  • 18
  • 53
  • 78
  • 9
    You should say what the problem was so that people who encounter this error in the future can have a hint on how to fix it. – Carl Veazey Sep 05 '12 at 05:47
  • Also, re-read the docs about NSString's path additions and NSURL - it's a very bad approach that you use for creting the URL. –  Sep 05 '12 at 05:52
  • As @CarlVeazey said... you should say what the problem was and how you solved it... i´m facing an issue like this right now... and this answer doesn´t help me at all. – VaroX Apr 04 '14 at 08:35