geNia
geNia

Reputation: 1025

iOS open attached file in an app. No such file or directory

In my iOS application I want the user to be able to export and import settings files. I am trying to implement the import feature at this moment.

I've send myself an email with the settings file (created in Ubuntu with UTF8 extension, if that matters)

I have defined my file extension (.inv) in pList like this:

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

I've overwrote methods in 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
}

And made a method to read the file in my 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)
    }
}

But when I try to read the file it says that the file does not exist:

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

Upvotes: 0

Views: 3115

Answers (1)

rmaddy
rmaddy

Reputation: 318804

Your code to get the file path isn't correct. Replace lines like this:

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

with:

let path = file?.path

FYI - never use the description method for anything other than debugging and logging.

Disclaimer - I'm not fluent in Swift so you may need to tweak my answer with a ! or ? somewhere in there.

Upvotes: 3

Related Questions