iCloud Drive or Google Drive
Some times we need to access the data in application like we need to upload the data to server or send to someone using the application so default iOS device provide only photo library to access but we need all types of file in one place due to this reason we need to access the google or iCloud drive.
iOS provide classes to use this functionality but respective drive application must be installed in the devices;
Follow this steps:
1: add this two delegate in class where you want to use.
like as:
1 2 |
class ViewController: UIViewController ,UIDocumentPickerDelegate,UIDocumentMenuDelegate{ } |
2: Now create one button so you can click and access the data :
Here i have taken one button and write this code:
1 2 3 4 5 6 7 8 |
@IBAction func importDrive(_ sender: Any) { let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import) importMenu.delegate = self importMenu.modalPresentationStyle = .fullScreen self.present(importMenu, animated: true, completion: nil) } |
Note:
for documnetTypes:
Here I have mentioned only images so you have to pass all types of file extension whatever you need .
3: Now write their delegate so you can pick the selected data.
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 |
@available(iOS 8.0, *) public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { let cico = url as URL print("The Url is : /(cico)", cico) do { let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions()) print(weatherData) let activityItems = [weatherData] let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil) if UI_USER_INTERFACE_IDIOM() == .phone { self.present(activityController, animated: true, completion: { _ in }) } else { let popup = UIPopoverController(contentViewController: activityController) popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true) } } catch { print(error) } //optional, case PDF -> render //displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest) } @available(iOS 8.0, *) public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) { documentPicker.delegate = self present(documentPicker, animated: true, completion: nil) } func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { print(" cancelled by user") dismiss(animated: true, completion: nil) } |
4: this delegate method will be called now, their methods definition
A: documentpicker: this method will called if have selected any file in drive then it will provide the url through which you can access the data and open in web service or share to other.
B: documentPickerWasCancelled : when user did not select any items.