Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
iOS Forensics Cookbook

You're reading from  iOS Forensics Cookbook

Product type Book
Published in Jan 2016
Publisher
ISBN-13 9781783988464
Pages 184 pages
Edition 1st Edition
Languages
Concepts
Authors (2):
Bhanu Birani Bhanu Birani
Profile icon Bhanu Birani
Mayank Birani Mayank Birani
Profile icon Mayank Birani
View More author details

The Documents directory


Our app only runs in a "sandboxed" environment. This means that it can only access files and directories within its own contents, for example, Documents and Library. Every app has its own document directory from which it can read and write data as and when needed. This Documents directory allows you to store files and subdirectories created by your app. Now, we will create a sample project to understand the Document directories in much more depth.

Getting ready

Open Xcode and go to File | New | File and then navigate to iOS | Application | Single View Application. In the popup, provide the product name DocumentDirectoriesSample.

How to do it...

Perform the following steps to explore how to retrieve the path of document directories:

  1. First, we will find out where in simulators and iPhones our document directories are present. To find the path, we need to write some code in our viewDidLoad method. Add the following line of code in viewDidLoad:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"%@", documentsDirectory);

    In the preceding code, first we fetch the existing path in our path's array. Now, we will take the first object of our path array in a string that means our string contains one path for our directory. This code will print the path for document directory of the simulator or device wherever you are running the code.

  2. To create the folder in documentsDirectory, run the following code:

    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

    In the preceding code snippet, the [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"] line will create our folder in the Document directory and the last code of NSFileManager will check whether that dataPath exists or not; if it does not exist, then it will create a new path.

  3. Now, compile and run the project; you should be able to see the path of Document directories in your project console. This should look like the following screenshot:

  4. Now, go to Finder; from the options, select Go to Folder and paste the documentsDirectory path from the console. This will navigate you to the Documents directory of the simulator. The Documents directory will look like the following screenshot:

You have been reading a chapter from
iOS Forensics Cookbook
Published in: Jan 2016 Publisher: ISBN-13: 9781783988464
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}