当前位置:网站首页>Swift -- 保存打印日志到沙盒

Swift -- 保存打印日志到沙盒

2022-06-22 14:49:00 锐意无限

最近项目测试的时候经常遇到卡死的问题,集成的bug收集器又收集不到问题所在,导致没有办法定位问题,就自己写个打印日志收集的方法,将之保存在沙盒里面,以便测试人员发现卡死的时候,我们能根据打印的日志定位问题所在,代码如下:

	private func redirectNSlogToDocumentFolder() {
    
        let filePath: String  =  NSHomeDirectory () +  "/Documents/PrintfInfo.log"
        let defaultManager = FileManager.default
        try? defaultManager.removeItem(atPath: filePath)
        
        freopen(filePath.cString(using: String.Encoding.ascii), "a+", stdout)
        freopen(filePath.cString(using: String.Encoding.ascii), "a+", stderr)
    }

调用很简单,直接在willFinishLaunchingWithOptions调用就行:

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    
        let device = UIDevice.current
        if device.model == "iPhone" {
    // 真机
            self.redirectNSlogToDocumentFolder()
        }
        
        return true
        
        
    }

苹果默认的App 文件的可见性是关闭的,我们需要在plist中重启权限才能查看沙盒中的数据:
Application supports iTunes file sharing 的值设为YES,就行了

原网站

版权声明
本文为[锐意无限]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_30963589/article/details/125366829