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)
Related
I am trying to get path of legal.txt in my project. However, whatever I do. I am getting a null path back. it is in the Policies directory. How do I get it's path as a string. I need to send that path to a webview controller. Can someone help.
-(void)linkPressedOnTextView:(PPLinksTextView *)tv url:(NSURL *)url{
if ([url.host isEqualToString:#"sign_up"]) {
GetStartedController *getStartedVC = [[GetStartedController alloc] init];
[self.navigationController pushViewController:getStartedVC animated:YES];
}
else if([url.host isEqualToString:#"consentMedicalSevices"]){
PPNavigationViewController *vc = [[PPNavigationViewController alloc]init];
[vc openPolicies:#"Policies/privacy.txt"];
Will this work. I need a way to direct user to a webview once he clicks on the link and privacy text opens up.
From apple doc:
pathForResource:ofType:inDirectory:
Returns the full pathname for the resource file identified by the specified name and extension and residing in a given bundle directory.
NSString *filePath = [NSBundle pathForResource:#"file" ofType:#"txt" inDirectory:[[NSBundle mainBundle] bundlePath]];
NSLog(#"File Path: %#", filePath);
Output:
File Path: /Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Bundle/Application/EB4EA25F-65E6-4CA8-B8C2-C7C0C64C6C0F/Sample.app/file.txt
I'm getting this behaviour using Xcode 8.0. The problem is, after downloading a file and storing it on documents directory (code provided below), QLPreviewController only displays document's name and size. The property currentPreviewItem returns the correct path document. What's even more strange, is that if I try to open that document from another controller in my app, it works fine. I've implemented both QLPreviewControllerDelegate and QLPreviewControllerDataSource.
Code for downloading and saving document:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSURL *url = [NSURL URLWithString:file[#"url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
if (!data) {
completion([NSError new]);
return;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:AppName];
path = [path stringByAppendingPathComponent:file[#"name"]];
file[#"filePath"] = [NSURL fileURLWithPath:path];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
completion(nil);
});
});
Then, when user selects a document I use the content saved on #"filePath" to show QLPreviewController. I've tried pushing it and presenting it modally and in both cases it just displays a gray page with document's name and size.
File wasn't showed because downloaded contents didn't contain file's extension. When I tried opening it in other controller, it worked because I temporary used another file name containing the correct extension. So I solved the problem adding the extension .pdf to downloaded files that didn't include it. This made QLViewController displaying correctly the file.
In my case, a file was created with the correct name, but the file contained an error message from the server, thus not representing the expected file structure.
I am attempting to create a file browsing app for iPads. I'm reluctant to use another app, such as Documents5, because I want to custom design it and bring in features we need.
Essentially I am looking to create an almost identical app to Documents5 with iCloud Drive support.
I have the code for viewing PDFs, MP3s etc., but I'm a bit bewildered with iCloud Drive integration and the menu interface for file browsing. Additionally, although I already have code for this, how do I link the files to the viewer/player?
iCloud Drive is pretty easy to implement. Assuming that your app has all entitlements and so on properly set up, you can move files to and delete files from iCloud by interacting with it like it is any other directory. You access the directory like this
- (NSString *)iCloudPath
{
NSURL *URL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
return [[URL path] stringByAppendingPathComponent:#"Documents"];
}
Moving files to this directory (to iCloud) is done like this
NSString *fileName = //the name of the file;
NSURL *fileURL = //the file you want to move;
NSString *iCloudPath = [self iCloudPath];
[[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:fileURL destinationURL:[NSURL fileURLWithPath:[iCloudPath stringByAppendingPathComponent:fileName]] error:nil];
The easiest way to display files is through a table view. You can implement a system where the file paths are stored in an array, so that when a cell is selected, you can decide how to open it based off of the file's extension. For example, let's say your table view looks like this
-----------------
| video.mp4 |
-----------------
| text.txt |
-----------------
| file.pdf |
if your user selects row 1 (text.txt), you could implement something like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *filePath = self.files[indexPath.row];
NSString *fileExtension = [filePath pathExtension];
if ([fileExtension isEqualToString:#"txt"])
{
//open the text file
}
}
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 use this code to save some PDF data to a file, send it to another app using the "Open In" menu, then delete the file when that's done:
- (void)openIn:(NSData *)fileData {
// save the PDF data to a temporary file
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", self.name];
NSString *filePath = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(), fileName];
BOOL result = [fileData writeToFile:filePath atomically:TRUE];
if (result) {
NSURL *URL = [NSURL fileURLWithPath:filePath];
UIDocumentInteractionController *controller = [[UIDocumentInteractionController interactionControllerWithURL:URL] retain];
controller.delegate = self;
[controller presentOpenInMenuFromBarButtonItem:self.openInButton animated:TRUE];
}
}
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
// when the document interaction controller finishes, delete the temporary file
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", self.name];
NSString *filePath = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(), fileName];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
This has worked fine until iOS 8. Now, the file is created and I can verify that it contains the correct content, the Open In menu appears, I can select an app, and the delegate method runs and cleans up the file. But instead of iOS switching to the selected app and copying the file into it as it did before, the Open In menu simply closes when I select an app, and the file is not copied.
This works if I give the UIDocumentInteractionController an existing file. It also works if I use the provided fileData but change the destination filename to the filename of an existing file. This suggests a permissions problem -- as if new files are created in iOS 8 with default permissions that UIDocumentInteractionController can't read.
Does anyone know what's happening and how I can work around it?
It looks like the order of operations has changed slightly in iOS 8. DidDismissOpenInMenu used to run after the file was finished sending, but now it runs after the file begins sending. This means my cleanup code was sometimes running before the file was finished sending, leaving no file to send. I figured this out after noticing that smaller files were being sent okay; apparently the processing for smaller files was finishing before my cleanup code got them, but the processing for larger files was not.
To ensure the correct timing, but also clean up files that are created when the user opens the DocumentInteractionController and then dismisses the controller without doing anything, I changed my methods like this:
- (void)openIn:(NSData *)fileData {
// save the PDF data to a temporary file
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", self.name];
NSString *filePath = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(), fileName];
BOOL result = [fileData writeToFile:filePath atomically:TRUE];
if (result) {
self.sendingFile = FALSE;
NSURL *URL = [NSURL fileURLWithPath:filePath];
UIDocumentInteractionController *controller = [[UIDocumentInteractionController interactionControllerWithURL:URL] retain];
controller.delegate = self;
[controller presentOpenInMenuFromBarButtonItem:self.openInButton animated:TRUE];
}
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
// the user chose to send the file, so we shouldn't clean it up until that's done
self.sendingFile = TRUE;
}
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
if (!self.sendingFile) {
// the user didn't choose to send the file, so we can clean it up now
[self openInCleanup];
}
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
// the user chose to send the file, and the sending is finished, so we can clean it up now
[self openInCleanup];
self.sendingFile = FALSE;
}
- (void)openInCleanup {
// delete the temporary file
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", self.name];
NSString *filePath = [NSString stringWithFormat:#"%#/Documents/%#", NSHomeDirectory(), fileName];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
Update for iOS 11
Before iOS 11, it seems that the operating system kept a copy of the file available until the receiving app was finished reading it, even though my cleanup function ran as soon as the file was sent out from my app. In iOS 11, this changed and the receiving app fails to read the file because my app deletes it before that's done. So now instead of saving the temporary file to Documents and using the openInCleanup method to delete it immediately, I'm saving the temporary file to tmp and emptying the tmp folder next time the app launches. This approach should also work with older iOS versions. Just remove openInCleanup, change Documents to tmp in the paths, and add this to applicationDidFinishLaunching:
// clear the tmp directory, which will contain any files saved for Open In
NSString *tmpDirectory = [NSString stringWithFormat:#"%#/tmp", NSHomeDirectory()];
NSArray *tmpFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:NULL];
for (NSString *tmpFile in tmpFiles) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:#"%#/%#", tmpDirectory, tmpFile] error:NULL];
}
After reading this post, I already hoped to have found the solution to a similar problem:
For me, as of iOS 8, sharing was only working with Mail.app. It was failing for Dropbox, etc.
Turns out it was something else:
On my interactionController I was setting an annotation like this:
interactionController.annotation = #"Some text"
For unknown reasons, this prevented Dropbox to open at all. There were no error messages or anything. Removing this line solved the issue.