iOS 9 UIDocumentInteractionController does not open file with the app - ios

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

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];
}

Open safari view controller from table view on iOS 9 and open in safari on iOS 8 or 7

Hi I would like to open my website from my table view cell in the safari view controller if the user is on iOS 9 or above. If the user is on iOS 7 or 8 the website should open up in the standard safari app.
This is the code I currently use which opens safari.
case 3: { // Follow us section
switch (indexPath.row) {
case 0: { //Website
NSURL *url = [NSURL URLWithString:#"http://www.scanmarksapp.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
break;
default:
break;
}
}
break;
I believe this code should open the safari view controller with my website. But I am unsure how to combine both sets of code.
- (void)openLink:(NSString *)url {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
if (URL) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
}
#pragma Safari View Controller Delegate
- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller {
[controller dismissViewControllerAnimated:YES completion:nil];
}
I understand this is the code used to determine what iOS version it is
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
I have followed your advice
- (void)openLink:(NSString *)url {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
if (URL) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
} else {
// will have a nice alert displaying soon.
}
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
NSURL *url = [NSURL URLWithString:#"http://www.scanmarksapp.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
Then added this code under my table view cell didSelectRowAtIndexPath
case 3: { // Follow us section
switch (indexPath.row) {
case 0: { //Website
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
if (URL) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
} else {
// will have a nice alert displaying soon.
}
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
NSURL *url = [NSURL URLWithString:#"http://www.scanmarksapp.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
}
break;
default:
break;
}
}
break;
I am getting the error "Use of undeclared identifier url" on this line of code
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.scanmarksapp.com", url]];
Removing url at the end of NSStringWithFormat makes the Safari view controller work. However on iOS below 9.0 e.g. 8.4 the app crashes.
The standard and recommended approach is to check for capability, not OS version. In this instance, you can check for the existence of the SFSafariViewController class.
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
// Open in Mobile Safari
}
edit
Your implementation of openLink: is wrong.
- (void)openLink:(NSString *)url {
NSURL *URL = [NSURL URLWithString:url];
if (URL) {
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self presentViewController:sfvc animated:YES completion:nil];
} else {
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
} else {
// will have a nice alert displaying soon.
}
}

UIDocumentInteractionController shows unwanted behaviour with options

I am getting the following error just after I press one of the document interaction controller buttons i.e. mail etc:
LaunchServices: invalidationHandler called
My email is not configured with iPhone Device.I want to show the alert for that.
Here is the code that creates the interaction controller - the URL etc is all valid however the delegate calls don't hit my controller even though I have implemented the delegate methods.
- (void)openAppList:(FileInfo *)fileinfo {
NSURL *fileURL = [NSURL fileURLWithPath:fileinfo.fullName];
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[interactionController retain];
interactionController.delegate = self;
BOOL present = [interactionController presentOptionsMenuFromRect:CGRectZero inView:self.tabBarController.view
animated:YES];
if (!present) {
[MainteOrErrorDialog initWithErrorCode:kAlertNotOpenInFileId filename:fileInfo.filename target:nil action:nil];
} else {
[interactionController retain];
}
}
UIDocumentInteractionDelegate Method
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
willBeginSendingToApplication:(NSString *)application
{
NSLog(#"Will Begin: %#", application);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
didEndSendingToApplication:(NSString *)application
{
NSLog(#"Did End: %#", application);
}

Opening a file by UIDocumentInteractionController

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

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.

Resources