Skip to content

Commit

Permalink
fix: Use extension safe APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
crleona committed Sep 27, 2024
1 parent 633deb5 commit b4a937a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Amplitude-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@
OBJ_128 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
LD = /usr/bin/true;
OTHER_SWIFT_FLAGS = "-swift-version 5 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/ManifestAPI -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -package-description-version 5.7.0";
SWIFT_VERSION = 5.0;
Expand All @@ -851,6 +852,7 @@
OBJ_129 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
LD = /usr/bin/true;
OTHER_SWIFT_FLAGS = "-swift-version 5 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/ManifestAPI -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -package-description-version 5.7.0";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -988,6 +990,7 @@
OBJ_85 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DRIVERKIT_DEPLOYMENT_TARGET = 19.0;
Expand Down Expand Up @@ -1026,6 +1029,7 @@
OBJ_86 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DRIVERKIT_DEPLOYMENT_TARGET = 19.0;
Expand Down
23 changes: 14 additions & 9 deletions Sources/Amplitude/Plugins/Vendors/AppUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ import Foundation
}

override func beginBackgroundTask() -> BackgroundTaskCompletionCallback? {
if isRunningInAppExtension {
if !isRunningInAppExtension, let application = IOSVendorSystem.sharedApplication {
var id: UIBackgroundTaskIdentifier = .invalid
let callback = { () in
application.endBackgroundTask(id)
id = .invalid
}
id = application.beginBackgroundTask(withName: "amplitude", expirationHandler: callback)
return callback
} else {
let semaphore = DispatchSemaphore(value: 0)
ProcessInfo.processInfo.performExpiringActivity(withReason: "Amplitude") { expired in
guard !expired else {
Expand All @@ -77,20 +85,17 @@ import Foundation
return {
semaphore.signal()
}
} else {
var id: UIBackgroundTaskIdentifier = .invalid
let callback = { () in
UIApplication.shared.endBackgroundTask(id)
id = .invalid
}
id = UIApplication.shared.beginBackgroundTask(withName: "amplitude", expirationHandler: callback)
return callback
}
}

private var isRunningInAppExtension: Bool {
return Bundle.main.bundlePath.hasSuffix(".appex")
}

// Extension-safe accessor for sharedApplication. Will return nil if run in an extension.
static var sharedApplication: UIApplication? {
return UIApplication.value(forKeyPath: "sharedApplication") as? UIApplication
}
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Sources/Amplitude/Plugins/iOS/IOSLifecycleMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class IOSLifecycleMonitor: UtilityPlugin {
// TODO: Check if lifecycle plugin works for app extension
// App extensions can't use UIApplication.shared, so
// funnel it through something to check; Could be nil.
application = UIApplication.value(forKeyPath: "sharedApplication") as? UIApplication
application = IOSVendorSystem.sharedApplication
super.init()
setupListeners()
}
Expand Down

0 comments on commit b4a937a

Please sign in to comment.