问题
I am trying to detect Hotspot status in iOS. For that I need to use the SystemConfiguration
APIs as follows
let sc = SCDynamicStoreCreate(nil, "com.apple.wirelessmodemsettings.MISManager" as CFString, nil, nil)
let info = SCDynamicStoreCopyValue(sc, "com.apple.MobileInternetSharing" as CFString)
But SCDynamicStoreCreate
and SCDynamicStoreCopyValue
are not available for iOS. I need to modify SCDynamicStore.h
file and make these functions available for iOS (They are currently marked available for Mac only).
This link mentions a way to do this by creating duplicate header.. SCDynamicStoreCreate is unavailable: not available on iOS. But this method is not working for me in swift.
How this can be achieved in swift?
Thanks
回答1:
There's a few ways you can do this.
Here's a way that is all Swift and does not involve altering header files.
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)
来源:https://stackoverflow.com/questions/46812375/modify-headers-of-scdynamicstore-h-in-swift