当前位置:网站首页>Swift -- save print log to sandbox

Swift -- save print log to sandbox

2022-06-22 16:08:00 Infinite determination

Recently, I often encountered the problem of jamming during project testing , Integrated bug The collector cannot collect the problem , There is no way to locate the problem , Write a method to print log collection , Save it in a sandbox , So that when the tester finds out that the card is stuck , We can locate the problem according to the printed log , The code is as follows :

	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)
    }

The call is simple , Directly in willFinishLaunchingWithOptions Just call :

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

Apple's default App File visibility is turned off , We need to be in plist You can only view the data in the sandbox with the restart permission in :
Application supports iTunes file sharing The value of the set YES, That's it

原网站

版权声明
本文为[Infinite determination]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221449151989.html