Opening a file by UIDocumentInteractionController - ios

I am using UIDocumentInteractionController for opening documents in my app. I have used method below for previewing the PDF file:-
- (IBAction)previewDocument:(id)sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"];
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentOptionsMenuFromRect:down.frame inView:self.view animated:YES];
}
I have read that we can read files in an app from other app too by UIDocumentInteractionController.
In my app, How can I read the files from other app using UIDocumentInteractionController? How does all this happens?

Previewing the PDF document and sharing.
Set Delegate Method:-
UIDocumentInteractionControllerDelegate
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"Your PDF Name" withExtension:#"pdf"];
UIDocumentInteractionController *documentInteractionController =[UIDocumentInteractionController interactionControllerWithURL:URL];
documentInteractionController.delegate = self;
[documentInteractionController presentPreviewAnimated:YES];
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
If you have any confusion, click on the below link and read :
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html

Related

Download image from iCloud Drive iOS objective C

I have gone through many links, but nothing worked for me. All i need to do is fetch all iCloud Drive files and download images. I succeeded fetching all files using below code
- (void)presentDocumentPicker {
NSArray *documentTypes = #[#"public.content", #"public.text", #"public.source-code ", #"public.image", #"public.audiovisual-content", #"com.adobe.pdf", #"com.apple.keynote.key", #"com.microsoft.word.doc", #"com.microsoft.excel.xls", #"com.microsoft.powerpoint.ppt"];
UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
documentPickerViewController.delegate = self;
[self presentViewController:documentPickerViewController animated:YES completion:nil];
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
self.documentURL = url;
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.delegate = self;
previewController.dataSource = self;
[self.navigationController pushViewController:previewController animated:YES];
}
now i don't know how to proceed further to download image.
i was able to download images using this
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
self.documentURL = url;
NSNumber *isDownloadedValue = NULL;
BOOL success = [url getResourceValue:&isDownloadedValue forKey: NSURLUbiquitousItemIsDownloadingKey error:NULL];
if (success && ![isDownloadedValue boolValue]) {
[[NSFileManager defaultManager]startDownloadingUbiquitousItemAtURL:url error:nil];
// load image from Ubiquity Documents directory
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:imageData];
}
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.delegate = self;
previewController.dataSource = self;
[self.navigationController pushViewController:previewController animated:YES];
}

iOS 9 UIDocumentInteractionController does not open file with the app

