I have a iBook file (say SampleiBook.iba) within the app bundle (not in documents directory).
Now i just want to open SampleiBook from within app. I tried this,
NSString *path = [[NSBundle mainBundle] pathForResource:#"SampleiBook" ofType:#".iba"];
NSString *stringURL = [#"ibooks://" stringByAppendingPathComponent:path];
NSURL *url = [NSURL URLWithString:stringURL];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
NSLog(#"Yes");
}
[[UIApplication sharedApplication] openURL:url];
the iBook application gets opened, showing only the files within the iBook application. But i want the one in app bundle to be opened.
Please guide me in achieving this.
It appears that you are trying to load the .iba (iBooks Author Book) file from your app. This not going to work because .iba is the file that contains your source for the .ibooks file type that I think you really want to load.
I am writing in swift these days. Here is a swift example I just ran. Assumes that the unbutton is wired from a nib or storyboard
#IBOutlet weak var iBooksBtn: UIButton!
#IBAction func iBooksBtnPressed(sender: AnyObject)
{
let ibookPath = NSBundle.mainBundle().pathForResource("Cupertino",
ofType: "ibooks")!
let url = NSURL(fileURLWithPath: ibookPath)
let interactionController = UIDocumentInteractionController(URL:url!)
interactionController.presentOptionsMenuFromRect(CGRectZero,
inView:self.iBooksBtn, animated:false)
}
You will have to use the UIDocumentInteractionController to open the iBook in iBooks.
You can easily create an instance for the iBook in mian bundle:
NSURL *iBookResourceURL = [[NSBundle mainBundle] URLForResource:#"SampleiBook" withExtension:#"iba"];
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: iBookResourceURL];
Related
I want to open specific file in iBooks App. I'm using UIDocumentsIntractionController to send this file to iBooks App. My code:
class ViewController : UIViewController {
...
...
#IBOutlet var iBooksBtn: UIButton!
#IBAction func book(sender: AnyObject) {
let ibookPath = NSBundle.mainBundle().pathForResource("test1",ofType: "ibooks")!
interactionController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath:ibookPath))
interactionController.UTI = "com.apple.ibooks"
interactionController.presentOpenInMenuFromRect(CGRectZero,inView:self.iBooksBtn, animated:false)
}
...
...
}
So, for now i store this file in mainBundle, but in future i want to download it from the Internet. So when i press button, i got few results to
choose:
But i want to open this file ONLY with iBooks and without requests to choose App (as on picture). As far as i know i need to use UTI for *.ibooks file, i guess UTI, that i used is wrong. Thanks for any help. Also custom URL scheme (ibooks:// and etc.) is not working for me.
To open iBooks you should use custom URL scheme:
To open the library:
NSString *stringURL = #"ibooks://";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
To open the iBooks store at a particular book page, you have to add the complete URL to that particular book:
NSString *stringURL = #"itms-books://itunes.apple.com/de/book/marchen/id436945766";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
NSString *stringURL = #"itms-bookss://itunes.apple.com/de/book/marchen/id436945766";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
I am trying to open a PDF file that I have stored locally within my app in iBooks. The app currently has a list of "literature" in a table view and when each cell is tapped the app segues to a webView that displays the PDF file (works great). I have made a BarButton at the top right in navBar to allow the user to open the PDF in iBooks (so they can store it on his/her device).
So far the button will open a UIDocumentInteractionController that displays all apps on the device that can open the file (after checking if iBooks is installed). Then when I click the iBooks icon the app crashes. I was able to revise the code so that iBooks opens without crashing, but the PDF file is not carried through so it's kind of pointless (code below is reverted back to when it crashes).
Code below is inside the IBAction of the barButton...
NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"itms-bookss:"]])
{
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
NSLog(#"ibooks is installed");
}
else
{
NSLog(#"no ibooks installed");
}
Fixed it! Took some time away from the project and after coming back two weeks later I figured it out in <15 minutes.
So it was a memory issue for the docController and by declaring in the .h file and using (retain) it works perfectly. I was also able to use the [NSBundle mainBundle] method as well.
.h
#property (retain)UIDocumentInteractionController *docController;
.m
#synthesize docController;
//in bar button IBAction
NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"itms-books:"]]) {
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
NSLog(#"iBooks installed");
} else {
NSLog(#"iBooks not installed");
}
On iOS 8 the layout of the file system changed and sharing files directly from the main bundle no longer works. Copy the file to the documentsdirectory and share it from there.
Here is how to create the file path:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *fileName = [NSString stringWithFormat:#"%#.pdf",litFileName];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *url = [NSURL fileURLWithPath:filePath];
I am trying to download and open .ics file in my app.
I found few question, and here's some code I am using
// NSString *path = [[NSBundle mainBundle] pathForResource:#"http://www.nmsd.wednet.edu//site/handlers/icalfeed.ashx?MIID=607" ofType:#"ics"];
NSURL *url = [NSURL fileURLWithPath:#"http://www.nmsd.wednet.edu//site/handlers/icalfeed.ashx?MIID=607"];
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Nothing at all happens. no errors at all. Would it be easier to look for a library to achieve this.
If you have a link to remote .ics file, then prepare it for Safari.
Let's suppose that you have a link to iCal file:
"pl-dev2.office.org/events/sync.ics?t=90613b6c7f8".
Just add prefix webcal:// to your link.
Swift
let link = "webcal://pl-dev2.office/events/sync.ics?t=90613b6c7f8"
let webcal = NSURL(string: link)
UIApplication.sharedApplication().openURL(webcal)
Objective-C
NSString *link = "webcal://pl-dev2.office/events/sync.ics?t=90613b6c7f8"
NSUrl *webcal = [NSURL URLWithString:link];
[[UIApplication sharedApplication] openURL:webcal];
Safari will sync it in your Calendar app for You.
The #Bartłomiej Semańczyk is adding a calendar, if you want to add a event you can open url with SFSafariController
let link = "https://www.apple.com/v/apple-events/home/p/built/assets/event.ics?d=3213214324313"
let url = URL(string: link)!
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
You use NSURL fileURLWithPath with an URL string as parameter although it expects file path. Change it to NSURL URLWithString.
we can use UIDocumentInteractionController to share a file to other apps like this:
NSURL *url = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: #"test.txt" ofType: nil]];
UIDocumentInteractionController *dic = [[UIDocumentInteractionController interactionControllerWithURL: url] retain];
[dic presentOpenInMenuFromRect: CGRectZero inView: self.view animated: YES];
but how to share a text string(not a text file) by UIDocumentInteractionController?
Android Device can do this.
Thanks.
You can't, UIDocumentInteractionController is for documents. It is even in the name.
A solution would be to save the string to text file and share that file, or if you want to share a string, for example to post on a social network you should use UIActivityViewController
I am attempting to put the following functionality into an iOS app I am writing:
Ship a set of PDFs in the resources folder of the project in XCode
Copy the PDFs to the app directory
Open the PDF in a webview.
As far as I can see, the first two steps work ok (I've used FileManager to check fileExistsAtPath after the copy operation).
However, the webview is empty, and is erroring out ("the requested URL does not exist on server").
My code for the file open is as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *localDocumentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = #"example.pdf";
NSString *localDocumentsDirectoryPdfFilePath = [localDocumentsDirectory
stringByAppendingPathComponent:pdfFileName];
pdfUrl = [NSURL fileURLWithPath:localDocumentsDirectoryPdfFilePath];
[webView loadRequest:[NSURLRequestWithURL:pdfUrl];
This works fine on the simulator, but doesn't work on the device
Are you sure you don't want to let the UIDocumentInteractionController do the heavy lifting for you?
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
As posted by Anna Karenina above, "The device is case-sensitive. Make sure the filename matches exactly"
As bshirley suggested UIDocumentInteractionController is a great option to present your PDF. Initially I tried using the 3rd party JSQWebViewController but I was getting a blank screen on device while on simulator it was working. UIDocumentInteractionController worked great for me! For Swift you can do:
let interactionController = UIDocumentInteractionController(url: fileURL)
interactionController.delegate = self
interactionController.presentPreview(animated: true)
and implement the delegate method:
// Mark: UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return UIApplication.shared.keyWindow!.rootViewController!
}