How to open a file browser and select a .pdf file

2019-04-14 03:35发布

How to open a file browser when a button is tapped in my application in iphone.It has to show the files with an extension of .pdf format.And it has to be saved to a local database.For example,if we want to send an email to xyz,we attach some files if we want to send files.If we click attach a file button,it will show the file explorer.In the same manner the file explorer has to be opened if the user clicks a button in iphone.Please help me out of this.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-04-14 04:26

I did something similar with my IOS application. As IOS don't have(I think) a file browser and you are, in big therms, only allowed to use your application Documents folder when he clicks to attach file I open a UIPopoverController and I show him all the files with the extension I want. In my case was the .csv files and I use this algorithm:

        _data= [[NSMutableArray alloc] init];
    NSString *documentsPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSError *error;
    NSArray* files= [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
    NSRange range;

    for (NSString* file in files) {
        range = [file rangeOfString:@".csv" 
                            options:NSCaseInsensitiveSearch];
        if (range.location!= NSNotFound) {
            [_data addObject:file];
        }
    }

Where _data is an array I use in my tableView to know all csv files I have. If you want to do that as a file browser show the directories to and let the user see the files into the directories recursively. My table dataSource:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell==nil) {
    cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}    
cell.textLabel.text=[_data objectAtIndex:indexPath.row];    
return cell;
}

I hope it helps you

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-14 04:27

Right now, with Files app you can choose from all files on iCloud or any connected cloud storage like Google Drive etc.

import MobileCoreServices

class ViewController: UIViewController, UINavigationControllerDelegate, UIDocumentPickerDelegate {

    func navigateToImportFile() {
        var types: [String] = [String]()

        // Restrict only to PDF, you can use any type you want here like audio, video, texts etc
        types.append(String(kUTTypePDF))

        let documentPickerController = UIDocumentPickerViewController(documentTypes: types, in: .import)
        documentPickerController.delegate = self
        present(documentPickerController, animated: true, completion: nil)
    }


    /// Called when pdf to import is selected
    func documentPicker(_ controller: UIDocumentPickerViewController,
              didPickDocumentAt url: URL) {

        print("Selected URL: \(url)")

        // Dismiss this view
        dismiss(animated: true, completion: nil)

        if let fileData = try? Data(contentsOf: url) {
            // Here is data of your PDF file
        }

    }

}

This is somehow familiar to filesystem and allows user to access any file they need as long as they store it somewhere in the cloud.

查看更多
登录 后发表回答