2017-10-18 49 views
1

我試圖在iOS中檢測到熱點狀態。爲此,我需要使用SystemConfiguration的API如下快速修改SCDynamicStore.h的頭文件

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

有幾種方法可以做到這一點。

這是一種全部是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) 
+1

謝謝@idz ...它工作得很好 – Suraj