gpt4 book ai didi

ios - 快速修改 SCDynamicStore.h 的 Headers

转载 作者:行者123 更新时间:2023-11-28 06:10:39 26 4
gpt4 key购买 nike

我正在尝试检测 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)。

此链接提到了一种通过创建重复 header 来执行此操作的方法。SCDynamicStoreCreate is unavailable: not available on iOS .但是这种方法对我来说并不适用。

如何快速实现这一目标?

谢谢

最佳答案

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

这是一种完全 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)

关于ios - 快速修改 SCDynamicStore.h 的 Headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com