-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentPicker.swift
53 lines (45 loc) · 1.75 KB
/
DocumentPicker.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// UIViewController+DocumentPicker.swift
//
// Created by Pushpank on 12/10/19.
// Copyright © 2019 Pushpank. All rights reserved.
//
import Foundation
import UIKit
class DocumentPicker: NSObject {
static var shared = DocumentPicker()
var completion:((_ data: Data?) -> Void)?
func showDocumet(_ controller: UIViewController, _ completion: @escaping(Data?) -> Void) {
self.completion = completion
if #available(iOS 11.0, *) {
UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil
}
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .import)
documentPicker.delegate = self
controller.present(documentPicker, animated: true, completion: nil)
}
}
extension DocumentPicker: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if controller.documentPickerMode == .import {
let coordinator = NSFileCoordinator()
coordinator.coordinate(readingItemAt: urls.first!, options: NSFileCoordinator.ReadingOptions(rawValue: 0), error: nil, byAccessor: { (url) in
self.getData(url)
})
}
controller.dismiss(animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true, completion: {
self.completion?(nil)
})
}
func getData(_ url: URL) {
do {
let data = try Data(contentsOf: url)
completion?(data)
} catch {
print(error.localizedDescription)
}
}
}