Why UIDocumentMenu Delegate to self doesn't work? - ios

I am following Apple documentation for UIDocumentMenuViewController and the following is my code. importMenu.delegate = self doesn't work and Xcode shows: Cannot assign value of type 'ViewController' to type 'UIDocumentMenuDelegate?' . Please help. Thanks!
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let importMenu = UIDocumentMenuViewController(documentTypes: ["public.text", "public.data"], inMode: .Import)
importMenu.delegate = self
self.presentViewController(importMenu, animated: true, completion: nil)
}
}

According to UIDocumentMenuDelegate Protocol Reference, your ViewController must conforms to UIDocumentMenuDelegate and must implements documentMenu:didPickDocumentPicker:
extension ViewController: UIDocumentMenuDelegate {
func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
// do stuffs here
}
}

your delegation class should extend from UIDocumentMenuViewDelegate in the view controller
import UIKit
class ViewController: UIViewController, UIDocumentMenuViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let importMenu = UIDocumentMenuViewController(documentTypes: ["public.text", "public.data"], inMode: .Import)
importMenu.delegate = self
self.presentViewController(importMenu, animated: true, completion: nil)
}
}

Related

Using UIDocumentPickerViewController(documentTypes:) in swift

While using UIDocumentPickerViewConroller in my code to select an audio file in app, this error came out and I can't find (documentTypes: )at UIDocumentPickerViewController.
#IBAction func AddMusic(_ sender: UIButton) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeAudio as String], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil)
}
}
extension ViewController: UIDocumentPickerDelegate{
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){
}
}
UIDocumentPickerViewController(documentTypes: [String], in: UIDocumentPickerMode) was deprecated in iOS 14.0
That Method is replaced by this UIDocumentPickerViewController(forOpeningContentTypes contentTypes: [UTType], asCopy: Bool) method
First, You need to import UniformTypeIdentifiers
import UniformTypeIdentifiers
So, You can use this as below
let supportedTypes: [UTType] = [UTType.audio]
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
pickerViewController.delegate = self
pickerViewController.allowsMultipleSelection = false
pickerViewController.shouldShowFileExtensions = true
self.present(pickerViewController, animated: true, completion: nil)

How to use MPMedaPickerController on swift 3?

I'm simply trying to get it so that when you press a button, you display the user's music library, and then you can select a song. I've found that the way to do this is through MPMediaPickerController but I've been struggling to get it to work. This is what my program looks like so far:
import MediaPlayer
import UIKit
class ViewController: UIViewController, MPMediaPickerControllerDelegate {
var mediaPicker: MPMediaPickerController?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func showSongs(_ sender: Any) {
displayMediaPicker()
}
func displayMediaPicker() {
mediaPicker = MPMediaPickerController(mediaTypes: .music)
if let picker = mediaPicker {
picker.delegate = self
picker.allowsPickingMultipleItems = false
picker.showsCloudItems = false
picker.prompt = "Please Pick a Song"
view.addSubview(picker.view)
}
}
}
I know that there should be a property [self presentViewController:picker animated: true completion:nil] after view.addSubview(picker.view), but when I type it I have the options presentingViewController & presentedViewContoller, but no presentViewController
Any help would be much appreciated
The function presentViewController: animated: completion: function was renamed in Swift 3 so now it looks like this:
present(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)
So, in your case you'd end up with a presentPicker function looking something like this (notice the last line):
func presentPicker() {
mediaPicker = MPMediaPickerController(mediaTypes: .music)
if let picker = mediaPicker {
picker.delegate = self
picker.allowsPickingMultipleItems = false
picker.showsCloudItems = false
picker.prompt = "Please Pick a Song"
present(picker, animated: false, completion: nil)
}
}
Hope that helps you.
Swift 5, Swift 4 Very simple solution
//MARK:- add it in your info.plist file
<key>NSAppleMusicUsageDescription</key>
<string>$(app Name) uses music</string>
//MARK:- Import in your file
import MediaPlayer
//MARK:- Import as your Delegates
MPMediaPickerControllerDelegate
//MARK:- Just call this function
func openAudio()
{
let audiopicker = MPMediaPickerController(mediaTypes: .anyAudio)
audiopicker.prompt = "Audio"
audiopicker.delegate = self
audiopicker.allowsPickingMultipleItems = false
self.present(audiopicker, animated: true, completion: nil)
}

