正如 vfn 所写,您需要附加调试器或将日志值保存到磁盘。
我正在做 OAuth,这需要模拟器离开应用程序并进行一些身份验证是 Safari,然后 Safari 将使用 URL 方案重新打开应用程序。这意味着我无法获得应用退出后记录的不同身份验证步骤的日志。
无论如何,我编写了这个类,它将消息记录到位于 ~/user*/library/application support/iPhone Simulator/user/yourapp*/documents 中的“log.txt”
*user 和 yourapp 当然是变量名。
//
// LogFile.m
//
//
// Created by RickiG on 11/30/09.
// Copyright 2009 www.rickigregersen.com.. All rights reserved.
//
#import "LogFile.h"
@implementation LogFile
+ (void) stringToLog:(NSString *) str {
NSDate *now = [NSDate date];
NSDateFormatter *logTimeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[logTimeFormatter setDateFormat:@"HH:mm:ss"];
NSString *timeStr = [NSString stringWithFormat:@"%@", [logTimeFormatter stringFromDate:now]];
NSString *logMsg = [NSString stringWithFormat:@"%@\n%@\n\n", timeStr, str];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"log.txt"];
NSData *dataToWrite = [[NSString stringWithString:logMsg] dataUsingEncoding:NSUTF8StringEncoding];
// Check if file exists
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path]) { // Returns a BOOL
NSData *dataFromFile = [[NSData alloc] initWithContentsOfFile:path];
NSMutableData *combinedDataToWrite = [NSMutableData dataWithData:dataFromFile];
[combinedDataToWrite appendData:dataToWrite];
[combinedDataToWrite writeToFile:path atomically:YES];
[dataFromFile release];
} else {
[fileManager createFileAtPath:path contents:dataToWrite attributes:nil];
}
}
@end