[NSLog日志]ios在真机中将NSLog日志存入文件并保存到document目录

保存Log日志
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //当真机连接Mac调试的时候把这些注释掉,否则log只会输入到文件中,而不能从xcode的监视器中看到。
    //如果是真机就保存到Document目录下的log.text文件中
    UIDevice *device = [UIDevice currentDevice];
    if (![[device model] isEqualToString:@"iPhone 4s Simulator"]) {
        // 开始保存日志文件
        [self redirectNSlogToDocumentFolder];
    }
    /*******************************************************************************/

    return YES;
    
}


保存到(/Documents/log.text)文件的Log日志

#pragma mark - 保存到(/Documents/log.text)文件的Log日志
- (void)redirectNSlogToDocumentFolder {
    
    /*
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *date = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.log",date];
     */
    
    NSString *fileName = [NSString stringWithFormat:@"log.text"];
    NSString *logfilePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",fileName];
    NSLog(@"logfilePath = %@",logfilePath);
    
    NSFileManager *fileManage = [NSFileManager defaultManager];
    //判断是否存在(/Documents/log.text)文件路径,不存在就创建文件
    BOOL islogfilePath = [fileManage fileExistsAtPath:logfilePath];
    if (!islogfilePath) {
        [fileManage createFileAtPath:logfilePath contents:nil attributes:nil];
    }
    freopen([logfilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
    /*
    fropen([logfilePath cStringUsingEncoding:NSASCIIStringEncoding], stdout);
    fropen([logfilePath cStringUsingEncoding:NSASCIIStringEncoding], stderr);
    */
}

Q:如何将日志打印到一个文件

A:可以使用freopen函数重定向标准输出和标准出错文件。因为printf函数会向标准输出(stdout)打印,而NSLog函数会向标准出错(stderr)打印。重新定向标准输出(stdout)和标准出错(stderr)到一个文件将会使他们打印日志到一个文件中。

freopen("/tmp/log.txt",
 "a+", stdout);

freopen("/tmp/log.txt",
 "a+", stderr);



项目配置共享文件

您可以通过如下步骤来让应用程序支持文件共享:在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。将您希望共享的文件放在应用程序的Documents目录。一旦设备插入到用户计算机,iTunes 9.1就会在选中设备的Apps标签中显示一个File Sharing区域。此后,用户就可以向该目录添加文件或者将文件移动到桌面计算机中。如果应用程序支持文件共享,当文件添加到Documents目录后,应用程序应该能够识别并做出适当响应。例如说,应用程序可以将新文件的内容显示界面上。请不要向用户展现目录的文件列表并询问他们希望对文件执行什么操作。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值