您可以从连接的设备下载日志(log)文件,也可以直接从应用程序发送.下面介绍这两种方法.
Send log files from app through email, in Swift
在引用DDFileLogger的类中编写此代码.我会把它放在一个定制的记录器类中,例如MyLogger.swift
var ddFileLogger: DDFileLogger!
var logFileDataArray: [NSData] {
get {
let logFilePaths = ddFileLogger.logFileManager.sortedLogFilePaths() as! [String]
var logFileDataArray = [NSData]()
for logFilePath in logFilePaths {
let fileURL = NSURL(fileURLWithPath: logFilePath)
if let logFileData = try? NSData(contentsOfURL: fileURL, options: NSDataReadingOptions.DataReadingMappedIfSafe) {
// Insert at front to reverse the order, so that oldest logs appear first.
logFileDataArray.insert(logFileData, atIndex: 0)
}
}
return logFileDataArray
}
}
然后,当用户点击一个按钮表示他们想要发送日志(log)时,
// Required by MFMailComposeViewController
import MessageUI
@IBAction func writeEmailTapped(sender: AnyObject) {
if MFMailComposeViewController.canSendMail() {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["your-email@company.com"])
composeVC.setSubject("Feedback for app")
composeVC.setMessageBody("", isHTML: false)
let attachmentData = NSMutableData()
for logFileData in MyLogger.sharedInstance.logFileDataArray {
attachmentData.appendData(logFileData)
}
composeVC.addAttachmentData(attachmentData, mimeType: "text/plain", fileName: "diagnostic.log")
self.presentViewController(composeVC, animated: true, completion: nil)
} else {
// Tell user about not able to send email directly.
}
}
这将导致出现一个" compose 邮箱"弹出窗口,其中包含一个名为diagnostic.log
的附件文件,即所有连接在一起的日志(log)文件.
特别感谢——这是另一个答案给出的Objective-C版本的快速翻译.
Get log file(s) from device directly, through USB cable
如果您想获取应用程序在设备上运行时创建的日志(log)文件,
- 将设备连接到mac电脑
- In Xcode, go to Window -> Devices
- 在设备列表的左上角,单击连接的设备.
- 在主面板的"已安装的应用程序"部分中,单击运行CocoaLumberjack的应用程序.
- 在已安装应用列表的底部,单击gear图标,然后下载容器.
- 在Finder中,右键单击保存的屏幕上的(显示菜单).xcappdata文件,然后 Select "显示软件包内容"
- 日志(log)文件保存在
/AppData/Library/Caches/Logs/
中
如果这对你有帮助的话,向上投票会很好!