简体   繁体   English

Swift中的MobileWiFi框架回调

[英]MobileWiFi framework callback in Swift

For a private project I'm trying to use the private MobileWiFi framework in Swift. 对于私人项目,我试图在Swift中使用私有MobileWiFi框架。

I found this website: http://iphonedevwiki.net/index.php/MobileWiFi.framework In Objective-C it's working like a charm, but I want to convert the code to Swift. 我发现这个网站: http//iphonedevwiki.net/index.php/MobileWiFi.framework在Objective-C中它的工作就像一个魅力,但我想将代码转换为Swift。

I have converted the function "Retrieving a list of known networks" into Swift already and that function is working. 我已经将“检索已知网络列表”功能转换为Swift,该功能正在运行。

Now I'm trying to convert the "Getting the WiFi signal strength" function but the uses a callback function and I have tried so hard, but can't get it to work. 现在我正在尝试转换“获取WiFi信号强度”功能,但使用回调函数,我已经尝试了这么努力,但无法让它工作。

   #include <MobileWiFi.h>

static WiFiManagerRef _manager;
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token);

int main(int argc, char **argv)
{
    _manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);

    CFArrayRef devices = WiFiManagerClientCopyDevices(_manager);
    if (!devices) {
        fprintf(stderr, "Couldn't get WiFi devices. Bailing.\n");
        exit(EXIT_FAILURE);
    }

    WiFiDeviceClientRef client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);

    WiFiManagerClientScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    WiFiDeviceClientScanAsync(client, (CFDictionaryRef)[NSDictionary dictionary], scan_callback, 0);

    CFRelease(devices);

    CFRunLoopRun();

        return 0;
}

static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token)
{
    NSLog(@"Finished scanning! networks: %@", results);

    WiFiManagerClientUnscheduleFromRunLoop(_manager);
    CFRelease(_manager);

    CFRunLoopStop(CFRunLoopGetCurrent());
}

Can someone explain to me how to use such a callback function in the swift language? 有人可以向我解释如何在swift语言中使用这样的回调函数吗?

Thanks in advance! 提前致谢!

I found this function: 我找到了这个功能:

pcap_loop(pcapSession, numberOfPackets,
{
    (args: UnsafeMutablePointer<u_char>,
     pkthdr:UnsafePointer<pcap_pkthdr>,
     packet: UnsafePointer<u_char>) ->Void in

            // singleton call
            let pa = PacketAnalyser.sharedInstance
            pa.Process()
},
nil)

On this website: http://diydeveloper.io/tech/2015/11/27/swift-pcap 在这个网站上: http//diydeveloper.io/tech/2015/11/27/swift-pcap

I think this is a posible solution for my problem. 我认为这是我的问题的可行解决方案。 Can somebody explain to me if I can use this example? 有人可以向我解释一下我是否可以使用这个例子?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM