5

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

6
  • possible duplicate of How can I transfer files from one application to another in the same iOS device? Commented Jan 1, 2014 at 17:28
  • UIDocumentInteractionController is great for when you wish to present the user with available options, however the OP asked for opening file with 'default application'. Eg: an html file will open with mobile safari automatically, and not present the user with other web browser apps, that have also registered for that UTI type. @Darren Commented Jan 1, 2014 at 17:47
  • ok when I use UIdocumentInterationController,it throws error as "launch services unable to find app identifier com.apple.mobilemail" Commented Jan 1, 2014 at 18:09
  • what file types are you wanting default application to open specifically? @user2634244 Commented Jan 1, 2014 at 18:42
  • I am using gif, tif, png and text. But I am using them in simulator. I need to ask onething, what is UTI of UIDocumentInteractionController? Do I neeed to specify the UTI, is it mandatory? Commented Jan 2, 2014 at 4:20

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hard to say but probably not. Main reason being the simulator has a limited repertoire of apps compared to an actual device. The only app I can see of any interest is the photos app. I would test with a png file on sim and see if you get a nibble, but my advice is to test this sort of thing on an actual device and avoid the sim completely.
Ok thanks, I tested some other samples also the preview is opening correctly but, actual document is not opening in simulator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.