We are living in a time where richer inter-app communication and tighter integration with the OS services is available in mobile, and iOS is no exception. iOS App extensions enable you to extend your reach beyond the process of presenting the UI. You can capture screen, intercept network, or interact with an Apple Watch while other apps are running in the foreground.
These extensions run in their own, sandboxed processes, and require extra care to monitor them in production. In this post, we are going to take a look at how Backtrace SDK can help us in our example Keyboard Extension.
You can find a pair of sample projects below if you prefer jumping right into it.
You'll need the latest Xcode and backtrace-cocoa SDK. CocoaPods CLI is not required.
Download the latest archive from backtrace-cocoa releases. Make sure you pick bitcode=YES embedded=YES flavor.
Launch your Xcode project.
Copy these into your project root by drag and dropping them into Project Navigator.
Rome/Backtrace.framework
Rome/Backtrace_PLCrashReporter.framework
Rome/Cassette.framework
Select your app and all extensions in the prompted dialog.
Navigate to each build target's General settings (i.e., your app and each extension).
Locate Framework, Libraries, and Embedded Content for apps and Frameworks and Libraries for extensions.
Add all the new frameworks to your targets if missing. Set embedding settings as Embed without Signing.
Navigate to Build Phases > Embed Frameworks and make sure all the frameworks are added there (app and extension targets).
Navigate to Build Settings and change Debug Information Format to Dwarf with dSYM File (app and extension targets).
Create a new directory with Finder in your project and name it Backtrace.
Copy all ".h" files in Backtrace.framework/Headers and Backtrace_PLCrashReporter.framework/Headers into the newly created directory.
Go back to Xcode and add Backtrace by drag & dropping it into Project Navigator. If you get prompted to add bridging header files, accept it.
Make sure files are added to all targets in the prompted dialog.
Create a new Obj-C class in your project (i.e., BacktraceWrapper.h and BacktraceWrapper.m). If asked, allow Xcode to create bridging headers for the targets that utilize Swift.
1#import <Foundation/Foundation.h>23NS_ASSUME_NONNULL_BEGIN45@interface BacktraceWrapper : NSObject67+(void)sendError:(NSError*)error attributes:(NSDictionary*)extra;89@end1011NS_ASSUME_NONNULL_END
1#import "BacktraceWrapper.h"2#if defined(__arm64__) && __arm64__3#import "Backtrace-Swift.h"4#import "Backtrace-PLCrashReporter-umbrella.h"5#endif67@implementation BacktraceWrapper89#pragma mark Globals10#if defined(__arm64__) && __arm64__11static BacktraceClient* client;12#endif1314#pragma mark Private1516+(void)initializeBacktrace {17#if defined(__arm64__) && __arm64__18if (client != nil) {19return;20}2122BacktraceCredentials* credentials = [[BacktraceCredentials alloc] initWithSubmissionUrl: [NSURL URLWithString: @"https://submit.backtrace.io/BACKTRACE_SUBDOMAIN/BACKTRACE_SUBMISSION_TOKEN/plcrash"]];2324BacktraceDatabaseSettings *dbSettings = [[BacktraceDatabaseSettings alloc] init];25dbSettings.maxRecordCount = 10;2627BacktraceClientConfiguration *configuration = [[BacktraceClientConfiguration alloc]28initWithCredentials: credentials29dbSettings: dbSettings30reportsPerMin: 30 // Default is 3031allowsAttachingDebugger: FALSE // If false, disables backtrace during development32detectOOM: FALSE];333435BacktraceCrashReporter* crashReporter = [[BacktraceCrashReporter alloc] initWithConfig: PLCrashReporterConfig.defaultConfiguration];36client = [[BacktraceClient alloc] initWithConfiguration:configuration crashReporter:crashReporter error:nil];3738[client.metrics enableWithSettings:[[BacktraceMetricsSettings alloc] init]];39#endif40}414243#pragma mark Public4445+(void)sendError:(NSError*)error attributes:(NSDictionary*)extra {46#if defined(__arm64__) && __arm64__47[self initializeBacktrace];48[client sendWithError:error attachmentPaths:@[] completion:^(BacktraceResult * _Nonnull result) {49NSLog(@"Backtrace error sent: %@", [error description]);50}];51#endif52}5354@end
If you want to expose BacktraceWrapper to your SWIFT files, add the following line to your bridging headers:
#import "BacktraceWrapper.h"
BacktraceWrapper.sendError(error: Error) will become available to your Swift code.
Once you are ready to distribute your app, run the commands below in your CI to upload debug symbols to Backtrace.
1cd $DERIVED_DATA_PATH/Build/Products/Debug-iphoneos/23zip -r symbols.zip *.dSYM4curl --fail-with-body --data-binary @symbols.zip -X POST -H "Expect: gzip" "https://submit.backtrace.io/$BACKTRACE_SUBDOMAIN/$BACKTRACE_SYMBOLS_ACCESS_TOKEN/symbols"5
From now on, your extensions will have the same error reporting capabilities that your app has. We know how painful it can become to debug custom keyboards or video calls in production especially when they crash silently with no useful error messages. Those days are now in the past.
We’d love to hear your feedback! We have recently added support for Swift Package Manager which will simplify working with App Extensions even further.
This article was originally published in January 2023 and has been updated in May 2023.