在swift中修改SCDynamicStore.h的标头

时间:2017-10-18 14:17:11

标签: ios objective-c swift iphone-privateapi

我正在尝试检测iOS中的热点状态。为此,我需要使用SystemConfiguration API,如下所示

let sc = SCDynamicStoreCreate(nil, "com.apple.wirelessmodemsettings.MISManager" as CFString, nil, nil)
let info = SCDynamicStoreCopyValue(sc, "com.apple.MobileInternetSharing" as CFString)

SCDynamicStoreCreateSCDynamicStoreCopyValue不适用于iOS。我需要修改SCDynamicStore.h文件并使这些功能适用于iOS(它们目前仅适用于Mac)。

此链接通过创建重复标题提及了一种方法。SCDynamicStoreCreate is unavailable: not available on iOS。但这种方法并不适合我。

如何在swift中实现这一目标?

由于

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。

这是Swift的一种方式,不涉及改变头文件。

    import SystemConfiguration

    // Define types for each of the calls of interest
    typealias TSCDynamicStoreCreate = @convention (c) (_ allocator: CFAllocator?, _ name: CFString, _ callout: SystemConfiguration.SCDynamicStoreCallBack?, _ context: UnsafeMutablePointer<SCDynamicStoreContext>?) -> SCDynamicStore?
    typealias TSCDynamicStoreCopyValue = @convention (c) (_ store: SCDynamicStore?, _ key: CFString) -> CoreFoundation.CFPropertyList?

    // Get a handle to the library, the flag `RT_NOLOAD` will limit this
    // to already loaded libraries
    let hLibrary = dlopen("/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration", RTLD_NOLOAD);

    // Load addresses of the functions from the library
    let MySCDynamicStoreCreate = unsafeBitCast(dlsym(hLibrary, "SCDynamicStoreCreate"), to: TSCDynamicStoreCreate.self)
    let MySCDynamicStoreCopyValue = unsafeBitCast(dlsym(hLibrary, "SCDynamicStoreCopyValue"), to: TSCDynamicStoreCopyValue.self)

    // Setup constants
    let name = "com.apple.wirelessmodemsettings.MISManager" as CFString
    let key = "com.apple.MobileInternetSharing" as CFString

    // Call the functions through the looked up addresses
    let dynamicStore = MySCDynamicStoreCreate(nil, name, nil, nil)
    let plist = MySCDynamicStoreCopyValue(dynamicStore, key)
    dump(plist)