Application Identifier NSSearchPathForDirectoriesInDomains Changes for each start and update of the simulator on a real device

I would like to save the images in the domain of iPhone users, so I will write the following code.

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

It shows how: /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 identifier A2850237-5E71-4373-81A6-B443032E1951

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

In this case, the application identifier is 1F9B5B0A-5A6C-4098-BF40-C978C60C93AF

Thus, there is a difference in the application ID between the previous and current installation, although I just updated the application and did not delete the application. Why is this caused and how to fix it?

It calls Xcode 7.2, 7.1, 7.0. And this is due not only to the installation of the simulator, but also to the actual installation of the device. Therefore, if iOS users update the application from the application store, the Application ID will be changed and the application sandbox will also be changed, and finally, users will not be able to link to their images.

Similar situations:

Relevant recommendations:

Thanks in advance.

EDIT

It seems that I need to continue the path as relative, not absolute.

I will try the approach, and if I solve my problem, I will update the question.

+5
source share
3 answers

I need the relative path not to be absolute. And I can also save the old startup images by selecting NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] + "persisted relative path" .

+2
source

The best solution is to save 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 the file. While the path and file of the URLs are potentially vulnerable between runs of your application, bookmarks can usually be used to recreate the URLs for the file even when the file has been moved or renamed

You must use NSURL instead of String first

Convert NSURL to NSData p>

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

Reading NSURL from the NSData bookmark p>

  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 in relativeToURL in your url document directory

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

0
source

try to save your file with only a name. extension

to save the 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 a 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`.... 
-1
source

Source: https://habr.com/ru/post/1241230/


All Articles