文本文件内容问题

时间:2017-04-07 01:10:09

标签: ios swift

所以,我正在制作一个导入系统,将来自电子邮件的文本文件带到应用程序中以读取内容。我对swift非常新,并且应用程序编程(主要是后端),我遇到了下面代码的问题。它很可能是非常低效的,并且可能有更好的方法来执行此操作,但是目前我有func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool和其他一些代码将变量分配给URL以发送给视图控制器(避难所没有工作)还有通知/ rootviewcontrollers)。但是,在运行此代码之后,结果而不是文件的内容是(" matrixFile4197009889-26.text",Unicode(UTF-8))。我该怎么办?请用婴儿语言解释。"

我的视图控制器代码:

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

    }

我的AppDelegate代码:

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
}

1 个答案:

答案 0 :(得分:0)

我认为你已经对某些部分已经很好了,但我也会在整个过程中包含它们。

<强> 1。让您的应用可用于打开TXT文件

要让系统知道您的应用已准备好接收TXT文件,您需要配置Info.plist本身,或者最简单的方法是通过TARGETS/"Info tab"/"Document Types section"进行配置:

enter image description here

此时,您的应用程序可用于处理来自其他外部应用程序的TXT文件。因此,当您即将打开附加到邮件的TXT文件时,您应该会在列表中看到您的应用:

enter image description here

<强> 2。准备应用程序以接收传入的TXT文件

为了处理支持的文件类型,您需要实施application:openURL:options:中已经提到的AppDelegate方法。在这里,您会收到文件路径作为网址,您可以轻松发送到ViewController进行进一步处理。此网址应如下所示:

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

第3。处理TXT文件

您还可以使用相应的String初始化工具将文件的内容存储在String中。

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

然后您可以将String传递给ViewController

因此application:openURL:options:中的AppDelegate应该看起来像这样(取决于您实际的视图控制器层次结构)

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
}