Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created July 17, 2024 04:40
Show Gist options
  • Save pookjw/dcb47cec65e823a6bdbd3189e0728b6c to your computer and use it in GitHub Desktop.
Save pookjw/dcb47cec65e823a6bdbd3189e0728b6c to your computer and use it in GitHub Desktop.
import UIKit
import ObjectiveC
import CoreServices
// LSApplicationWorkspace *
fileprivate var applicationWorkspace: AnyObject {
let isa: AnyClass = objc_lookUpClass("LSApplicationWorkspace")!
let cmd = sel_registerName("defaultWorkspace")
let method = class_getClassMethod(isa, cmd)
let imp = method_getImplementation(method!)
let casted = unsafeBitCast(imp, to: (@convention(c) (AnyClass, Selector) -> AnyObject).self)
return casted(isa, cmd)
}
// NSArray<LSApplicationProxy *> *
fileprivate var allApplications: [AnyObject] {
let applicationWorkspace = applicationWorkspace
let cmd = sel_registerName("allApplications")
let method = class_getInstanceMethod(type(of: applicationWorkspace), cmd)
let imp = method_getImplementation(method!)
let casted = unsafeBitCast(imp, to: (@convention(c) (AnyObject, Selector) -> [AnyObject]).self)
return casted(applicationWorkspace, cmd)
}
fileprivate func bundleContainerURL(_ applicationProxy: AnyObject) -> URL? {
let cmd = sel_registerName("bundleContainerURL")
let method = class_getInstanceMethod(type(of: applicationProxy), cmd)
let imp = method_getImplementation(method!)
let casted = unsafeBitCast(imp, to: (@convention(c) (AnyObject, Selector) -> URL?).self)
return casted(applicationProxy, cmd)
}
fileprivate func localizedShortName(_ applicationProxy: AnyObject) -> String? {
let cmd = sel_registerName("localizedShortName")
let method = class_getInstanceMethod(type(of: applicationProxy), cmd)
let imp = method_getImplementation(method!)
let casted = unsafeBitCast(imp, to: (@convention(c) (AnyObject, Selector) -> String?).self)
return casted(applicationProxy, cmd)
}
fileprivate func bundleIdentifier(_ applicationProxy: AnyObject) -> String? {
let cmd = sel_registerName("bundleIdentifier")
let method = class_getInstanceMethod(type(of: applicationProxy), cmd)
let imp = method_getImplementation(method!)
let casted = unsafeBitCast(imp, to: (@convention(c) (AnyObject, Selector) -> String?).self)
return casted(applicationProxy, cmd)
}
// https://x.com/Rednick16_/status/1813411959565336695
// Internally uses -[ISBundleIdentifierIcon prepareImageForDescriptor:]
fileprivate func applicationIconImage(_ bundleIdentifier: String) -> UIImage? {
let isa: AnyClass = UIImage.self
let cmd = sel_registerName("_applicationIconImageForBundleIdentifier:format:scale:")
let method = class_getClassMethod(isa, cmd)
let imp = method_getImplementation(method!)
let casted = unsafeBitCast(imp, to: (@convention(c) (AnyClass, Selector, String, Int, CGFloat) -> UIImage?).self)
let format: Int
if UIDevice.current.userInterfaceIdiom == .pad {
format = 8
} else {
format = 10
}
return casted(isa, cmd, bundleIdentifier, format, UIScreen.main.scale)
}
@main
struct Foo {
static func main() {
for applicationProxy in allApplications {
print("---------------")
if let localizedShortName = localizedShortName(applicationProxy) {
print(localizedShortName)
}
if let bundleContainerURL = bundleContainerURL(applicationProxy) {
print(bundleContainerURL)
}
if let bundleIdentifier = bundleIdentifier(applicationProxy),
let iconImage = applicationIconImage(bundleIdentifier)
{
print(iconImage)
}
print("---------------")
}
}
}
@speedyfriend433
Copy link

Omg thank you so much 😭😭😭🥳🥳🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment