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

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.

Related

UIDocumentPickerViewController - Delegate method not being called

I'm using UIDocumentPickerViewController to select documents from the Files and upload it to a server. I'm able to successfully access Files, but upon clicking on the file the delegate method doesn't get called.
I've used the following code to call the document picker:
class Uploads: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func uploadDocument(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePlainText)], in: .import)
documentPicker.delegate = self
if #available(iOS 11.0, *) {
documentPicker.allowsMultipleSelection = false
} else {
}
present(documentPicker, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension Uploads: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print(urls.first)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Cancelled")
}
}
I noticed that I'm getting the following warning upon calling the delegate method:
Instance method 'documentPicker(:didPickDocumentsAt:)' nearly matches
optional requirement 'documentPicker(:didPickDocumentsAt:)' of
protocol 'UIDocumentPickerDelegate'
Make 'documentPicker(_:didPickDocumentsAt:)' private to silence this
warning
I believe that the delegate method isn't being called due to this warning, although I couldn't figure out why I'm getting this warning.
Sharing code sample hopefully it'll help:
class ViewController : UIViewController,UIDocumentPickerDelegate{
var documentBrowser: UIDocumentPickerViewController = {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let browser = UIDocumentPickerViewController(documentTypes: [documentsPath], in: .open)
browser.allowsMultipleSelection = true
return browser
}()
override func viewDidLoad() {
super.viewDidLoad()
self.addChild(documentBrowser)
documentBrowser.view.frame = self.view.bounds
view.addSubview(documentBrowser.view)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){
print(urls)
}
}
Problem appears if class which adopts 'UIDocumentPickerDelegate' protocol is declared as 'open'.
For example this class will have a problem:
open class FilePickerHelper: UIDocumentPickerDelegate
while this class will not have a problem:
class FilePickerHelper: UIDocumentPickerDelegate

swift: UIDocumentPickerViewController-plugin com.apple.UIKit.fileprovider.default invalidated

I have a problem regarding the plugin configuration.I searched but found no answers anywhere. Here is my code
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UIDocumentPickerDelegate,UITextViewDelegate {
#IBOutlet weak var webView: UIWebView!
#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:["public.data", "public.text"],inMode: UIDocumentPickerMode.Import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.FormSheet
presentViewController(documentPicker, animated: true, completion: nil)
}
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
print(url)
if controller.documentPickerMode == UIDocumentPickerMode.Open{
print("this is a Open")
}
if controller.documentPickerMode == UIDocumentPickerMode.Import{
print("this is a Import")
}
if controller.documentPickerMode == UIDocumentPickerMode.ExportToService{
print("this is a ExportToService")
}
if controller.documentPickerMode == UIDocumentPickerMode.MoveToService{
print("this is a MoveToService")
}
}
}
and also the output in the console is
this is a Import
2017-01-18 10:29:22.968 IBooksTest[442:43393] plugin com.apple.UIKit.fileprovider.default invalidated
what is wrong here or am i going in wrong direction?. i even looked at similar problem Here but still issue is same.
Thanks,

Insert an Image into PDF file Swift

I have searched a lot of pages but haven't seen any good explanation for making image to pdf. Also there are many never answered question. So it's looks helpful to ask here. The app has 'Camera', 'Photo Library' and 'Create PDF' buttons. After taken/choosing picture I want to attach this image into pdf file by 'Create PDF' button
Main Storyboard:
ViewController:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet weak var PhotoLibrary: UIButton!
#IBOutlet weak var Camera: UIButton!
#IBOutlet weak var ImageDisplay: UIImageView!
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.
}
//MARK: Photo Library Button Action
#IBAction func PhotoLibraryAction(sender: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
//MARK: Camera Button Action
#IBAction func CameraAction(sender: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
//MARK: Adding Texts on Selected/Taken Image
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageDisplay.image = image
}
dismissViewControllerAnimated(true, completion: nil)
}
}
Use the following function to convert your image into pdf
func createPdfFromView(imageView: UIImageView, saveToDocumentsWithFileName fileName: String)
{
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, imageView.bounds, nil)
UIGraphicsBeginPDFPage()
let pdfContext = UIGraphicsGetCurrentContext()
if (pdfContext == nil)
{
return
}
imageView.layer.renderInContext(pdfContext!)
UIGraphicsEndPDFContext()
if let documentDirectories: AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first
{
let documentsFileName = (documentDirectories as! String) + ("/\myPDFImage.pdf")
debugPrint(documentsFileName, terminator: "")
pdfData.writeToFile(documentsFileName, atomically: true)
}
}
I create pdf of a view, so I pass a UIView to this function. Pass your UIImageView to this function and it should work fine.

