Print and Email document created on iPad - ios

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

Related

QLPreviewController not displays saved file

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.

iOS Share GIF to ONLY Twitter

I need to give my players an ability to post GIF replays ONLY to their Twitter accounts. I've managed to create a basic general sharing dialog but the problem is, I can't remove all irrelevant sharing options: Notes, Skype and etc. Long hours of fighting with iOS and no results. There are no activity types for the stuff I want to exclude, so adding it to "excludedActivityTypes" is impossible. Tweet Sheet didn't help either, it can't share GIFs.
Are there any other options, guys? Current implementation:
I want to do smth like this (just add FB to excluded activities):
If you are only wanting to allow sharing of the animated GIF to Twitter, it doesn't make much sense to use the UIActivityViewController to display the share sheet with only one option.
Why not just build your own using the SLComposeViewController? You have greater control over the UI and it's fewer button presses for the user. To do so, you can take a look at the sample code provided in this tutorial.
// Use this code :by. Ramani Hitesh iOS developer)
NSURL *imageUrl =[self.ImageArray objectAtIndex:currentPhotoIndex];
NSString *path=imageUrl.absoluteString;
NSArray *strings = [path componentsSeparatedByString:#"/"];
NSString *mygif=[strings objectAtIndex:strings.count-1];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataPath = [documentsPath stringByAppendingPathComponent:#"/MrHRamani"];
NSString *filePath = [dataPath stringByAppendingPathComponent:mygif];
NSURL *urll=[NSURL fileURLWithPath:filePath];
NSLog(#"imag %#",imageUrl);
self.documentationInteractionController.delegate = self;
self.documentationInteractionController.UTI = #"net.whatsapp.image";
self.documentationInteractionController = [self setupControllerWithURL:urll usingDelegate:self];
[self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

Open a pdf saved in my documents folder in Xcode project

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)

How to access and display documents from iPad3 programatically?

I am implementing an application that allows users to add and share different documents. I am done with adding documents by enabling "Application supports iTunes file sharing" in the plist. So user can add his/her documents directly to the application with the help of iTunes. Now my problem is I need access and display all documents under my application in a table view with their title. Based on the user selection I need to display it in a pdf or any other format.
How can I access all the documents under my application?
Also is there any other way to dump documents in to my application except using iTues? Please suggest a better option.
I found a solution for this. By using the following code snippet we can easily access all the files.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList)
{
if ([s hasSuffix:#".pdf"])
{
//do stuff
}
else
{
// do stuff
}
}

Loading Local PDF File Into WebView

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!
}

Resources