1import UIKit
2
3enum Crash {
4 private static var offered = false
5
6 static func start() {
7 guard let folder = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first else { return }
8 let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "development"
9 _ = sb_crash_start(folder.appendingPathComponent("crash.txt").path, build, "iOS \(UIDevice.current.systemVersion)")
10 }
11
12 /// Offers the last run's report, then runs `then` once it is answered, so that what crashed
13 /// that run does not open again before the report can be sent; at once where there is none.
14 static func offer(from host: UIViewController, then: @escaping () -> Void) {
15 guard !offered, let saved = sb_crash_report() else { return then() }
16 let report = String(cString: saved)
17 sb_string_free(saved)
18 offered = true
19 let alert = UIAlertController(
20 title: "Snowbound quit unexpectedly", message: "Send a report to help fix the problem. You can review the report before sending it.", preferredStyle: .alert)
21 alert.addAction(UIAlertAction(title: "Send Report", style: .default) { _ in
22 send(report, from: host)
23 then()
24 })
25 alert.addAction(UIAlertAction(title: "Show Report", style: .default) { _ in show(report, from: host, then: then) })
26 alert.addAction(UIAlertAction(title: "Don’t Send", style: .cancel) { _ in
27 sb_crash_forget()
28 then()
29 })
30 // The scene's window is not yet on screen as it connects.
31 DispatchQueue.main.async { host.present(alert, animated: true) }
32 }
33
34 private static func show(_ report: String, from host: UIViewController, then: @escaping () -> Void) {
35 let page = UIViewController()
36 page.title = "Crash Report"
37 let text = UITextView()
38 text.text = report
39 text.font = .monospacedSystemFont(ofSize: 13, weight: .regular)
40 text.isEditable = false
41 text.backgroundColor = .systemBackground
42 page.view = text
43 let navigation = UINavigationController(rootViewController: page)
44 navigation.isModalInPresentation = true
45 page.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Don’t Send", primaryAction: UIAction { [weak navigation] _ in
46 sb_crash_forget()
47 navigation?.dismiss(animated: true, completion: then)
48 })
49 page.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Send Report", primaryAction: UIAction { [weak navigation] _ in
50 navigation?.dismiss(animated: true) {
51 send(report, from: host)
52 then()
53 }
54 })
55 host.present(navigation, animated: true)
56 }
57
58 private static func send(_ report: String, from host: UIViewController) {
59 let session = URLSession(configuration: .ephemeral)
60 var request = URLRequest(url: URL(string: String(cString: sb_crash_address()))!)
61 request.httpMethod = "POST"
62 request.setValue("text/plain; charset=utf-8", forHTTPHeaderField: "Content-Type")
63 request.httpBody = Data(report.utf8)
64 session.dataTask(with: request) { _, response, error in
65 session.finishTasksAndInvalidate()
66 let sent = error == nil && (response as? HTTPURLResponse).map { (200..<300).contains($0.statusCode) } == true
67 DispatchQueue.main.async {
68 if sent {
69 sb_crash_forget()
70 } else {
71 let alert = UIAlertController(title: "Report wasn’t sent", message: "You can try again.", preferredStyle: .alert)
72 alert.addAction(UIAlertAction(title: "Try Again", style: .default) { _ in send(report, from: host) })
73 alert.addAction(UIAlertAction(title: "Don’t Send", style: .cancel) { _ in sb_crash_forget() })
74 host.present(alert, animated: true)
75 }
76 }
77 }.resume()
78 }
79}