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!
}
Related
I am using the Device tab in Xcode to view the content of my app. Here is what I see:
The app has iTunes file sharing enabled:
However I am unable to see the App in iTunes (under my Apps) even after synchronising the phone.
I am wondering if there is an alternative way to access the "test-data.csv" file that my app is generating.
XCode does seem only to list the file but does not allow me to click on the file to open it.
EDIT:
This is the file path I use:
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"test-data.csv"];
As you are using iTunes sharing. You will be able to see the data in iTunes only. Make sure that you are selecting your device first in the iTunes and then going to Application tab. If you are not seeing the apps there, this means your app might be showing in the sharing section. Scroll down a bit and you will see the File Sharing section-> Apps. See if you are able to find your application there.
Hope you might get your app there.
See the screeshot:-
Are you sure you are saving your data in the public folder of your app and not in the private one?
Public folder:
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
Private folder:
func getLibraryDirectory() -> NSString {
var paths = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
let libraryDirectory = paths[0]
return libraryDirectory
}
You can send this file by attaching with email and then download from your email on other desktop etc.
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"CSV File"];
[mailer addAttachmentData:[NSData dataWithContentsOfFile:#"PathToFile.csv"]
mimeType:#"text/csv"
fileName:#"FileName.csv"];
[self presentModalViewController:mailer animated:YES];
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];
I need to modify an app to print and email a document created on the iPad.
I'm thinking that I should create a PDF document, as shown in the tutorial link below:
http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2
However, once created, I want to print and/or email the document? Are there any other suggestions which may not require pdf document?
I need to create a document based on data entered into a UIView and then either print and/or email it accordingly.
Any suggestions or assistance with this would be great.
You might want to use UIDocumentInteractionController for this purpose. Once you've created your PDF, you can present it with UIDocumentInteractionController by passing the URL of your created/saved PDF. This automatically gives you the option of printing/copying and emailing.
Example: (Assuming that you've saved your PDF in the local Documents folder)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath= [documentsDirectory stringByAppendingPathComponent:#"SAVED_PDF_NAME HERE"];
NSURL *URL = [NSURL fileURLWithPath:writableDBPath];
if (URL) {
// Initialize Document Interaction Controller
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
documentInteractionController.UTI = #"com.adobe.pdf";
// Configure Document Interaction Controller
[documentInteractionController setDelegate:self];
[documentInteractionController presentPreviewAnimated:YES]; // This will present the PDF along with options like Mail, Print and Copy..
NOTE: This is after creating your PDF.
Checkout the following links
http://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/drawingprintingios.pdf
or
http://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html#//apple_ref/doc/uid/TP40010156-CH12-SW14
I have made an app that creates a pdf and stores it in the apps documents folder. I would now like to open it and view it from within the app when a 'View pdf' UIButton is pressed.
I have already looked at a few questions on here and I have considered either a separate view controller or perhaps a scroll view.
What is the best method to use?
UPDATE:
I have followed advice and I am trying to use QLPreviewController. I have added QuickLook framework and now have the following, but I am stuck on how to get the path recognised in the pathForResource. Any suggestions?
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
return [NSURL fileURLWithPath:path];
}
- (IBAction)viewPdfButton:(id)sender {
NSString *filename= #"ObservationPDF.pdf";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documnetDirectory = [path objectAtIndex:0];
NSString *pdfPathWithFileName = [documnetDirectory stringByAppendingPathComponent:filename];
[self generatePdf: pdfPathWithFileName];
QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentViewController:previewController animated:YES completion:nil];
}
If the PDF file is in the app documents folder then you shouldn't be thinking about passing it to another app, you should be looking to present the file inside the app. 2 general options:
Add a UIWebView and load the local file into it
Use QLPreviewController to show a new view containing the PDF
The web view is simple and requires no transition on the UI. The preview controller needs a transition but offers some sharing / printing support for free.
This line is confused (and invalid syntax by the looks of it):
NSString *path=[[NSBundle mainBundle] pathForResource:[pdfPathWithFileName] ofType:nil];
You only use NSBundle to get items out of the bundle, and that isn't what you have. You should just be creating the URL with the file path where you save the file:
[NSURL fileURLWithPath:pdfPathWithFileName];
(which you may store or you may need to recreate in the same way as when you save the file)
I've implemented the already nth-times discussed open in iBook feature in my PDF viewer. And it works great when the PDF file does not contain spaces (example1.pdf, example2.pdf). When the PDF has some space (example 1.pdf) in the name clicking the open in iBook button does nothing.
NSString *fileURL = [(Documents *)(self.detailItem) url];
NSArray *subStrings = [fileURL componentsSeparatedByString:#"/"];
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:[subStrings lastObject]];
docIntController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
docIntController.delegate = self;
docIntController.UTI = #"com.adobe.pdf";
[docIntController presentOptionsMenuFromBarButtonItem:sender animated:YES];
Any suggestion is welcomed. Thanks :)
Since the filePath string is intended to be a URL, you likely need to run it through NSString -stringByAddingPercentEscapesUsingEncoding: before invoking fileURLWithPath:
Try enclosing the full path that you send to iBooks in quotes.
For example, use "example 1.pdf" instead of example1.pdf.