Xcode 6 keeps renaming my app's directory in iOS8 simulator after each run.
Asked Answered
A

6

73

I'm running Xcode 6 Beta 5 but this has been happening since the first beta. My app's directory in the simulator keeps being renamed after each run. It took me a while to figure this out. I'm using this to get the doc's dir reference.

NSString *folder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                        NSUserDomainMask,
                                                        YES) lastObject];

NSLog(@"Documents Dir: %@",folder);

Now for example on the first run it'll be:

/Users/Joey/Library/Developer/CoreSimulator/Devices/5B9930EE-A9B4-4B36-BABB-AA864ACAF2DE/data/Containers/Data/Application/4B10C2E4-A5C3-4C64-93B1-4069FCCB9C46/Documents

Second run now it's:

/Users/Joey/Library/Developer/CoreSimulator/Devices/5B9930EE-A9B4-4B36-BABB-AA864ACAF2DE/data/Containers/Data/Application/7E9EB62D-115A-4092-AD23-CB6BA3E5E10F/Documents

Third run:

/Users/Joey/Library/Developer/CoreSimulator/Devices/5B9930EE-A9B4-4B36-BABB-AA864ACAF2DE/data/Containers/Data/Application/EC8F41E8-52ED-4B10-9808-B3ACC46FC6AA/Documents

This is wreaking havoc with my app because it stores path references for certain files within the app. It's not that my NSLog statement is returning incorrect results, I verified this is what happening in Finder. It's changing the name every time. Has anyone seen this happen? Is this a "feature" that I'm misunderstanding?

Apologete answered 10/8, 2014 at 21:55 Comment(6)
Never store absolute paths. Only store the path relative to the Documents folder.Gymnastic
Well I guess that's a lesson learned.Apologete
The path will change on users' devices too whenever they update to a newer version of your app.Gymnastic
lesson learned the hard wayTortuga
I found even the app does not exit (i mean force to quit) and is put into background, the value could changes too :)Parabolize
How is NSString *folder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; considered an "absolute path"? Looks dynamic, what am I missing?Lentissimo
A
50

Turns out Xcode 6 does in fact change the app's UUID every run, and I'm in the wrong for storing absolute paths.

Apologete answered 11/8, 2014 at 2:33 Comment(7)
Not just Xcode; the same happens when you download an updated version of the app from the store (the HEX part changes). All paths should be stored relative to app folder, and full path reconstructed at runtime (this one actually bit me once!)Denicedenie
Nicolas, is this behavior (for device installs/upgrades) new since iOS 8?University
I needed to have a shortcut to quickly access this directory for debugging purposes on the simulator. I tried a few over complicated ways of digging through the xcodeproj file to find the last build dir. That didn't work. I plan to have the app (when running in the simulator only) cache its documents path to some easily accessible file, then when I need to open my app's documents dir in Finder, I'll just link it to the documents path cache file that it created on last run. This way it will always point to the app's document directory even though the app guid changes each run.Sadowski
OTA installs (by any method, TestFlight, AppStore, or other) have always changed the UUID. It's just that cable loading before xCode 5 retained the same UUID.Horacehoracio
@NicholasMiari, how does one create a path relative to app folder then?Lode
How to use relative path in this, by the way?Amido
@Sadowski A little late in the game but this is still useful. If you drag a reference to the parent of the "Documents", "Library" folder to the sidebar in Finder, then you can always go back to it quickly. The sidebar reference also changes when the main folder changes upon each app launch in the simulator. It's not pretty - but it's functional.Phantasmal
W
8

USE SIMPHOLDERS

I used this app on Xcode 5 opens the Documents folder, for the currently running app in the simulator, in Finder.

http://simpholders.com/

not ready for Xcode 6 yet (as of sep 24 2014) but saves all this hassle.

In Xcode 6 / iOS8 The bundle is now separate from the data./ The application GUID is regenerated between runs in Xcode (not sure why)

  DOCUMENTS DIR:/Users/gbxc/Library/Developer/CoreSimulator/Devices/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Data/Application/C220D351-0BE7-46BA-B35E-D16646C61A3F/Documents
mainBundlePath_:/Users/gbxc/Library/Developer/CoreSimulator/Devices/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Bundle/Application/12200D1D-9B67-408B-BCF7-38206CBE0940/myappname.app/BLANK_BLOG_SCALED.jpg

1. FIND THE DEVICES FOLDER in SIMULATOR

/Users/gbxc/Library/Developer/CoreSimulator/Devices/

open each /device.plist to see which GUID is which device in XCode - I think this is static

3. FIND THE DEVICE you're running on iPad 2 - I think this is static

/Devices/AC79941F-EC56-495E-A077-773EEE882732

