Skip to content

Instantly share code, notes, and snippets.

@hemant3370
Last active September 24, 2016 07:01
Show Gist options
  • Save hemant3370/3168c46544a34db31cdc47a5ded250f0 to your computer and use it in GitHub Desktop.
Save hemant3370/3168c46544a34db31cdc47a5ded250f0 to your computer and use it in GitHub Desktop.
// Showing the menu action sheet
var documentMediaTypes: [String] = []
documentMediaTypes.append(String(kUTTypePDF))
documentMediaTypes.append(String(kUTTypePNG))
documentMediaTypes.append(String(kUTTypeImage))
documentMediaTypes.append(String(kUTTypeVideo))
documentMediaTypes.append(String(kUTTypeMovie))
documentMediaTypes.append(String(kUTTypeItem))
let viewController: UIDocumentMenuViewController = UIDocumentMenuViewController(documentTypes: documentMediaTypes, inMode: .Import)
viewController.delegate = self
viewController.modalPresentationStyle = .FormSheet
self.presentViewController(viewController, animated: true, completion: { _ in })
// Menu picker Delegate
func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
self.presentViewController(documentPicker, animated: true, completion: nil)
}
// File Picked delegate
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
print(url.path)
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let docURL = documentsUrl.URLByAppendingPathComponent(url.path!.componentsSeparatedByString("/").last!)
do {
let docData = try NSData(contentsOfURL: url, options: NSDataReadingOptions())
docData.writeToURL(docURL, atomically: true)
} catch {
print(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment