Loading a file programmatically into iBooks - objective-c

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.

Related

Images in my html file in UIWebView not showing

Here is my scenario:
I have a folder called "HTML" in my bundle. This folder has an HTML file and 5 images. The HTML file references all the images at some point using a simple img tag.
<img src="tut_navigation.png" />
Now I have a UIWebview which I used to load the HTML file. All of the contents of the HTML file are rendered correctly except for my images. My images are not showing up for some strange reason. Then I did some research and found related posts saying to load URL like so:
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tutorial" ofType:#"html" inDirectory:#"HTML"]];
NSString* htmlString = [NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
Once I made this change and ran it again, this time only the first image is loading up, the others ones are not. I'm lost as to what the issue is. When I view this HTML file in a web browser, everything is working fine. Any ideas?
The baseURL needs to be the a path to the HTML folder inside the app bundle, not (as you have it) a path to the app bundle itself.
Change this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
To this:
[self.webView loadHTMLString:htmlString baseURL:url];
You had the right URL for the base URL (url) and then you just threw it away and substituted the URL of the bundle itself, which is wrong.
Even better, forget the base URL and just use a URL instead of a path string for the file itself:
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tutorial" ofType:#"html" inDirectory:#"HTML"]];
NSURLRequest* req = [[NSURLRequest alloc] initWithURL:url];
[self.webview loadRequest: req];

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.

Images & CSS not loading in iPhone UIWebView

When my UIWebView loads my index.html the CSS and JavaScript references show as broken. What is wrong with the code below? (I verified that appUrlString is pointing to the correct directory)
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#""];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
NSString *appUrlString = [NSString stringWithFormat:#"%#/web/", [[NSBundle mainBundle] resourcePath]];
NSURL *appUrl = [NSURL URLWithString:appUrlString];
[_appWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:appUrl];
This is what shows in the simulator.
You should use
NSURL *appUrl = [NSURL fileURLWithPath:appUrlString];
because it's a local filePath. In your version 'appURL' was giving back nil.
So no baseurl got set.

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

Send bundled PDF to iBooks from within app

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