I am having some set of html files and want to switch between them in a webView.
When i try to load any html then it gives me some thing like this
Load error,The opertaion couldn't be completed.(NSURLErrorDomain error -999.)
How would i fix this issue.
for loading htm i am using the lines of code
self.playerPath = [[BundleUtils bundleDirectoryFor:nil] stringByAppendingPathComponent:@"Browser Player files/HTMLplayer/RENE001NL_p001_G02_20_meeleesboek_XCode_pagetr_test_Newgen/"];
NSStrin开发者_如何学Gog* indexFilePath = [self.playerPath stringByAppendingPathComponent:@"index001.html"];
NSURL* indexUrl = [NSURL fileURLWithPath:indexFilePath];
[self.browserView loadRequest:[NSURLRequest requestWithURL:indexUrl]];
+ (NSString *) bundleDirectoryFor:(NSString *) bundleName
{
NSString* bundlePath = nil;
if (bundleName) {
NSString* basePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.bundle", bundleName]];
NSBundle* bundle = [NSBundle bundleWithPath:basePath];
bundlePath = [bundle resourcePath];
} else {
bundlePath = [[NSBundle mainBundle] resourcePath];
}
return bundlePath;
}
EDIT:
would you try and escape your path before creating the NSURL?
....
NSString* encodedParam = [indexFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* indexUrl = [NSURL fileURLWithPath:encodedParam];
....
EDIT 2:
You can inspect the content of your app binary by revealing it in the finder (right click on the product tab in Xcode) and then selecting "show package content". You will see exactly where your html is located and will be able to adjust the value you pass in.
OLD ANSWER:
I see two issues with your code:
you are using
loadRequest
, which is fine to retrieve content from the network, but (I guess) not fine to load a local file;use of a full path (
Browser Player files/HTMLplayer/RENE001NL_p001_G02_20_meeleesboek_XCode_pagetr_test_Newgen/
) while an iphone app binary structure is a flat directory (i.e., all the different folders you might have in Xcode are flattened out to a single directory).
I would suggest trying with this code:
NSString* basePath = [[NSBundle mainBundle] bundlePath];
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"index001" ofType:@"html"];
NSData* myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
[_webView loadData:myData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL fileURLWithPath:basePath]];
....
The error seems like you have cancelled a ongoing load request.
NSURLErrorCancelled
Returned when an asynchronous load is canceled.
A Web Kit framework delegate will receive this error when it performs a cancel operation
on a loading resource. Note that an NSURLConnection or NSURLDownload delegate
will not receive this error if the download is canceled.
Available in Mac OS X v10.2 and later.
Declared in NSURLError.h.
Probably the way either your html is referencing to another link or webview delegate is cancelling the load operation. Hard to guess why this has occured.
精彩评论