An SDK may need to check if the UIAppDelegate function for background events is defined to determine if it will handle background downloads. Since Swift does not support reflection this is one way to do it using some lower level functions.
Last active
April 8, 2025 18:04
-
-
Save brennanMKE/9f406e41a95aed6e22e13bf7ec05e00d to your computer and use it in GitHub Desktop.
Check if iOS app supports background downloads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var supportsBackgroundDownloads: Bool { | |
guard let appDelegate = UIApplication.shared.delegate else { | |
return false | |
} | |
let signature = "application:handleEventsForBackgroundURLSession:completionHandler:" | |
var methodCount: UInt32 = 0 | |
guard let methodList = class_copyMethodList(type(of: appDelegate), &methodCount) else { | |
return false | |
} | |
let range = 0..<Int(methodCount) | |
let found = range.reduce(into: []) { result, index in | |
let selName = sel_getName(method_getName(methodList[index])) | |
let methodName = String(cString: selName, encoding: .utf8)! | |
result.append(methodName) | |
}.first(where: { $0 == signature }) != nil | |
return found | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment