Reputation:
I have saved an html page using the following line of code :
NSURL *yoyoyo = [[NSURL alloc] initWithString:@"www.google.com"];
NSData *data = [[NSData alloc] initWithContentsOfURL:yoyoyo];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *documentsDirectory = @"/Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/";
NSLog(@"%@", documentsDirectory);
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:@"file.html"];
[html writeToFile:htmlFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
The file named as gets saved as "file.html" folder in the path mentioned i.e. documentsDirectory. Is there a way that I can open this file in safari or webView of my application ?? Thanks and Regards.
Upvotes: 0
Views: 1844
Reputation: 49
Try this code This may be helpful for you.
NSString *htmlFile;
htmlFile = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
Where abc.html is your html file in application.
Upvotes: 0
Reputation: 21221
to load it in your web view, do the following
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:@"file.html"];
NSString *htmlContents = [NSString stringWithContentsOfFile:htmlFilePath
encoding:NSUTF8StringEncoding
error:NULL];
[webView loadHTMLString:htmlContents baseURL:nil];
You also have an issue in getting documentDir
Instead of
NSString *documentsDirectory = @"/Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/";
use
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
Save the file again and try to load using the above code
Upvotes: 1
Reputation: 89509
It can be as simple as:
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: htmlFilePath];
which would open your saved HTML file in Safari.
If your app has it's own webview, you can create a URLRequest and pass that along to your webview's "loadRequest:
" method.
Upvotes: 0