Does unique app id could change on device? - ios

I have a coredata DB which store pdf absolute path for files.
I discovered that my pdf could not be loaded, after investigation it is because the path for Documents folder has changed becaue App's unique id has changed.
Was /var/mobile/Applications/FEF0A8B5-6FA3-47DA-98EC-732A3FFA6FD3/Documents/
And is now: /var/mobile/Applications/17525DA9-C2C5-4515-9635-0ECC76BE9131/Documents/
Could this happens?
In what situation?
Thanks a lot.
Thierry

When you delete the app, the next time that value will be different. You can use:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

Related

What happens to files stored in the documents directory when an app is updated?

I couldn't find the answer to this anywhere but I am saving various files in my app to the documents directory using archiving. If I release an update for the app and the user installs it what happens to these files? Will they be deleted?
code to get to documents:
//get the directory
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//set it to a string
NSString *documentDirectory = [documentDirectories firstObject];
return [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#stickers.archive",[PFUser currentUser].username]];
No, the files will remain.
You can then go ahead and change them as part of the update or just use them like before.
But you should be careful how you reference the saved files, since they will be in a new directory after the update: App Updates, NSURL, and Documents Directory Basically you should only save the relative path from the documents directory, that will remain unchanged as well.

How to clear the content of .log file programmatically in iOS

In my application, i have logged the details of the application in project.log with the help of following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:#"project.log"];
freopen([logPath fileSystemRepresentation],"a+",stderr);
This code will perfectly log all the details that i defined in nslog. Now i don't want my log file to grow much further. So i want to clear the content of log files that was logged before 1 week or more. But i need the content of log for last 7 days. Is there any possible solution to accomplish the above?
There is an easy solution. It's not exactly what you have asked, but maybe it's valuable to you.
On startup, do this:
check if project.log exists and is greater than (e.g.) 2MB.
If no, just use project.log.
If yes: 1) remove project.old.log if it exists. 2) rename project.log to project.old.log 3) log to project.log (it will be created if you open with "a+")

NSFileManager UUID app directory

I'm using NSFileManager and i'm trying to copy a file to path "/var/mobile/Applications/7AC2295E-2775-41EA-B017-AB4048A09F0C/Document" the file will copy fine.
but the path of "7AC2295E-2775-41EA-B017-AB4048A09F0C" is randomly changed in every time i delete and install the app again. So, is there a way to get the correct path of my app or search for file name, If file exist then replace\delete..etc the file? thanks alot.
The proper way to get access to the Documents directory in your app's sandbox is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
Then you create your path:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"filename.ext"];
You don't have to use the absolute path, use something like
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"test" ofType:#"txt"] toPath:[documentsDirectory stringByAppendingPathComponent:#"test.txt"] error:nil];
This demonstrates copying the file test.txt from the apps bundle to the documents directory.
Do something like this to create a file test.txt
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"test.txt"] contents:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://a-cstudios.com/text.json"]] attributes:nil];
EDIT
If your using this and want to use it on a jailbroken device, to put something in the sandbox is the same as I said before, so you still don't have to use the UUID. If you want to put something in a system directory, that is when you would use something like /usr/include/, or wherever you want to. To put something in another apps sandbox, you would have to use the complete path, and you would have to know the UUID, as thats not part of your sandbox. So, unless you're copying a file to an app that you made, you have to use the complete path.
If your application needs to write a file to its Documents directory.
use something like :
NSString *filePath = NSHomeDirectory();
then append your actual file name to filePath.
then use filePath in your FileManager calls.

how to filter part of a path in iOS?

we have an app released, which wasn't held for developer release. Our customer released the app instead of waiting for us changing the backend for the new version. This now leads to wrong information at a local CoreData Storage, where the problem begins.
There seems to be a field holding a local path to a file on Documents directory like:
/var/mobile/Applications/9EE88C79-F060-4EE....C0A/Documents/core23/thumb.png
The folder where the App lives in on local device changes during the update of an app (looks like!?). Now we need to filter this field to change the path before /Documents/to the actual path where the app is installed on local device.
Does somebody have a handy solution for that? Any help is appreciated.
MadMaxApp
-[NSString pathComponents] returns an array of all path components. Call that on your errant path string and build it back up from the resulting array.
-[NSString componentsSeparatedByString:(NSString*)] would also work just as well if passed #"/" for the separator string.
You can't hard code that path. To get the documents directory path use the following code:
-(NSString *)documentsDirectoryPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return documentsDirectoryPath;
}

Managing files in iOS5

I want to handle files in my iOS App.
My app should create a file with a custom suffix (e.g. file.mysuff) and save it to the device so I'll be able to copy it using iTunes File Sharing.
Then I want to be able to attach that file to a new mail.
When the receiver opens the document, mail should launch my app and handle that file.
Are there good tutorials on that topic?
I'm still quite new to cocoa / cocoa touch so it should be easy to my.
Is maybe a wrapper out there that I could implement so I just have to code something like
[self [saveMyFile path:[NSURL] contents:[NSString]]]??
Thanks for help!
Greets, J.
This is one example of how you can save the file to documents on the iPhone to use later. This stores a dictionary from a list, changes a value and then writes the updates dictionary back to the file specified. Let me know if this is what you were looking for.
//user document directory and instantiate dictionary
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *plistFilePathInDocumentsDirectory = [documentsDirectoryPath stringByAppendingPathComponent:#"YourFile"];
NSMutableDictionary *yourList= [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilePathInDocumentsDirectory];
//save the new information to the plist in the user documents directory
[yourList setObject:someObject forKey:someKey];
[yourList writeToFile:plistFilePathInDocumentsDirectory atomically:YES];

Resources