3

I'm creating an app that gets a file from Icloud and converts it into a b64 format string, but I have a problem:

I really don't know how to get the data from this file. I thought that could be easy opening the Path from the imported file from ICloud but It returns nil when I try to acess to the route.

My code example is here, as you can see I have the temp route :( (file:///.../Aplication/xxx-xxx-xxx-xx/temp/com.domain.AppName/Carolina.cer):

extension KeyViewController: UIDocumentMenuDelegate {

    func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        print ("documentMenu")
        self.presentViewController(documentPicker, animated: true, completion: nil)
    }
}
extension KeyViewController: UIDocumentPickerDelegate {

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    print("url string")
    print(String(url))
//prints file:///.../Aplication/xxx-xxx-xxx-xx/temp/com.domain.AppName/Carolina.cer

if controller.documentPickerMode == UIDocumentPickerMode.Import {
            if (NSFileManager.defaultManager().fileExistsAtPath(String(url))){
                    print("exists")
            }
            else{
                    print("not exists")
            }
        dispatch_async(dispatch_get_main_queue()) {
            if let fileName = url.lastPathComponent {
                var fileNameArray = fileName.componentsSeparatedByString(".")
                print ("fileNameArray")
                print (fileNameArray)
                if ((fileNameArray[1] != "cer") && (fileNameArray[1] != "key")) {
                    self.alertMessage = "Not a valid selection, choose .cer or .key Files"
                    self.showAlertMessage(self.alertMessage)
                }
                else{
                //saving the name
                    if (fileNameArray[1]=="cer")
                    {
NSUserDefaults.standardUserDefaults().setObject(fileName, forKey: "temporalCerFile")
                    }
                    else{
                    //saving the name
NSUserDefaults.standardUserDefaults().setObject(fileName, forKey: "temporalKeyFile")
                    }
NSUserDefaults.standardUserDefaults().synchronize()
                    }

                }

            }

        }
    }
}

How can I get the content inside the file?. Please, hope you can help me

Elizabeth Hernandez
  • 149
  • 1
  • 1
  • 12
  • Where's your attempt to access the file at the given URL? The code you posted prints the URL and splits up the filename of the URL but it makes no attempt to actually access the file at the URL. – rmaddy Oct 12 '16 at 22:26
  • Edited, sorry I forgot to paste that part. Thanks – Elizabeth Hernandez Oct 12 '16 at 22:33
  • The updated code you posted still makes no attempt to access the file. All you have so far is a check to see if it exists. And the code you posted won't even compile since you are trying to pass an `NSURL` to a method that expects a `String`. – rmaddy Oct 12 '16 at 22:37
  • That's the point I really don't know how to access the file(I'm starting with iOS), so I've tried to access the path first if exists but it doesn't. If that path exists I think I've to use let document = File.open(path) and then something like while var line = document.readline() – Elizabeth Hernandez Oct 12 '16 at 22:48
  • try this solution https://stackoverflow.com/a/50397424/2171764 – Jhonattan May 17 '18 at 17:38

1 Answers1

0

Your code isn't working because you are passing the incorrect value to fileExistsAtPath:.

The proper way to convert a file NSURL to a file string is to use the path() method.

if (NSFileManager.defaultManager().fileExistsAtPath(url.path())) {

It might be url.path instead of url.path(). I'm not sure about the older Swift syntax. In Swift 3 it would be:

if FileManager.default.fileExists(atPath: url.path) {

Once you know it exists, that are many ways to load the file. It all depends on what you need to do with it. But the first requirement is to move the file from the given url to a location inside your app's sandbox. At least if you need the file to survive the next use of your app. If you just need to look at the file this one time, you don't need this step.

rmaddy
  • 314,917
  • 42
  • 532
  • 579