2015-12-09 24 views
0

在我的iOS應用程序中,我希望用戶能夠導出和導入設置文件。 我正試圖在這一刻實現導入功能。iOS在應用程序中打開附加文件。沒有這樣的文件或目錄

我已經給自己發送與設置文件的電子郵件(在Ubuntu與UTF8擴展創建的,如果該事項)

我在plist中定義我的文件擴展名(.inv)是這樣的:

<key>CFBundleDocumentTypes</key> 
<array> 
    <dict> 
     <key>CFBundleTypeIconFiles</key> 
     <array/> 
     <key>CFBundleTypeName</key> 
     <string>Stratix Settings File</string> 
     <key>CFBundleTypeRole</key> 
     <string>Editor</string> 
     <key>LSHandlerRank</key> 
     <string>Owner</string> 
     <key>LSItemContentTypes</key> 
     <array> 
      <string>com.invera.settings</string> 
     </array> 
    </dict> 
</array> 
<key>UTExportedTypeDeclarations</key> 
<array> 
    <dict> 
     <key>UTTypeConformsTo</key> 
     <array> 
      <string>public.data</string> 
     </array> 
     <key>UTTypeDescription</key> 
     <string>Stratix Settings File</string> 
     <key>UTTypeIdentifier</key> 
     <string>com.invera.settings</string> 
     <key>UTTypeTagSpecification</key> 
     <dict> 
      <key>public.filename-extension</key> 
      <string>inv</string> 
     </dict> 
    </dict> 
</array> 

我已經重寫了方法的AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    if launchOptions != nil { 
     let options = launchOptions! as NSDictionary 

     let file = options.objectForKey(UIApplicationLaunchOptionsURLKey) as! NSURL? 

     if file != nil { 
      let path = "" + (file?.filePathURL?.description)! 

      ((self.window?.rootViewController as! UINavigationController).visibleViewController as! LoginViewController).readSettingsFile(path) 
     } 
    } 

    return true 
} 

func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool { 
    let path = "" + (url.filePathURL?.description)! 

    ((self.window?.rootViewController as! UINavigationController).visibleViewController as! LoginViewController).readSettingsFile(path) 

    return true 
} 

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { 
    let path = "" + (url.filePathURL?.description)! 

    ((self.window?.rootViewController as! UINavigationController).visibleViewController as! LoginViewController).readSettingsFile(path) 

    return true 
} 

並提出一種方法來讀取我的ViewController文件:

func readSettingsFile(filePath : String) { 
    NSLog("File path : " + filePath) 
    do{ 
     let content = try String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding) 
     NSLog(content) 
    } catch let error as NSError { 
     print(error) 
    } 
} 

但是當我嘗試讀取它說,該文件不存在的文件:

File path : file:///private/var/mobile/Containers/Data/Application/E7076F5B-9131-4253-BAE7-0053CC872C2B/Documents/Inbox/settings-32.inv 

Error Domain=NSCocoaErrorDomain Code=260 "The file 「settings-32.inv」 couldn’t 
be opened because there is no such file." UserInfo={ 
NSFilePath=file:///private/var/mobile/Containers/Data/Application/E7076F5B-9131-4253-BAE7-0053CC872C2B/Documents/Inbox/settings-32.inv, 
NSUnderlyingError=0x14ed7760 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 
+0

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/instp/NSURL/URLByStandardizingPath –

回答

1

你的代碼來獲取文件路徑是不正確的。這樣的替換線:

let path = "" + (file?.filePathURL?.description)! 

有:

let path = file?.path 

僅供參考 - 從來沒有使用description方法比調試和記錄的任何其他。

免責聲明 - 我不是流利的斯威夫特,所以你可能需要調整我的答案,!?某處。

+0

這是它!謝謝! – geNia

相關問題