jerfin
jerfin

Reputation: 893

How to open downloaded file with app in IOS?

I am developing a app in which i downloaded datas of various format such as (.pdf,.txt,.mp3 etc) i have used the code

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,recFilName]; [nsdataFromBase64String writeToFile:filePath atomically:YES];

I successfully downloaded by using the above code, Now i have to open the downloaded files with the appropriate app that supports that extension can any one help me with the code.

Upvotes: 0

Views: 1531

Answers (2)

iCoder
iCoder

Reputation: 1318

Document picker allows user to select the items outside the App's sandbox. If you are downloading the files and storing it locally on your documents directory please see below code ..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
// Suppose you want to open downloaded PDF file
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"YourPDFFileName"];

NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];

// Now load this request in WebView.

Similarly you can open other formats as well.

Upvotes: 0

Mahendra
Mahendra

Reputation: 8904

You need to create an instance of UIDocumentPickerViewController and implement its delegate methods.

UIDocumentMenuViewController *documentPicker =
[[UIDocumentMenuViewController alloc] initWithDocumentTypes:@[<items>,...]
            inMode:UIDocumentPickerModeOpen];
documentPicker.delegate = self;
[self presentViewController:documentPicker animated:YES completion:nil];

Upvotes: 2

Related Questions