Objective-c/iOS - Open local html file with Safari - objective-c

I have an HTML file save in a temporary directory like that :
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentDirectory = NSTemporaryDirectory();
NSString *documentPath = [documentDirectory stringByAppendingPathComponent:#"mydocument.html"];
[fileManager createFileAtPath:documentPath contents:myHTMLDocumentData attributes:nil];
The document is created in my temporary file. After it, I want to open this document in Safari but it doesn't work :
NSURL *url = [NSURL fileURLWithPath:documentPath];
[[UIApplication sharedApplication] openURL:url];
Nothing happen at screen, no error...
However, if I replace "url" by #"http://google.fr", Safari is launched with google.fr and I can access to my temporary file by typing the url "file://localhost..../myHtmlDocument.html" in Safari.
Hope you can help me

You cannot open a document with resides in your app bundle through Safari (please, see iOS Security Model).
What you need is using an UIWebView to display your document content inside your app using – loadHTMLString:baseURL:; e.g.:
UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];

i dont think you can achieve that, since both UIApplication openURL: and UIDocumentInteractionController will not open local files inside your bundle
Do the following instead, in your viewDidLoad
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath: documentPath]];
[webView loadRequest:request];

Related

OS X Web View App - Won't Load Local .html Web Page

I have the following code in an AppDelegate.m file that opens Google when I open my app. I am trying to change it to open a .HTML web page I copied into my Xcode project but it is not working. I am using Xcode 4.5.2.
The code that works to open Google is:
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]];
[self.webView.mainFrame loadRequest:request];
}
I tried changing it to (so that it would load my test.html file I put in the project folder but the app just comes up blank now). What am I missing?
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"test.html"]];
[self.webView.mainFrame loadRequest:request];
}
As long as your test.html is copied at the root of your project (make sure it's also copied to your final app bundle, huh? - check Build Phases) you can load it up your Web View using this code :
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];
[NSURL URLWithString:#"test.html"] is not creating a valid URL to your file. Try that: (it is from an iOS project, but should apply to OS X apps as well). I'm using loadHTMLString:baseURL: instead of loadRequest:
NSString* fileName = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:NULL];
NSURL *baseURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
[NSURL URLWithString:#"test.html"]
There's your problem. Assuming that your test.html file is in the resource folder of your application bundle, do this instead:
[[NSBundle mainBundle] URLForResource:#"test" withExtension:#"html"];
NSBundle has a few other methods for easily creating URLs in case the one above isn't appropriate (like if the file is in a subdirectory). Take a look at the NSBundle reference page for more info.

Loading a file programmatically into iBooks

I'm not sure why this code isn't working. Any ideas? iBooks opens, but nothing is presented.
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ibooks"];
NSString *stringURL = [NSString stringWithFormat:#"itms-books:%#", fileToOpen];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
The itms-books: URL scheme is for links to the iBookstore. If you have a PDF or ePub file you want to allow the user to open in iBooks, look into QLPreviewController or UIDocumentInteractionController.

How to store a pdf file loaded into an UIWebView in my Documents directory?

Currently, I detect if the UIWebView load a pdf file by doing a check on the current URL. Next I download the pdf file with ASIHTTPRequest library. The problem is that if the file is display in the UIWebView, is that it is already downloaded somewhere, so I download this file twice. How can I get this file load in my UIWebView ?
The purpose is to store this file loaded in my UIWebView in my Document directory.
Here's how you can download, read and store your pdf locally in iphone application, so that you don't have to download regularly:
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ // if file not present
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you simply want to read/load pdf from your Resource folder then simply do this :
NSString* filePath= [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"]];
/// or you can even read docs file as : pathForResource:#"sample" ofType:#"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
All I can suggest is either to download it a second time, or put it in the temporal storage and then put it it the UIWebView and when the user asks, then put it where you want from the temporal storage.

How can i detect Phone number and Hyperlink from Pdf file?

I am displaying my pdf file in UIWebView now i want to detect links and Phone number which is available in my pdf and by pressing link it should open in browser and touching on number it should be called on that particular number so please anybody have idea about it.Give me some suggestion on it.I have written this code for achieving this task but it do not work:
webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 764, 1004)];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Gita.pdf"];
NSLog(#"FilePath==>%#",filePath);
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSLog(#"url==>%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView setDataDetectorTypes: UIDataDetectorTypeAll];
[webView loadRequest:requestObj];
check this link:
http://bill.dudney.net/roller/objc/entry/tiledlayer_on_the_iphone and http://bill.dudney.net/roller/objc/entry/catiledlayer_example.
Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

reading excel sheet with url in Iphone sdk

I am Having a problem here.I am downloading the excel sheet using Url and have to displaying it in the web view here my problem is the Excel sheet is not displaying.
I written the code as:
NSString *str = #"";
str = [str stringByAppendingString:
#"http://databases.about.com/library/samples/address.xls"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[webView loadData:data MIMEType:#"application/xls" textEncodingName:#"UTF-8"
baseURL:[NSURL URLWithString:#"http://google.com"]];
[contentView addSubview:webView];
Thank you,
Monish.
Are you able to view it using Safari on the phone or does it say "unsupported format"?
Try another file that works in Safari and then try in UIWebView.
Also see this: http://developer.apple.com/iphone/library/qa/qa2008/qa1630.html
First make sure you are able to view the xls in Safari then try code like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.yourwebsite.com/good.xls"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}