Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use extension safe APIs #231

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading