Skip to content

Instantly share code, notes, and snippets.

@Ilesh
Created November 5, 2019 11:42
Show Gist options
  • Save Ilesh/d292ae0ae71b624eacbda5e937b1f11e to your computer and use it in GitHub Desktop.
Save Ilesh/d292ae0ae71b624eacbda5e937b1f11e to your computer and use it in GitHub Desktop.
iCloud UIDocumentPickerDelegate with specific files like doc, documents, text and PDF.
//
// IPDocumentPicker.swift
//
//
// Created by Ilesh on 22/11/18.
// Copyright © 2018. All rights reserved.
//
import UIKit
import MobileCoreServices
class IPDocumentPicker :NSObject,UIDocumentPickerDelegate {
static let shared = IPDocumentPicker()
typealias complitionHandler = (Bool,URL?) -> Swift.Void
private var block : complitionHandler?
func OpenDocumentpicker(controller:UIViewController, response: @escaping complitionHandler){
let types = [kUTTypePDF,"com.microsoft.word.doc","org.openxmlformats.wordprocessingml.document",] as [Any]
let documentPicker = UIDocumentPickerViewController(documentTypes: types as! [String], in: .import)
documentPicker.delegate = self
block = response
documentPicker.modalPresentationStyle = .formSheet
UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = UIColor.black
controller.present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("IPDocumentPicker :- Cancelled ")
self.block?(true,nil) // isCancel,nil
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("IPDocumentPicker URL:- \(urls)")
self.block?(false,urls.first)
}
}
/*extension IPDocumentPicker: UIDocumentPickerDelegate {
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment