6

I'd like to persist images to user domain of iPhone so I write the following code.

let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
print(path)

It shows like: /Users/xxx/Library/Developer/CoreSimulator/Devices/1312F880-6BDC-45D2-B3B3-4D2374313C67/data/Containers/Data/Application/A2850237-5E71-4373-81A6-B443032E1951/Documents/

In this case, Application ID is A2850237-5E71-4373-81A6-B443032E1951

And the problem is when I run on simulator again WITHOUT REMOVING THE APP, it shows like: /Users/xxx/Library/Developer/CoreSimulator/Devices/1312F880-6BDC-45D2-B3B3-4D2374313C67/data/Containers/Data/Application/1F9B5B0A-5A6C-4098-BF40-C978C60C93AF/Documents/

In this case, Application ID is 1F9B5B0A-5A6C-4098-BF40-C978C60C93AF

So there are Application ID difference between previous and current install although I just did update the app and didn't remove the app. Why it is caused and how to fix it?

It causes Xcode 7.2, 7.1, 7.0. And it causes with not only simulator install but also actual device install. So if iOS users update the app from app store, the Application ID will be changed and app sandbox will also be changed and finally users cannot refer their images.

Similar Situations:

Related Guidelines:

Thanks in advance.

EDIT

It seems I have to persist path as relative not absolute.

I'll try the approach and if I solved my problem, I'll update the question.

Community
  • 1
  • 1
bekkou68
  • 802
  • 1
  • 9
  • 20
  • Possible duplicate of [Xcode, changing applications subfolder?](http://stackoverflow.com/questions/2464832/xcode-changing-applications-subfolder) – Eric Aya Jan 22 '16 at 13:03
  • I can understand Application ID changes when I re-install (remove the existing app and install the app again) the app. But, in this case, Application ID changes when I update (NOT remove the existing app and install the app again) the app. If file container changes by updating the app, how can I keep persisted data in User Domain over next version? – bekkou68 Jan 23 '16 at 02:40

3 Answers3

3

I have to persist path relative not absolute. And I also can salvage old run images by fetching with NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] + "persisted relative path".

bekkou68
  • 802
  • 1
  • 9
  • 20
1

A better solution is to save a bookmark data https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html

A bookmark is an opaque data structure, enclosed in an NSData object, that describes the location of a file. Whereas path- and file reference URLs are potentially fragile between launches of your app, a bookmark can usually be used to re-create a URL to a file even in cases where the file was moved or renamed

First you must use NSURL instead of String

Convert an NSURL to NSData

let data: NSData? = try? url.bookmarkDataWithOptions(.SuitableForBookmarkFile, includingResourceValuesForKeys: nil, relativeToURL: nil)

Read an NSURL from bookmark NSData

    var isStale: ObjCBool = false
    let url = try? NSURL(
        byResolvingBookmarkData: bookData,
        options: [],
        relativeToURL: nil,
        bookmarkDataIsStale: &isStale)
    guard let fullURL = url else {
        return nil
    }

You can fill relativeToURL to your document directory url

Alternatively you can use FileKit : path.bookmarkData and Path(bookmarkData: ..)

phimage
  • 274
  • 1
  • 6
-1

try to persist your file just with the name.extension

to save document

    let pathDocument = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
let fullPath =  let fullPath = pathDocument+"/"+"fileName.ext"
 //...add some code to save document at `fullPath`...

to get document

    let pathDocument = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
let fullPath =  let fullPath = pathDocument+"/"+"fileName.ext"
//... add code to get data at path : `fullPath`....