microsoft / plcrashreporter

Reliable, open-source crash reporting for iOS, macOS and tvOS
Other
2.91k stars 538 forks source link

PLCrashReporter does not catch crash when app uses Google-Mobile-Ads-SDK #260

Closed DRybochkin closed 2 years ago

DRybochkin commented 2 years ago

podfile target 'DemoApp' do use_modular_headers! use_frameworks! :linkage => :static

pod 'SwiftLint'

pod 'nanopb' pod 'PromisesObjC' pod 'GoogleUtilities' pod 'GoogleUserMessagingPlatform' pod 'GoogleAppMeasurement' pod 'Google-Mobile-Ads-SDK'

pod 'AppCenter/Crashes'

end

Sources AppDelegate:

import UIKit import AppCenter import AppCenterCrashes import os

@main final class AppDelegate: UIResponder, UIApplicationDelegate {

// MARK: - Properties

var window: UIWindow?

// MARK: - Lifecycle

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let window = UIWindow(frame: UIScreen.main.bounds)
    Crashes.delegate = self
    AppCenter.start(withAppSecret: "a3b30845-59e1-c445-b824-b78a2aa4cc08", services: [Crashes.self])
    if Crashes.hasReceivedMemoryWarningInLastSession {
        log(message: "hasReceivedMemoryWarningInLastSession")
    }
    if Crashes.hasCrashedInLastSession {
        log(message: "hasCrashedInLastSession")
    }
    window.rootViewController = ViewController()
    window.makeKeyAndVisible()
    self.window = window
    return true
}

}

extension AppDelegate: CrashesDelegate {

func crashes(_ crashes: Crashes, shouldProcess errorReport: ErrorReport) -> Bool {
    log(message: "shouldProcess \(errorReport)")
    return true
}

func crashes(_ crashes: Crashes, didSucceedSending errorReport: ErrorReport) {
    log(message: "didSucceedSending \(errorReport)")
}

func crashes(_ crashes: Crashes, willSend errorReport: ErrorReport) {
    log(message: "willSend \(errorReport)")
}

func crashes(_ crashes: Crashes, didFailSending errorReport: ErrorReport, withError error: Error?) {
    log(message: "didFailSending \(errorReport) \(String(describing: error?.localizedDescription))")
}

}

private extension AppDelegate {

// MARK: - Private functions

func log(message: String) {
    os_log(.default, "AppDelegate: %{public}s", message)
    print(message)
}

func log(error: String) {
    os_log(.error, "AppDelegate: %{public}s", error)
}

}

ViewController:

import UIKit import os

final class ViewController: UIViewController {

// MARK: - Initializations

init() {
    super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

// MARK: - Lifecycle

override func viewDidLoad() {
    super.viewDidLoad()

    if #available(iOS 14.0, *) {
        createButton(point: CGPoint(x: 50, y: 50), title: "Generate UICollection crash", handler: { [weak self] _ in
            self?.collectionCrash()
        })
        createButton(point: CGPoint(x: 50, y: 350), title: "Generate ObjC crash", handler: { _ in
            objc_terminate()
        })
        createButton(point: CGPoint(x: 50, y: 425), title: "Generate NSObject crash", handler: { _ in
            let nsArray = NSArray()
            nsArray.adding(NSNumber(value: 1))
            let isError = nsArray.object(at: 10) as? NSNumber != nil
            os.os_log(.default, isError ? "" : "")
        })
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

}

}

private extension ViewController {

// MARK: - Private functions

func collectionCrash() {
    let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    let viewController = UIViewController()
    window.rootViewController = viewController
    window.makeKeyAndVisible()
    let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 20, height: 20), collectionViewLayout: UICollectionViewLayout())
    viewController.view.addSubview(collectionView)
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Test", for: IndexPath(row: 0, section: 0))
    let isError = "\(cell.self)" == "Test1"
    os.os_log(.default, isError ? "" : "")
}

func createButton(point: CGPoint, title: String, handler: @escaping UIActionHandler) {
    if #available(iOS 14.0, *) {
        let button = UIButton(type: .system, primaryAction: UIAction(handler: handler))
        button.backgroundColor = .lightGray
        button.setTitle(title, for: .normal)
        button.frame = CGRect(x: point.x, y: point.y, width: view.frame.width - 100, height: 50)
        view.addSubview(button)
    }
}

}

aleksandr-dorofeev commented 2 years ago

I am closing this issue as a duplicate of #2431.