How to browse the iPhone simulator - iphone

my app is creating a CSV file out of a db. I want to browse and open the file to test. How do I access the iphone simulator's storage ?

All data of the Simulator is stored as local files on your Mac.
The path for the user data of iOS apps in the Simulator is:
~/Library/Application Support/iPhone Simulator/[OS version]/Applications/[appGUID]/

You'll want to print out the location of where you store your file when you run the app in the simulator.
You can use this code to get the location of the Library/Cache folder here:
-(NSString *) mediaPathForFileName:(NSString *) fileName
{
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [directoryPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", cachesDirectory, fileName];
return filePath;
}
Pass in a random file name like "test.txt":
// somewhere in your viewDidLoad method
[self mediaPathForFileName:#"test.txt"];
This will print out the path to your app Library/Cache folder.

you need get the path of that file by using NSLog and then user Shift+Cmd+G to go to that path.

pritn this path
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

Related

How to find Pdf file in iPhone

hello i am beginner in iOS In one of my activity i want to ask .....I displayed pdf using WebView ......on screen and save this Pdf then write this type of code ........
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath =[documentsDir stringByAppendingPathComponent:#"myPDF.pdf"];// your yourPdfFile file here
NSLog(#"pdf file %#",pdfFilePath);
NSURL *url = [NSURL fileURLWithPath:pdfFilePath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController presentOpenInMenuFromRect:CGRextZero inView:self.view animated:YES];
When we run this code on simulator (using Xcode) then show path and I got this Pdf file successfully in documents folder .......
but when we run this code in iPhone device then I got this type of path......
/var/mobile/Applications/D33A80AA-C0AD-4211-ADE3-4906372CDA40/Documents/myPDF.pdf
So I don't know where is my Pdf in iPhone device and how to got this Pdf and open .....when i want ......
If your PDF is in the documents folder, you should retrieve the path like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
Don't try to use anything related to the absolute path as you see it in the simulator.
One reason you might not find a file on the device where you do find it on the simulator is that the simulator is case-insensitive but the device is case-sensitive, so be sure your names match exactly.
But! Be aware that the Documents folder is now perhaps not where you want to be storing things - unless you are sure you want them backed up with iCloud. The File System Programming Guide says to use Documents ONLY for documents that cannot be recreated by the app, "critical" documents. A lot of the time it makes sense to store them in the user Library folder instead. In that case the arguments to NSSearchPathForDirectoriesInDomains would be:
(NSLibraryDirectory, NSUserDomainMask, YES)

how to access my iphone's log file on the iphone itself

So my app keeps on crashing and losing all the logs outputed in console. I decided to jailbreak the phone, and use this code to write to a file:
+(void)Log:(NSString *)content
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* docDir = [paths objectAtIndex:0];
NSString * logFile = [docDir stringByAppendingString:#"/log.txt"];
NSData *dataToWrite = [content dataUsingEncoding: NSUTF8StringEncoding];
NSFileHandle* outputFile = [NSFileHandle fileHandleForWritingAtPath:logFile];
[outputFile seekToEndOfFile];
[outputFile writeData:dataToWrite];
[outputFile closeFile];
}
if I put a break point, the value of outputFile looks like this:
/var/mobile/Applications/B8AB0D75-7FBE-4C5B-8D48-2ABCE9C7564D/Documents/log.txt
I installed vim on my phone via cydia, and there is no log.txt in the said directory! (I've SSH'd into my iPhone using iphone tunnel as root SSH. On another attempt, I manually created the log.txt using vim and ran the code again.. still nothing gets appended to the file.. any ideas?
turns out to be a writing permission to the file.. so i just chmod'ed it and it works fine.. D'OH!

Logging from the IPhone: where is the file?

I am trying to creating a file on my MacOS FS from my IPhone app by means of this code
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent: #"log.txt"];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:path];
if(output == nil) {
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
output = [NSFileHandle fileHandleForWritingAtPath:path];
} else {
[output seekToEndOfFile];
}
I guess the code is OK as it works when executed by the Simulator. However I can't actually see where the file is created when executed from the IPhone. If I print the path I get
/var/mobile/Applications/XXX-XXXX-XXX-XXX/Documents/log.txt
On the simulator, you should find them under: /Users/loginname/Library/Application Support/iPhone Simulator/5.1/Applications/594931F3-B9EF-4B2C-833D-76C2DCC61C6B/Documents. You can go to this location from Finder by choosing the Go menu item, holding down the Option key, then choosing Library.
NOTE: Fill in your own loginname and device ID in the path above.
If you are on the device, you can copy the file over to your Mac using XCode, using the organizer (choose Window | Organizer). Highlight the App (after it has been executed, of course) and there is an option to copy the file over. You don't have direct access to the device file system.
You can also use NSLog() which just sends the debug info to the output window in XCode.
EDIT: Added detail on how to show Library folder.

How to read a .data file encoded using NSKeyedArchiever iPhone

I use NSKeyedArchiver to archive data in my iPhone app into a .data. Long story, short, I lost all my data. I use "iPhone back up extractor" and it works great. I found a .data file for my app.
To be clear, I found a .data file and I would to open and see what the contents look like. I understand no software (Ex: notepad) might be able to read that file and I may have to use Xcode. If you know of any way, Xcode/notepad/someProgram, please let me know.
This is the code that I implement when I build the app:
- (NSString *)locPath {
return pathInDocumentDirectory(#"tableArray.data");
}
- (void)archieve{
// Get the path to the document directory
NSString *path = [self locPath];
// grab the array
NSMutableArray *tableArray = [someViewController tableArray];
// archive the array to file
[NSKeyedArchiver archiveRootObject:tableArray toFile:path];
}
This code is called during applicationWillTerminate, applicationDidEnterBackground, etc...
The data is restored/called upon in the didFinishLaunchingWithOptions like this:
// Get the path to the document directory
NSString *path = [self locPath];
// Unarchive .data into an array
NSMutableArray *tableArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
Any help is appreciated! Thanks!!!
PS: Link for iPhone back up extractor is http://supercrazyawesome.com/
The easiest way I found is to get the document directory using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [paths objectAtIndex:0];
NSLog(#"Directory: %#", dir);
In there, the Documents dir will contain the .data file

Application's Document folder in iphone

I am using dropbox API and downloading file in application document folder.
I am able to view list of files in table from document directory. How can I read photos or file from this table view?
And what about the iPhone. Can we access document folder directly?
This is the more commonly used approach to get the documents directory –
NSArray *searchResults = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [searchResults objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
You can use NSFileManager to examine the contents of the directory. You can look at the contentsOfDirectoryAtPath:error: method.
Access document Folder -
NSDictionary *theCatalogInfo=nil;
NSString *theCatalogFilePath = [NSString stringWithFormat:#"%#/Documents/",NSHomeDirectory()];
theCatalogFilePath = [theCatalogFilePath stringByAppendingString:#"File Name"];
if(nil!=theCatalogFilePath)
{
theCatalogInfo=[[NSDictionary alloc]initWithContentsOfFile:theCatalogFilePath];
}
return theCatalogInfo;