Text file problem

So, I am creating an import system to bring text files from email to an application in order to read their contents. I am very new to swift and application programming (the backend does it mostly) and I am having a problem with the code below. This is most likely very inefficient and there is probably a better way to do it, but I currently have func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

some other code with some other code to assign variables to the url to send to the view controller (didn't work with notifications / rootviewcontrollers yet). however, after running this code, the result is ("matrixFile4197009889-26.text", Unicode (UTF-8)) instead of the file content. What should I do? Please explain in "children's language".

My view controller code:

let delegate = UIApplication.shared.delegate as! AppDelegate
    if delegate.importFileIndicator == true {
        let filemgr = FileManager.default
        let docsDirURL = try! filemgr.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let inboxURL = docsDirURL.appendingPathComponent("Inbox")
        print(inboxURL)
        do{
            var directoryContents = try FileManager.default.contentsOfDirectory(at: inboxURL, includingPropertiesForKeys: nil, options: [])
            var fileSearchBoolCounter = false
            var fileSearchCounter = 0
            var fileURL: URL
            while fileSearchBoolCounter == false {
                if (String(describing: directoryContents[fileSearchCounter].lastPathComponent).range(of: String(describing: NSURL(string: delegate.urlString)!.lastPathComponent!)) != nil) {
                    fileURL = directoryContents[fileSearchCounter]
                    fileSearchBoolCounter = true
                    print(fileURL)
                    let path = inboxURL.appendingPathComponent((NSURL(string: delegate.urlString)?.lastPathComponent!)!)

                    encryptedMessageField.text = try String(contentsOfFile: String(describing: path), encoding: String.Encoding.utf8)
                }else{
                    print(directoryContents[fileSearchCounter])
                    fileSearchCounter += 1
                    print(NSURL(string: delegate.urlString)!.lastPathComponent!)
                }
            }

            delegate.importFileIndicator = false
            fileSearchBoolCounter = false
            fileSearchCounter = 0
        }catch let error as NSError{
            print(error)
        }

    }

      

My AppDelegate code:

var importFileIndicator = false
var urlString = ""

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    urlString = String(describing: url)
    print(urlString)
    importFileIndicator = true
    return true
}

      

+3


source to share


1 answer


I think you already have a good relationship with some parts, but I'm going to include them throughout the process too.

1. Make your application available to open TXT file

In order for the system to know that your application is ready to receive the TXT file, you need to configure it yourself Info.plist

, or the easiest way is to configure it via TARGETS/"Info tab"/"Document Types section"

:

enter image description here

At this point, your application will become available to process TXT files coming from other external applications. Therefore, when you are about to open the TXT file attached to the mail, you should see your application listed:

enter image description here

2. Prepare your application to receive incoming TXT file

In order to handle a supported file type, you need to implement the method application:openURL:options:

you already mentioned in AppDelegate

. Here you will get the path to the file as a url, which you can easily send to the url ViewController

for further processing. This url should look something like this:



(lldb) po urlfile:///private/var/mobile/Containers/Data/Application/42D78E58-C7EC-4F3B-9100-B731AF7A4E45/Documents/Inbox/sample.txt

      

3. Process TXT file

Here, you can also save the contents of the file to String

using the appropriate initializer String

.

String(contentsOf: url, encoding: String.Encoding.utf8)

      

and then you can pipe that String

to ViewController

.

So yours application:openURL:options:

in yours AppDelegate

should look something like this (depending on your actual controller hierarchy):

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    do {
        let contentString = try String(contentsOf: url, encoding: .utf8)

        if let window = self.window, let viewController = window.rootViewController as? ViewController {
            viewController.displayText(text: contentString) 
        // here you pass the actual content as String to your custom ViewController which implements a displayText: function that receives a string
        }
    }
    catch {
        // contents could not be loaded
    }

    return true
}

      

0


source







All Articles