When loading URLs for UIWebView, I usually do it like this:
 
1. I get the path for my resource, then pass this to other functions.

2. Create the NSURL using the html path.

3. Create the NSURLRequest which will be passed to our UIWebView.

4. Load NSURLRequest to our UIWebView.

 
Running this in the device will be okay. The HTML will be loaded successfully.
But running this in the iOS simulator will fail loading the HTML. This is because [NSURL URLWithString:] must take in a string that conforms to RFC 2396.

 
When running in a device, the htmlPath variable above will contain something like the following:

The htmlPath variable does not contain any spaces, which will make [NSURL URLWithString:] return a non-nil object.
 
When running in the iOS Simulator, the htmlPath variable above will contain something like the following:

The path above contains spaces, which will make [NSURL URLWithString:] return nil.

  
To address this problem, we will use another API to create the NSURL, once which is specifically for system paths, [NSURL fileURLWithPath:].
We will replace #2 with the following line:

Better yet, we will specify that the specified path is a file, and not a directory. This will save some i/o, according to Apple’s documentation in the NSURL header.

 
That’s it!
 
References:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

Happy Goals for iOS

Happy Goals for iOS

Want an app to help you track and achieve your goals, without the complex stuff, saving you from headaches? Checkout Happy Goals - Habits & Goals Tracker now in the App Store! Happy Goals has been featured in the App Store in over 32 countries. Start now, better late than never!

 

Thank you!

%d bloggers like this: