Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application ID of NSSearchPathForDirectoriesInDomains Changes per Simulator Run & Update App on Real Device

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:

  • File System Basics

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.

like image 949
bekkou68 Avatar asked Oct 30 '22 10:10

bekkou68


2 Answers

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

like image 98
bekkou68 Avatar answered Nov 11 '22 15:11

bekkou68


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: ..)

like image 30
phimage Avatar answered Nov 11 '22 13:11

phimage