4. Find your application /Documents folder

/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Data/Application/C220D351-0BE7-46BA-B35E-D16646C61A3F/Documents

BEWARE the GUID C220D351-0BE7-46BA-B35E-D16646C61A3F is regenerated everytime the app is run in XCode 6

 NSArray *paths_ = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        if(paths_){
            _docsDir = [paths_ firstObject];
            DebugLog(@"DOCUMENTS DIR:%@",_docsDir);         
        }else{
            ErrorLog(@"paths_ is nil - cant get Documents directory");
        }

MAIN BUNDLE path

NSString *mainBundlePath_ = [[NSBundle mainBundle] pathForResource:@"someimageinyourbundle" ofType:@"jpg"];
/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Bundle/Application/12200D1D-9B67-408B-BCF7-38206CBE0940/clarksonsiq.app/BLANK_BLOG_SCALED.jpg

NEVER CACHE THE PATH to /Documents between runs it will change.

I was serializing it to a plist and couldnt figure out why they kept disappearing

The GUID above /Documents keeps changing between runs but if you have /Documents open in Finder the folder stays open.

https://devforums.apple.com/thread/235911?tstart=0

https://devforums.apple.com/thread/238754?tstart=0
Wyrick answered 24/9, 2014 at 10:12 Comment(2)
I am using simPholders but for xcode 6 its terror to find application any other option?Depredate
Anyone know of an alternative to Simpholders? It always just hangs on launch for me :(Megawatt
D
7

Free Solution

Use Open Source Library OpenSim. OpenSim is an open source alternative for SimPholders, written in Swift.

Paid Solution

Use SimPholder application to know current application location.

enter image description here

For xcode 6.0 >

Download SimPholder 2.0 alpha 2


For xcode 5.1 <

Download SimPholders 1.5

Depredate answered 20/10, 2014 at 15:14 Comment(2)
Yes, I've been using it and it rocks. I just wish it worked with my iOS7 simulators as well.Apologete
Can you explain how this fixes/helps with the problem described in the question?Sanctum
S
1

I can confirm that this is Xcode 6 related not iOS 8.

I have two development machines. On one of them I have Xcode 5. I was working all the time on that machine and my URL's were fine (photo app, photos are visible).

Yesterday I checked in form git my source on a machine with Xcode 6. I noticed that my photos are not visible any more, only photos that are created during that app session.

After little debugging, I realized that file:///var/mobile/Applications/B6A6BAEF-C90C-4A2A-93DB-E6700B88971F/Documents/ is changing on every app run.

All that time I am working with iOS 7 device.

I am going to check once more on a machine with Xcode 5 to confirm when I get my hands on it.

Syrup answered 23/11, 2014 at 13:26 Comment(0)
E
1

You need to to save only path inside DocumentDirectory(directory/file name), and add it to the DocumentDirectory every time you load the file...

-(void)saveImage:(UIImage *)image{
NSData *pngData = UIImagePNGRepresentation(image);
NSString *pathInDocumentDirectory = [APP_DocumentDirectory stringByAppendingPathComponent:PROFILE_IMAGE_NAME];
NSString *filePath = [self documentsPathForFileName:pathInDocumentDirectory];
//Save pic file path - DirName/Filename.png
[XYZPreferencesHelper setUserImageFilePath:pathInDocumentDirectory];
 //Write the file
[[NSFileManager defaultManager] createFileAtPath:filePath contents:pngData attributes:nil];

}



-(void)loadSavedUserPicture{
//Load saved DirName/Filename.png
NSString *pathInDocumentDirectory = [XYZPreferencesHelper getUserImageFilePath];

if (pathInDocumentDirectory != nil){
    //Full path with new app Document Directory
    NSString *filePath = [self documentsPathForFileName:pathInDocumentDirectory];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        NSData *pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *image = [UIImage imageWithData:pngData];
        if (image != nil){
            userPicImageView.image = image;
        }

    }

 }
}



- (NSString *)documentsPathForFileName:(NSString *)name
{
NSString *documentsPath = [self createRestcallDirectoryIfNotExist];
return [documentsPath stringByAppendingPathComponent:name];
}



-(NSString *)createRestcallDirectoryIfNotExist{
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
path = [documentsPath stringByAppendingPathComponent:APP_DocumentDirectory];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
{
    if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error])
    {
        NSLog(@"Create directory error: %@", error);
    }
}
return documentsPath;
}
Engadine answered 29/11, 2016 at 9:20 Comment(0)
A
0

Because app's UUID changes and not reliable 
so we should not store urls instead we should store just file names and reconstruct url at runtime , on update/reinstall iOS creates new home directory, stores app bundle in it and copies documents files so url changes 


Aman answered 23/12, 2021 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.