Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open file from local file system with default application iOS

I have downloaded some files from server and stored into local file system. I want to open them with default application in the device. How can I open files with default application. Please provide some sample code.

thanks

like image 703
KishoreThindaak Avatar asked Jan 01 '14 14:01

KishoreThindaak


1 Answers

First you need to represent the resource (downloaded file to be opened) with an NSURL object. The following assumes an NSString named filePath that is already initialised with the path to the resource to open.

NSURL *resourceToOpen = [NSURL fileURLWithPath:filePath];

Then it's best to check first that there is an app that will open the resource.

BOOL canOpenResource = [[UIApplication sharedApplication] canOpenURL:resourceToOpen];

Finally if the above line returns yes then open the resource.

if (canOpenResource) { [[UIApplication sharedApplication] openURL:resourceToOpen]; }

I quote the following from UIApplication class reference with respect to the instance method canOpenURL:

This method guarantees that that if openURL: is called, another app will be launched to handle it. It does not guarantee that the full URL is valid.

However, if your wish to present the user with a list of apps that have registered with the appropriate UTI for that file type you can do something like this-

UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

You must implement the UIDocumentInteractionControllerDelegate protocol. Also for known file types the system should resolve the correct assignment of the UTI property without setting it.

like image 81
Bamsworld Avatar answered Oct 19 '22 01:10

Bamsworld