swift: iClouds Documents seems to be disabled when loaded from custom application

i am trying to upload the PDF files from iCloud in my custom application. But as i move to iCloud from my application the documents cannot be selected it seems like as if they are disabled. Most of all the explanations are in Obj-C so i could no understand exactly.
What is wrong here? This is my code in swift2:
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIDocumentPickerDelegate, UITextViewDelegate {
#IBOutlet weak var emailButton: UIButton!
#IBOutlet weak var UITextField: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func handleImportPickerPressed(sender: AnyObject) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeText as NSString as String], inMode: .Import)
documentPicker.delegate = self
presentViewController(documentPicker, animated: true, completion: nil)
}
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
print(url)
}
They are disable means that document types you have in your iCloud does not match while your are selecting.Use documents types as:
#IBAction func handleImportPickerPressed(sender: AnyObject) {
let documentPicker = UIDocumentPickerViewController(documentTypes:["public.data", "public.text"],inMode: UIDocumentPickerMode.Import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.FormSheet
presentViewController(documentPicker, animated: true, completion: nil)
}
There are many other document types. They are disable means that they cannot be read by the documentTypes you are passing.

Swift 3 add new contact with phone and email information

I'm trying to prompt the user to create a new contact and pass in information. (specifically a phone and email)
I've found numerous examples of using a CNMutableContact and adding an email to it. However, any of the code involving the CNContact gives me a "Use of undeclared type" error.
How can I setup my class to prompt the user to save the contact?
import ContactsUI
//add CNContactViewControllerDelegate to your ViewController
class ViewController: UIViewController , CNContactViewControllerDelegate {
func addPhoneNumber(phNo : String) {
if #available(iOS 9.0, *) {
let store = CNContactStore()
let contact = CNMutableContact()
let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue :phNo ))
contact.phoneNumbers = [homePhone]
let controller = CNContactViewController(forUnknownContact : contact)
controller.contactStore = store
controller.delegate = self
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController!.pushViewController(controller, animated: true)
}
}
You Can Do Something Like This.
extension ViewController: CNContactViewControllerDelegate {
func showNewContactViewController() {
let contactViewController: CNContactViewController = CNContactViewController(forNewContact: nil)
contactViewController.contactStore = CNContactStore()
contactViewController.delegate = self
let navigationController: UINavigationController = UINavigationController(rootViewController: contactViewController)
present(navigationController, animated: false) {
print("Present")
}
}
}
Swift 4
import ContactsUI
implement delegate CNContactViewControllerDelegate
#IBAction func UserTap_Handler(_ sender: Any) {
self.navigationController?.isNavigationBarHidden = false
let con = CNContact()
let vc = CNContactViewController(forNewContact: con)
vc.delegate = self
_ = self.navigationController?.pushViewController(vc, animated: true)
}
//MARK:- contacts delegates
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
print("dismiss contact")
self.navigationController?.popViewController(animated: true)
}
func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
return true
}

How to open files imported by UIDocumentPicker?

In my app I picked files by UIDocumentPicker and put file names on a tableView. When clicking on a cell, I want the app open the file. I have no idea how to open the files picked before. Please help.
import UIKit
extension ViewController: UIDocumentMenuDelegate {
func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
self.presentViewController(documentPicker, animated: true, completion: nil)
}
}
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
if controller.documentPickerMode == UIDocumentPickerMode.Import {
dispatch_async(dispatch_get_main_queue()) {
if let fileName = url.lastPathComponent {
self.files.append(fileName)
}
}
}
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var files = [AnyObject]()
#IBOutlet weak var fileTableView: UITableView!
#IBAction func addDocuments(sender: AnyObject) {
let importMenu = UIDocumentMenuViewController(documentTypes: ["public.data", "public.text"], inMode: .Import)
importMenu.delegate = self
self.presentViewController(importMenu, animated: true, completion: nil)
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.data", "public.text"], inMode: .Import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
self.presentViewController(documentPicker, animated: true, completion: nil)
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
You need to keep a reference to the entire URL, not just the filename.
Add the full url to self.files. Then update your cellForRowAtIndexPath to show just the lastPathComponent of that URL.
Then in didSelectRowAtIndexPath you have the access to the full URL of the file.

Resources