Logowanie do pliku na iPhonie
Jaki byłby najlepszy sposób na zapisanie logów do pliku lub bazy danych w aplikacji na iPhone ' a?
Idealnie, wyjście nslog () może zostać przekierowane do pliku za pomocą freopen (), ale widziałem kilka raportów, że to nie działa. Czy ktoś już to robi lub ma jakieś pomysły jak to najlepiej zrobić?
Dzięki!
5 answers
Z powodzeniem wykorzystałem freopen(...) przez telefon, aby przekierować wyjście do mojego pliku.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-14 18:52:05
Jeśli chcesz użyć Cocoa, NSString i NSData mają metody odczytu/zapisu do pliku, a NSFileManager daje Ci operacje na plikach. Oto przykład (powinien działać na iPhonie):
NSData *dataToWrite = [[NSString stringWithString:@"String to write"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"fileName.txt"];
// Write the file
[dataToWrite writeToFile:path atomically:YES];
// Read the file
NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:path];
// Check if file exists
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:path]; // Returns a BOOL
// Remove the file
[fileManager removeItemAtPath:path error:NULL];
// Cleanup
[stringFromFile release];
[fileManager release];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-15 17:59:09
Ten kod działa świetnie dla mnie..
#if TARGET_IPHONE_SIMULATOR == 0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif
Możesz następnie pobrać plik dziennika z iphone ' a za pomocą metody opisanej tutaj http://blog.coriolis.ch/2009/01/09/redirect-nslog-to-a-file-on-the-iphone/#more-85
Zauważ, że użycie freopen spowoduje zatrzymanie pracy konsoli w XCODE.. jednak z jakiegoś powodu konsola można zobaczyć w Organizer Xcode nadal działa świetnie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-20 16:38:34
Ten kod działa dla mnie:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
#if TARGET_IPHONE_SIMULATOR == 0
freopen([@"/tmp/my_logs.txt" fileSystemRepresentation], "w", stderr);
#endif
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-26 11:56:47
Rozważ użycie kakaowego Drwala . Jest to lekkie, ale elastyczne narzędzie, które zastępuje funkcjonalność NSLog. Moim zdaniem jest w tej samej klasie co Log4J, pozwalając na niestandardowe appendery i tym podobne. Posiada na przykład rejestrator SQLite.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-28 15:44:21