0

I save a user preference for an audio file in NSUserDefaults with a URL built like this:

soundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audioFile1.m4r", [[NSBundle mainBundle] resourcePath]]];

That preference is subsequently retrieved like this:

self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.audioURL error:&error];

This has worked great and I've had no trouble until recently when the app went into adhoc deployment through TestFlight for beta testing. Now, upon reinstalls of the app, the sound doesn't play. Logging shows that the path relies on a GUID (or equivalent) that has changed with the new install:

file://localhost/var/mobile/Applications/D74D8B09-5B27-4EF9-A344-68304CFE5549/myApp.app/audioFile1.m4r

was stored in the preferences, but the URL when built in the subsequent install is:

file://localhost/var/mobile/Applications/5F607CD7-6E32-45F0-9897-0DDBACBDD6B0/myApp.app/audioFile1.m4r

So my questions are: Am I doing it wrong? If so, what is the correct way? Why does this only happen under TestFlight?

4

1 回答 1

2

You never store absolute URLs for files in your documents directory. This is because when you update an app iOS creates a new directory for that app with a different hexadecimal name. Now your absolute URL is referencing the incorrect location and won't return the proper file.

Files Saved During App Updates

When a user downloads an app update, iTunes installs the update in a new app directory. It then moves the user’s data files from the old installation over to the new app directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:

<Application_Home>/Documents

<Application_Home>/Library

Although files in other user directories may also be moved over, you should not rely on them being present after an update.

Please refer iPhoneOSProgrammingGuide for more details

于 2012-12-13T05:37:57.937 回答