which is this controller in iOS - ios

I was surfing some site in safari. it was attachment button. when I clicked on that button. it showed this controller. Can any1 tell me which is this default functionality safari provide. it is dere in various apps too like slack.
its shows icloud/google drive/dropbox all three in one action.

It is UIDocumentPickController. Check the apple developer documentation
let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.text"], in: UIDocumentPickerMode.import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.formSheet
self.present(documentPicker, animated: true, completion: nil)
And delegate
// MARK: - UIDocumentPickerDelegate Methods
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if controller.documentPickerMode == UIDocumentPickerMode.import {
// This is what it should be
// self.newNoteBody.text = String(contentsOfFile: url.path!)
}
}
After adding the code you should check this post too or else you will get an exception. You should go to capabilities & turn iCloud capabilities on. Add icloud containers also there
UIDocumentMenuController is the same as UIDocumentPickerViewController. But its deprecated. check the developer documentation here

Related

How to get iCloud image URL in swift

I am using SwiftAssetsPickerController for fetching images from photo library and iCloud. It provides all images, But those images which are stored in iCloud, do not provide image URL. I need iCloud image URL. So how can I get iCloud images URL?
import MobileCoreServices
func setDocumentPicker() {
let importMenu = UIDocumentMenuViewController(documentTypes: [kUTTypeJPEG as String], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
after that you have to call the delegate method of DocumentPicker
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print("URL= \(url)")
}
NOTE: You must have to signed in with iCloud account in your device because you can not access iCloud files directly, and you have to enable iCloud functionality in capabilities in also enable & configure in App ID on apple developer account

How to restrict selecting folder for UIDocumentPickerViewController while multiple select option

I have integrated UIDocumentPickerViewController in application. I can access the files from Files app. Need to restrict folder selection from drive and cloud as well.
When I enable multiple file "Select" option using "allowsMultipleSelection". when user select "Select" option in documentPickerViewController, I don't want to allow user to select folder.
How do I restrict user to select "Folder"?.
Code used to launch Files app is:
let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.data"], in: UIDocumentPickerMode.import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
self.present(documentPicker, animated: true, completion: {
if #available(iOS 11.0, *) {
documentPicker.allowsMultipleSelection = true
} else {
// Fallback on earlier versions
}
})
You can not restrict folder selection however you can ignore selected folder URL.
There will "/" character with end of each folder url, So you can compare it by using below code and skip it.
Swift 4
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    var arrFilesURL = [URL]()
    arrFilesURL = urls.filter { (url) -> Bool in
        return url.absoluteString.last != "/"
    }
    controller.dismiss(animated: true, completion: nil)
}

UIDocumentMenuViewController blank Options

I have a problem with iCloud file import
I'm getting empty options after I choose the place to import from (iCloud, google drive, dropbox ... etc)
I have data on iCloud drive and the entitlements are right
So what is wrong with this code or is there anything about the settings may cause this problem?
here is the code
#IBAction func selectionButtonAction(_ sender: Any) {
let types = [kUTTypePDF as String ,kUTTypePNG as String, kUTTypeImage as String,kUTTypeJPEG as String]
UINavigationBar.appearance().isTranslucent = true
let documentMenu = UIDocumentMenuViewController(documentTypes: types, in: .import)
documentMenu.delegate = self
self.viewController?.present(documentMenu, animated: true, completion: nil)
}
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentMenu.delegate = self
self.viewController?.present(documentMenu, animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if controller.documentPickerMode == .import {
// do staff
}
}
Here is what I get When I run the app on iOS 10
When I Press choose File
then after pressing on iCloud
That's what I get if I run the app on iOS 9:
I'm sure I have data there like here in the iCloud Drive application
And the entitlement
I had the same problem, when trying to present a already used Instance of the UIDocumentMenuViewController.
I was instantiating it lazily like this:
lazy var documentMenuController: UIDocumentMenuViewController = {
let controller = UIDocumentMenuViewController(documentTypes: ["com.adobe.pdf"], in: .import)
controller.delegate = self
return controller
}()
Which produced this error every time except the first time.
So I changed the code to this:
var documentMenuController: UIDocumentMenuViewController {
let controller = UIDocumentMenuViewController(documentTypes: ["com.adobe.pdf"], in: .import)
controller.delegate = self
return controller
}
To obtain a fresh instance on every access of the property.

UIDocumentPickerViewController doesn't call the didPickDocument method

I have a document picker, but after selecting a document, the didPickDocumentAt part is never triggered. It was working before I updated swift, is there something that is different now?
func selectDocument(_ sender: UIButton!){
let documentPickerVC = UIDocumentPickerViewController(documentTypes: ["org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"], in: UIDocumentPickerMode.import)
documentPickerVC.delegate = self
self.present(documentPickerVC, animated: true, completion: nil)
}
func documentPicker(_ documentPicker: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print("file picked: \(url)")
documentPicker.dismiss(animated: true, completion: nil)
}
Nothing is 'failing' either, it just isn't calling that documentPicker method.
I have a similar one for selecting media and that one works fine...
func selectMedia(_ sender: UIButton!){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
picker.delegate = self
picker.allowsEditing = false
picker.mediaTypes = [kUTTypeMovie as String]
self.present(picker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String: Any]) {
let url = info[UIImagePickerControllerMediaURL] as! URL
print("media picked: \(url)")
}
Edit:
I just added the documentPickerWasCancelled method, and for some reason that is being called when I select a document.
Note I am selecting a document from google drive, would that have an affect on anything?
Edit 2:
Answered, uninstalled and reinstalled and it worked. See answer below. Thanks everyone for the suggestions.
The above code looks like it is correct. I uninstalled the google drive app (where I was getting files from) and reinstalled it and then it worked as expected. I also tried from dropbox and that worked as well.
Not sure what was making it fail before.

How to access Cloud drives to import files to my app in swift?

I want to get documents from Cloud services like iCloud , google drive and dropbox on a button click (like in WhatsApp screenshot below), does anyone know how to do it in swift ? Thanks in advance
From your project's capabilities. First enable both the iCloud services and the Key-Sharing, import MobileCoreServices in your class and finally extended the following three classes inside your UIViewController :
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Implement the following functions :
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
print("import result : /(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
How to call all of this? Add the following bit of code to your click function..
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Click your button. The following menu will pop up ..
In the case of DropBox. Upon clicking on any item. You will be redirected back to your app and the URL will be logged in your terminal.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Enjoy it.
First enable iCloud documents Capabilitie, see Apple documentation here
The you have to use UIDocumentMenuViewController
let importMenu = UIDocumentMenuViewController(documentTypes: doctypes, inMode: .Import)
importMenu.delegate = self
importMenu.popoverPresentationController?.barButtonItem = self.addButon;
self.presentViewController(importMenu, animated: true, completion: nil)

Resources