In previous iOS, I used this code and UIDocumentInteractionController to open a file, from my app, with another app. Since iOS 9, the UIDocumentInteractionController appears on the screen, I select the option "Copy to", but nothing happens. How can I solve this?
NSURL *URL = [NSURL fileURLWithPath:path];
if (URL != nil) {
// Initialize Document Interaction Controller
documentController = [UIDocumentInteractionController interactionControllerWithURL:URL];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
you have to manage your response time of open :-
-(void)exportToDropBox{
[self performSelector:#selector(presentImagePicker) withObject:nil afterDelay:1.0];
}
-(void)presentImagePicker{
NSURL *targetURL = [NSURL fileURLWithPath:exportedPdfFileName];
[self presentDocumentWithURL2:targetURL];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
- (void)presentDocumentWithURL2:(NSURL*)url {
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
self.documentController.delegate = self;
[self.documentController presentOpenInMenuFromRect:CGRectMake(self.view.frame.size.width/2 - 49/2, self.view.frame.size.height-49, 49, 49) inView:self.view animated:YES];
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
self.documentController = nil;
}

How to take video from gallery like image inside application in ios

In my application i need to take video from simulator and i need to upload to server.can any one help to me.Please provide any solution as soon as possible.
Thanks & Regards
T.swathi
First, create a UIImagePickerController
On your class, you will need to implement the UIImagePickerControllerDelegate protocol and wire it up to the delegate property on the picker. You should be able to get the location of the media selected from didFinishPickingMediaWithInfo once the user has selected something.
//Check in iOS Device not in Simulator
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// this is the file system url of the media
NSURL* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
// TODO: read in data at videoUrl and write upload it to your server
}
Use this function may be help full
-(void)selectVideoButtonClicked:(id)sender{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:nil];
}
and take delege of "UIImagePickerControllerDelegate" & and import framework and using delegate function pick video from library
MobileCoreServices.framework first import in project and after than import in file CoreServices/CoreServices.h
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey: UIImagePickerControllerMediaURL] path];
NSLog(#" Video Path %#",moviePath);
NSString *fileName = [[NSString alloc]init];
//If you wants get File Name Then you can use this
if ([moviePath rangeOfString:#"//"].location != NSNotFound) {
NSString *separatorString = #"//";
NSArray *disSplit = [moviePath componentsSeparatedByString:separatorString];
fileName = disSplit[1] ;
// [self videoUploadingDirect:moviePath];
}
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Thanks :)

Open file viewer (PDF) from iOS application

We have a code to download a PDF from the server using a SOAP protocol.
We store the file on the Documents folder of the iOs device, and the we want to allow the user to open the file (usually PDF's files)
But, at least on the simulator, the application didn't open the file successfully.
Of course, I can open manually the file (in order to check that the file has been downloaded perfectly)
// Get the documents folder where write the content
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:documentDownloaded.fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFileCompletePath]]){
NSString *filePath = [[NSString alloc] initWithFormat:#"%#", [self getFileCompletePath]];
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
if ([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL: url];
} else {
NSLog(#"Not supported application to open the file %#", filePath);
}
[url release];
[filePath release];
}
The file full path in the simulator is:
/Users/User/Library/Application Support/iPhone Simulator/4.3.2/Applications/87800296-2917-4F26-B864-B20069336D1D/Documents/0000907.pdf
Anybody knows if is something wrong?
The file could be and spreadsheet, pdf, image, document ... but for the first work around will be great just for pdf's.
Thank you,
omz you was right. The final solution was the next.
My controller implemented the UIDocumentInteractionControllerDelegate
When the user wants to open the document the code was the next:
NSString *filePath = [[NSString alloc] initWithFormat:#"%#", [self getFileCompletePath]];
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
UIDocumentInteractionController *documentController = [self setupControllerWithURL:url
usingDelegate:self];
[documentController retain];
[documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
[url release];
[filePath release];
Using the next method, you could found it on the apple developer library
- (UIDocumentInteractionController *)
setupControllerWithURL: (NSURL *) fileURL
usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
Finally we include the delegate methods:
#pragma mark methods for the UIDocumentInteractionControllerDelegate
- (void)previewDocumentWithURL:(NSURL*)url
{
UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
preview.delegate = self;
[preview presentPreviewAnimated:YES];
[preview retain];
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
[controller autorelease];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
return self.view.frame;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
return self.view;
}
Thank you,
You cannot open file URLs with UIApplication's openURL: method. Because of sandboxing, other apps could not access any files within your application's Documents folder.
You need to use UIDocumentInteractionController to open documents that you download or create in other applications.

How to load a PDF before it's view is pushed

I'd like to load the PDF in my child view before it gets pushed onto the navigation stack. Right now there is a brief delay while the UIWebview is loading up the PDF file. What I would like is the PDF to be loaded so it slides on to the screen with the view.
I tried customizing the initWithNib method like below but then nothing loads. My thought was "If I start the PDF load during initialization it'll be ready when I go to push it." Guess not.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:pdfURL ofType:#"pdf"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
}
return self;
}
Here's my current implementation:
In the parent view I initialize the pdfViewer using this:
PDFViewerVC *vc = [[PDFViewerVC alloc]initWithNibName:#"PDFViewerVC" bundle:nil];
vc.pdfURL = #"Service Manual RS20";
vc.title = [self.list objectAtIndex:row];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
In the pdfViewer I load the PDF using this:
-(void)viewDidLoad{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:pdfURL ofType:#"pdf"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
[super viewDidLoad];
}
Thanks.
Try waiting until the UIWebView's webViewDidFinishLoad: delegate method is called before pushing its view controller onto the navigation stack.
You don't get very far with UIWebView, you need to go deeper, use Quartz.
Of course that means you need to replicate much that UIWebView has.
Check Apple's ZoomingPDFViewer for a simple example. http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html

Resources