Why is my data is not writing to Firebase - App crashes due to setObjectForKey: key cannot be nil

for the past two weeks i've been having a struggle to write data to firebase, although the tutorials seem so simple :/....My app has a view controller and a tableview controller. users can add an event Title, event date and description (strings) and add a picture as well. Whenever i try to run the simulator it crashes on me! See code below. Please please help me out this is really getting frustrating.. Even if I place breakpoints in the code on top it crashes.. If someone can help me in a chat that would be awesome or maybe send the file to cause its been too long seriously I need an answer so i understand the problem.
import UIKit
import Firebase
import FirebaseDatabaseUI
class EventViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//outlets for text & image
#IBOutlet weak var photoImageView: UIImageView!
#IBOutlet weak var eventName: UITextField!
#IBOutlet weak var eventDate: UITextField!
#IBOutlet weak var eventDes: UITextView!
//Database connection
let rootref = FIRDatabase().reference()
var imagePicker: UIImagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func submitEvent(sender: AnyObject) {
let name = eventName.text
let date = eventDate.text
let text = eventDes.text
var data: NSData = NSData()
var user = NSDictionary()//declare here
if let image = photoImageView.image {
data = UIImageJPEGRepresentation(image,0.1)!
}
let base64String = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
if let unwrappedName = name , unwrappedDate = date, unwrappedText = text{
//use your declared dictionary
user = ["name":unwrappedName, "date":unwrappedDate, "text":unwrappedText, "photoBase64":base64String]
}
//Add firebase child node
//let event = FIRDatabase().reference().child(name!)
//Do not create one more reference to database
rootref.child(name!)
rootref.child(name!).setValue(user)
navigationController?.popViewControllerAnimated(true)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches, withEvent: event)
}
//UIImagePickerControllerDelegate methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
photoImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func addPicture(sender: AnyObject) {
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
} else {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
imagePicker.delegate = self
presentViewController(imagePicker, animated: true, completion:nil)
}
}
}

How can I save the videos that I'm recording?

I'm having trouble saving the videos that I'm recording. I can shoot, but I can not save them, just want to film and record videos in the library.
Can anyone help me?
//My viewController
import UIKit
import MediaPlayer
import MobileCoreServices
import AVFoundation
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate {
let captureSession = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?
override func viewDidAppear(animated: Bool) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
println("captureVideoPressed and camera available.")
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera;
imagePicker.mediaTypes = [kUTTypeMovie!]
imagePicker.allowsEditing = false
imagePicker.showsCameraControls = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
else {
println("Camera not available.")
}
}
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.
}
func imagePickerController(picker: UIImagePickerController!, info: NSDictionary!) {
let tempImage = info[UIImagePickerControllerMediaURL] as! NSURL!
let pathString = tempImage.relativePath
self.dismissViewControllerAnimated(true, completion: {})
UISaveVideoAtPathToSavedPhotosAlbum(pathString, self, nil, nil)
}
}
//My Library
import UIKit;
import MobileCoreServices
class LibraryViewController: UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate {
#IBOutlet weak var myRecord: UIImageView!
#IBAction func aBottonPlay(sender: AnyObject) {
var catchVideo = UIImagePickerController()
catchVideo.delegate = self
catchVideo.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
catchVideo.mediaTypes = [kUTTypeMovie]
self.presentViewController(catchVideo, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
myRecord.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
}

Resources