Anytime I import a document via UIDocumentPickerViewController, I see a message logged to the console:
TestApp[66587:1550086] plugin com.apple.UIKit.fileprovider.default invalidated
Does this mean theres something wrong with my code? Or is this just an expected warning? Is there anyway to suppress it?
#IBAction func attachFile(sender: UIButton) {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.image"], inMode: .Import)
documentPicker.delegate = self
self.presentViewController(documentPicker, animated: true, completion: nil)
}
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
}
I am using xcode 8.3.2 and swift 3.0...Here is my code...it is working well..just call openDocumentPicker() i hope it helps you
extension AddCaseStep3ViewController : UIDocumentMenuDelegate,UIDocumentPickerDelegate{
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let url = url as URL
//this is the url of your doc you can send enjoy!!
}
#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) {
//dismiss(animated: true, completion: nil)
}
func openDocumentPicker(){
//
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.content","public.data","kUTTypePDF"], in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true, completion: nil)
}}
Related
I want to open document file using swift 4. My requirement is that I want to use document picker. Using Document picker I can upload that file using my application.
In short in my app iIwant other documents like .pdf from device. i had try this .
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let myURL = urls
print("import result : \(myURL)")
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
#IBAction func dsds(_ sender: Any) {
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
I have a button in my view controller, when click a button it open a document picker view controller, i have set type like KUTypePDF and KUTypeZipArchive , when i select any of these item from google drive the picker controller gets dismiss and in console it shows a long un finished strings of number that doesn't stop. I'm confused why it is showing that long string. What i want is that after i select any file from google drive it should be shown in my app. I'm getting the file url right. My code is this,
#IBAction func docsBtnTapped(_ sender: Any) {
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF),String(kUTTypeZipArchive)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .fullScreen
self.present(importMenu, animated: true, completion: nil)}
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: {
print("Hello")
})
}
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)
}
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)
}
This long string come in console when i select any item of PDF or Zip file,
You can use UIDocumentInteractionController to open any kind preview from url:
Here is Simple code to open url:
var documentController:UIDocumentInteractionController!
#IBAction func btnOpenClicked(_ sender: Any) {
if let fileURL = Bundle.main.url(forResource: "icon", withExtension: "jpg") {
// Instantiate the interaction controller
self.documentController = UIDocumentInteractionController.init(url: fileURL)
self.documentController.delegate = self
self.documentController.presentPreview(animated: true)
}
else {
print("File missing! Button has been disabled")
}
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
I have a mail API which sends some information and document. I send only Files documents but I want to show one more choice in UIDocumentPickerView.
How can I show the list " document , photos , camera etc." in the same picker with images ?
My code is :
#available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let urlData = url as URL
let theFile = urlData.absoluteString
self.documentName.text = "\(theFileName)"
}
#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) {
dismiss(animated: true, completion: nil)
}
func mySpecialFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
It's a custom component, you need to code it yourself of find something similar on GitHub or CocoaControls.
Ok, im a bit further now. I got the email button working within the app, but it doesnt add the picture that i have selected in UIImageview in the e-mail. Can someone tell me how to add the picture selected to the email body?
#IBAction func FotoKnop(sender: AnyObject) {
}
#IBAction func chooseImageFromPhotoLibrary() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
#IBAction func chooseFromCamera() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
imageView.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
// start e-mail
#IBAction func sendEmailButtonTapped(sender: AnyObject) {
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
}
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposerVC.setToRecipients(["jvanhattem#it-serve.nl"])
mailComposerVC.setSubject("Mail vanuit PicMail")
mailComposerVC.setMessageBody("Onderstaand de doorgestuurde informatie", isHTML:
false)
return mailComposerVC
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
sendMailErrorAlert.show()
}
// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
You should have a look at documentation for MFMailComposeViewController
You can set the recepients and attachment using setToRecipients: and addAttachmentData:mimeType:fileName:
Try this code fur your email function:
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposerVC.setToRecipients(["nurdin#gmail.com"])
mailComposerVC.setSubject("Sending you an in-app e-mail...")
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
return mailComposerVC
}
And this code in your function for the action or where you want to add...
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
// On Success
} else {
// Error handling here
}
After spending all day (>12 hours) trying to isolate a bug in 13 lines of mind-bogglingly generic code, I have come to the dubious conclusion that there must be a bug in the current iteration of CNContactPickerViewController, in iOS 9.2.
Simply copy+paste this ViewController and link the invite action to a button.
The bug is that MFMessageComposeViewController dismisses itself immediately.
If anybody knows what to do with this, do share?
import UIKit
import MessageUI
import ContactsUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate, CNContactPickerDelegate {
let contactPickerVC = CNContactPickerViewController()
let messageVC = MFMessageComposeViewController()
override func viewDidLoad() {
super.viewDidLoad()
contactPickerVC.delegate = self
}
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
if let phoneNumberValue = contact.phoneNumbers.first?.value as? CNPhoneNumber {
if let phoneNumber = phoneNumberValue.valueForKey("digits") as? String {
// Configure message ViewController
messageVC.messageComposeDelegate = self
messageVC.recipients = [phoneNumber]
messageVC.body = "Yoyoyo"
picker.presentViewController(messageVC, animated: true, completion: nil)
}
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func invite(sender: AnyObject) {
presentViewController(contactPickerVC, animated: true, completion: nil)
}
}
I got it working by dismissing the pickerVC and changing the controller which presents the messageVC!
Insert (before the messageVC config lines):
picker.dismissViewControllerAnimated(true, completion: nil)
Replace
picker.presentViewController(messageVC, animated: true, completion: nil)
with
presentViewController(messageVC, animated: true, completion: nil)