How can I get pdf file of ibook in my application? - ios

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)
}

Related

Document Picker View Controller issue in Swift

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
}

How do I do Custom Alert Action Sheet?

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.

Can't get app to dismiss the mail compose view controller & return to first screen

I'm using the Swift book to try to learn to code. I added the delegate method to dismiss the view, but it's not working. What am I missing here?
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}
}
You just need to declare the delegate function outside of the IBAction:
#IBAction func emailButtonTapped(_ sender: UIButton) {
if !MFMailComposeViewController.canSendMail() {
print("Can not send mail")
return
}
guard MFMailComposeViewController.canSendMail() else { return }
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients(["example#example.com"])
mailComposer.setSubject("Look at this")
mailComposer.setMessageBody("Hello, this is an email from the app I made.", isHTML: false)
present(mailComposer, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}

There's a bug in CNContactPickerController

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)

Importing document with UIDocumentPickerViewController warns "fileprovider plugin was invalidated"

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)
}}

Resources