79401412

Date: 2025-01-30 22:54:54
Score: 0.5
Natty:
Report link

@zeiteisen code is still working on iOS18, but crashes on NSString(utf8String: response) when response is nil, for examples in Shortcuts scripts. Update:

extension UIInputViewController {
    func getHostBundleId() -> String? {
        if let id = hostBundleIdValueBefore16 { id } else { hostBundleIdValueFor16 }
    }
    
    private var hostBundleIdValueBefore16: String? {
        let value = parent?.value(forKey: "_hostBundleID") as? String
        return value != "<null>" ? value : nil
    }
    
    private var hostBundleIdValueFor16: String? {
        guard let pid = parent?.value(forKey: "_hostPID") else { return nil }
        let selector = NSSelectorFromString("defaultService")
        guard let anyClass: AnyObject = NSClassFromString("PKService"),
              let pkService = anyClass as? NSObjectProtocol,
              pkService.responds(to: selector),
              let serverInis = pkService.perform(selector).takeUnretainedValue() as? NSObjectProtocol
        else {
            return nil
        }
        let lities = serverInis.perform(NSSelectorFromString("personalities")).takeUnretainedValue()
        let bundleId = Bundle.main.bundleIdentifier ?? ""
        guard let infos = lities.object(forKey: bundleId) as? AnyObject,
              let info = infos.object(forKey: pid) as? AnyObject,
              let con = info.perform(NSSelectorFromString("connection")).takeUnretainedValue() as? NSObjectProtocol
        else {
            return nil
        }
        let xpcCon = con.perform(NSSelectorFromString("_xpcConnection")).takeUnretainedValue()
        let handle = dlopen("/usr/lib/libc.dylib", RTLD_NOW)
        let sym = dlsym(handle, "xpc_connection_copy_bundle_id")
        typealias XpcFunc = @convention(c) (AnyObject) -> UnsafePointer<CChar>?
        let cFunc = unsafeBitCast(sym, to: XpcFunc.self)
        if let response = cFunc(xpcCon) {
            let hostBundleId = NSString(utf8String: response)
            return hostBundleId as String?
        }
        return nil
    }
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @zeiteisen
  • Low reputation (1):
Posted by: Sim