2017-04-07 54 views
3

因此,我製作了一個導入系統,將電子郵件中的文本文件帶入應用程序以讀取內容。我對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 
} 
+0

1.你不想使用'absoluteString'上'url'。這是一個文件URL。只需使用網址就可以了。 2.如果'urlString'有一個空的前綴,看看有什麼意義?所有字符串都會匹配。 3。'urlString'已經是'String'了。 'urlStringVar'變量的意義是什麼? – rmaddy

+0

@rmaddy我更正了所有這些東西,但我仍然得到相同的結果 –

+0

更新您的問題以顯示您的最新代碼和問題。 – rmaddy

回答

0

我想你已經有一些地方已經很好了,但我會包括他們太全處理。

1.您的應用打開TXT文件

爲了讓系統知道你的應用程序準備接收一個TXT文件,你需要配置Info.plist本身,或者最簡單的方法是通過TARGETS/"Info tab"/"Document Types section"配置:

enter image description here

在這一點上你的應用程序可用來處理TXT文件從其他外部應用程序來。所以,當你要打開的是附加在郵件中的TXT文件,你應該可以看到你的應用程序列表:

enter image description here

2.準備應用程序接收傳入的TXT文件

爲了處理支持的文件類型,您需要實現AppDelegate中已經提到的application:openURL:options:方法。在這裏,您接收到的文件路徑爲url,您可以輕鬆發送到您的ViewController進一步處理。此URL應該看起來是這樣的:

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

,然後可以傳遞StringViewController

所以你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 
}