问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/9922755/how-to-open-a-file-browser-and-select-a-pdf-file-in-ios