Is sentry-cocoa safe to use in App Extensions?

We have several app extensions bundled with our iOS app. There is some shared code between them where we plan on using Sentry to log messages. Before shipping this, I wanted to confirm whether or not it is safe to use Sentry from within an AppEx. My initial tests seem to indicate that it does indeed work, but since I didn’t see anything in the docs about App Extensions, I wanted to ask here first. Thanks!

1 Like

Hey,
We never thoroughly tested it but it should work without a problem.
If anything comes up, feel free to create a ticket on Github, will look into it asap then.

Should Mac extensions work too (e.g. Today widget for the sidebar or a Safari extension)?

Yes, should also work.

Ok, I managed to make it work both in a Today extension and in a Safari extension on the Mac. A few tips:

  • make sure the app and extension are signed
  • open the Console app and scan the logs coming from your app for any errors
  • enable the Outgoing Connections sandbox capability on the extension, otherwise any requests to Sentry will fail
  • if you build the first version before you add CocoaPods, the binary goes to a different folder than the one made with CocoaPods, so make sure the system doesn’t keep loading the old version

It’s mostly working now, although some exceptions are displayed as a generic [NSApplication _crashOnException:], but I’m not even sure if it’s specific to an extension context… If you want to debug this, here’s a sample project: https://www.dropbox.com/s/7kcdecfybikyfmc/SentryTodayTest.zip?dl=0

Code:

@IBAction func button1Pressed(sender: NSButton) {
    crashByNilUnwrap(item: nil)
}

func crashByNilUnwrap(item: String?) {
    print(item!.lowercased())
}

@IBAction func button2Pressed(sender: NSButton) {
    crashByMissingMethod(item: NSString())
}

func crashByMissingMethod(item: AnyObject) {
    FooHelper().explode()
}
- (void)explode {
    id string = [NSArray array];
    [string lowercaseString];
}

Trace for “button 1”:

SentryTodayExt 0x000108968bf3 TodayViewController.crashByNilUnwrap(item : String?) -> ()SentryTodayExt/TodayViewController.swift:54
SentryTodayExt 0x000108968ac1 TodayViewController.button1Pressed(sender : NSButton) -> ()SentryTodayExt/TodayViewController.swift:50

Trace for “button 2”:

AppKit 0x7fffb4f3e44e -[NSApplication _crashOnException:]
AppKit 0x7fffb4f3e382 -[NSApplication reportException:]
AppKit 0x7fffb4bd44c5 -[NSApplication run]
AppKit 0x7fffb4b9ee0e NSApplicationMain

Thank you for your write up and demo project :slight_smile:
I also tried to make the “button 2” crash display the correct stack trace but without success.
You can also set a Principal class but SentryCrashExceptionApplication isn’t working because it expects a NSViewServiceApplication our application inherits from NSApplication

I wasn’t able to figure out how to inherit from NSViewServiceApplication since this header seems to be in a private framework, so for now I think we will have to live with it.

Ok, makes sense, thanks.