Send bundled PDF to iBooks from within app - objective-c

I'm using the following code attached to a button, to attempt to open a PDF file in iBooks, but nothing's happening when I click the button. The method's definitely being called.
- (IBAction)openDocs
{
NSURL *url = [NSURL fileURLWithPath:#"MS.pdf"];
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
I've declared the class itself as the relevant delegate, and since the documentation says there are no required delegate methods, that's all I've done. Nothing's happening when I tap the button, and I'm trying to figure out what I'm missing – any help is greatly appreciated!

If MS.pdf is in your bundle then you need to get the full path like so:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MS" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];

Related

Download ePub (NSURLConnection), save it, and open it with UIDocumentInteractionController

So I am trying to download an ePub book with NSURLConnection. So far, the download is working perfectly. The issue comes when I try to save it and open it. When I try to open it with UIDocumentInteractionController, the app crashes saying that the directory is null.
Here is my code:
//Download ePub file
NSURL *url = [NSURL URLWithString:#"http://www.terceiroanjo.com/materiais/missaopiloto.epub"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
then
// save it ("data" = NSMutableData that was appended from the received data of the download)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *filePath = [NSString stringWithFormat:#"%u/%#", NSDocumentDirectory,#"missaopiloto.epub"];
[data writeToFile:filePath atomically:YES];
}
and to open the ePub:
NSString *path = [[NSBundle mainBundle] pathForResource:#"missaopiloto" ofType:#"epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
When I try to open it, the app crashes:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
I get it, the directory is nill, but why? What am I doing wrong. Any ideas?
Thanks.
I think the problem here is that, once you download and save a file, it's not becoming part of the bundle.
If you want to access the file you must provide the local path and create the local URL
NSString *path = [NSString stringWithFormat:#"%u/%#", NSDocumentDirectory,#"missaopiloto.epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

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

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];

Return to my App after make a call

After my app make a call i would like to return to my app. When user hit "End" in call screen now he always see phone app. How to force app to return to my original app?
I make a call using:
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL]];
Try this.......
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", phoneNumber]
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
You can use the following code to get back to your app after making a call from your app
NSURL *url = [NSURL URLWithString:#"telprompt://phone number"];
[[UIApplication sharedApplication] openURL:url];
There's no way to do that without jailbreaking.

How can I load an HTML file?

How do I load an HTML file into my app (xcode)? I'm using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
}
When I start the app, I only see a white page. What's wrong?
.h file:
#import <UIKit/UIKit.h>
#interface poules : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) UIWebView *webView;
#end
Here is a working example from one of my projects. My index.html file is in a folder called Documentation/html in the resources directory. Its important to note that these are "folder references", not groups (hence the blue icon):
then to load it in a webView:
NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
NSArray *pathComponents = [NSArray arrayWithObjects:resourceDir, #"Documentation", #"html", #"index.html", nil];
NSURL *indexUrl = [NSURL fileURLWithPathComponents:pathComponents];
NSURLRequest *req = [NSURLRequest requestWithURL:indexUrl];
[webView loadRequest:req];
try loadHTMLString:baseURL: method
NSString *html=[NSString stringWithContentsOfFile:your_html_path encoding:NSUTF8StringEncoding error:nil];
[webview loadHTMLString:html baseURL:baseURL];
First thing you can check whether your webView is connected or not.
If it is then, you can break down your code to check what is wrong with request that you are trying to load.
1.. Create a NSString for the file path like this, and check if the path is returned or not.
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
2.. Create a NSURL from the above string like this, and check if the url is correct or nil.
NSURL *url = [NSURL URLFromString:urlString];
3.. And then create the request.
NSURLRequest *request = [NSURLRequest requestFromURL:url];

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];
}