macOS 应用的崩溃报告器

CI Status Swift 5.0 Version License Platform Carthage compatible

你的应用总有一天会崩溃。请做好自动收集崩溃数据的准备,因为并非每个用户都是技术专家,能够从内置的 Console 应用中发送 .crash 文件给你。

要求

对于可选的服务器端点脚本,你需要 PHP 7.x。

安装

Carthage

github "CleanCocoa/CrashReporter"

CocoaPods

pod 'CrashReporterMac'

SwiftPM

.package(url: "https://github.com/CleanCocoa/CrashReporter", from: "0.2.0")

手动

如果你想自定义 UI,请检出源码并将 CrashReporter/ 中的所有代码复制到你的项目中。

使用方法

服务器端点

你需要一个服务器端点来接收崩溃报告。

该框架不关心服务器如何处理

崩溃报告器框架将执行 HTTP POST 请求

只要你的 URL 可以从应用程序访问,你就可以自己编写端点。

或者你可以使用此仓库中提供的简单端点!它位于 php/index.php。此 PHP 服务器脚本将尝试以带时间戳的附件形式通过电子邮件将崩溃日志发送给你,例如 20190701204853 Sherlock-2.0.crash(其中时间戳表示 ISO 格式的日期 2019-07-01 20:48:52)。如果用户输入了她的电子邮件地址,她默认会收到一份抄送。你可以在 PHP 脚本的 frontmatter 中切换此设置。

要在本地机器上运行脚本进行快速测试,请运行

$ php -S 127.0.0.1:3333 php/index.php

然后在你的 Swift 代码中使用 URL(string: "http://127.0.0.1:3333/") 作为端点。

应用程序设置

请参阅 Example/ 中的代码,它是 Xcode 项目的一部分。

你可以在你的应用程序中直接使用该框架来检查崩溃报告

import CrashReporter

let crashReporterURL = URL(string: "http://127.0.0.1:3333/")!
let crashReporter = CrashReporter(
    crashReporterURL: crashReporterURL,
    privacyPolicyURL: URL(string: "https://example.com/privacy-policy")!)

// Run the check in the background and display 
// a crash reporter window if needed
crashReporter.check()

应用程序中的首选项面板

如果允许用户在崩溃报告器窗口中勾选“自动发送崩溃报告”,你应该在应用程序的首选项面板中添加类似的选项,以便可以撤消此设置。

请参考 DefaultsKeys.sendCrashLogsAutomaticallyKey

如果想使用 Cocoa Bindings 在你的首选项面板或主菜单中配置“自动发送崩溃报告”复选框,你可以在你的类中创建一个简单的、符合 KVC 的包装器

// Assuming this is loaded from a Nib where you set an object of this type as the
// target for "Value" Cocoa Bindings.
class PreferenceController: NSViewController {
    let crashReporter: CrashReporter = // ... setup before ...
    
    // Cocoa bindings path is `self.sendCrashReportsAutomatically`
    @objc public dynamic var sendCrashReportsAutomatically: Bool {
        get {
            return crashReporter.sendCrashReportsAutomatically
        }
        set {
            crashReporter.sendCrashReportsAutomatically = newValue
        }
    }
}

API

如果你没有更改崩溃报告器设置的 UserDefaults 键,请在你的应用程序中使用各种 DefaultsKeys.standard 属性来查找值

许可证

整个项目都按照 MIT 许可证分发。请参阅 LICENSE 文件 以供参考。

快速概览