2016年5月10日 星期二

0510 SWIFT WIFI 讀取


1. 使用方式:
建立 IfWifiList.swift 檔案
2.加入XCODE中
3.系統詢問是否要建立標頭黨 確認是
4.在新建立的標頭檔加入 #import "ifaddrs.h"
至此就可以編譯了

參考使用範例 呼叫程式

SWIFT 程式範例 (詳情參見wifiTest2 範例)
    @IBAction func btnGetIpPress(sender: UIButton) {
        //利用不同函數 驗證wifi指令
        var number = getAddressList(1) //取得現在IP列表
        number = getSSIDList2(number+1)  //取得現在SSID
        getSSIDList1(number+1)
   
    }

    // MARK: 呼叫範例程式
    // 傳入參數為列印位置高度
    func getAddressList(startIdx:Int)->Int{
        let wifiIPList=ifaddrsSwift.getIFAddresses()
        let wifiCount = CFArrayGetCount(wifiIPList)
        creatLabel(startIdx,showText: "wifi count \(wifiCount)")
        var count = startIdx
        for wifiIP in wifiIPList {
            count++;
            creatLabel(count,showText: wifiIP )
        }
        return count
    }
    func getSSIDList2(startIdx:Int) -> Int{
        let ssidList = ifaddrsSwift.getSSID()
        let wifiCount = ssidList.ssidCount
        let arrayList = ssidList.ssidList
        creatLabel(startIdx,showText: "SSID  count \(wifiCount)")
        var count=startIdx
        for wifiIP in arrayList {
            count++;
            creatLabel(count,showText: wifiIP)
        }
        return count
    }
    func getSSIDList1(startIdx:Int) -> Int{
     
        let ssidList = NEHotspotHelper.supportedNetworkInterfaces()
        let wifiCount = ssidList.count
        let arrayList = ssidList
        creatLabel(startIdx,showText: "supp count \(wifiCount)")
        var count=startIdx
        for wifiIP in arrayList {
            count++;
            creatLabel(count,showText: wifiIP as! String)
        }
        return count
    }

 //////////////////////////////////////////////////////////////////////
//
//  IfWifiList.swift
//  WifiTest2
//
//  Created by thai bob on 2015/12/23.
//  Copyright © 2015年 thai bob. All rights reserved.
//
//存放有關網路相關函數

import Foundation
import NetworkExtension //for NEHotspotHelper
import SystemConfiguration.CaptiveNetwork //CNCopySupportedInterfaces

// MARK: 網卡相關函數
public class ifaddrsSwift {
 
 
    //取得IP
    class func getIFAddresses() -> [String] {
     
        var addresses = [String]()
     
        // Get list of all interfaces on the local machine:
        var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
        if getifaddrs(&ifaddr) == 0 {
         
            // For each interface ...
            for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
                let flags = Int32(ptr.memory.ifa_flags)
                var addr = ptr.memory.ifa_addr.memory
             
                // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
                if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                    if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
                     
                        // Convert interface address to a human readable string:
                        var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                        if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                            nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                                if let address = String.fromCString(hostname) {
                                    addresses.append(address)
                                }
                        }
                    }
                }
            }
            freeifaddrs(ifaddr)
        }
     
        return addresses
    }
 
    //get ssid
    class func getSSID() -> (ssidCount:Int,ssidList:[String]) {
     
        var ssidStringList = [String]()
      //  var ssidList:CFArray?=nil
        var errorCode=0
        let ssidIfList = CNCopySupportedInterfaces()
        if ssidIfList == nil {
            errorCode = -1
        }else if CFArrayGetCount(ssidIfList) == 0{
            errorCode = -2
        }else{
            errorCode = CFArrayGetCount(ssidIfList)
            let totalCount = errorCode
         
            for i in 0..<totalCount{
                let interfaceName: UnsafePointer<Void> = CFArrayGetValueAtIndex(ssidIfList, i)
                let rec = unsafeBitCast(interfaceName, AnyObject.self)
                let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
                if unsafeInterfaceData != nil {
                    let interfaceData = unsafeInterfaceData! as Dictionary!
                    let currentSSID = interfaceData["SSID"] as! String
                    ssidStringList.append(currentSSID)
                    print("\(i)=\(currentSSID)")
                }
                else {
                    errorCode--
                    //currentSSID = ""
                }
            }
        }
//        if bError != 0 {
//            currentSSID = "Error"
//        }
     
        return (errorCode,ssidStringList)
    }
 
    //get ssid
    class func getSSID2() -> (ssidCount:Int,ssidList:[String]) {
        var ssidList = [String]()
        var ssidCount = 0
        for newOne in NEHotspotHelper.supportedNetworkInterfaces(){
            ssidCount++
            ssidList.append(newOne.SSID)
        }
        return (ssidCount,ssidList)
    }
 
}





// MARK:可能可以取得SSID的程式碼
Register your app as Hotspot helper.
#import <NetworkExtension/NetworkExtension.h>

NSArray * networkInterfaces = [NEHotspotHelper supportedNetworkInterfaces];
NSLog(@"Networks %@",networkInterfaces);
UPDATE (Sept. 11th)
The following Captive Network APIs have been re-enabled in the latest version of iOS 9 instead.
CNCopySupportedInterfaces
CNCopyCurrentNetworkInfo
UPDATE (Sept. 16th)
If you still prefer to use NetworkExtension and Apple gave you permission to add the entitlements, then you can do this to get the wifi information:
for(NEHotspotNetwork *hotspotNetwork in [NEHotspotHelper supportedNetworkInterfaces]) {
    NSString *ssid = hotspotNetwork.SSID;
    NSString *bssid = hotspotNetwork.BSSID;
    BOOL secure = hotspotNetwork.secure;
    BOOL autoJoined = hotspotNetwork.autoJoined;
    double signalStrength = hotspotNetwork.signalStrength;
}

沒有留言:

張貼留言

cocos2dx-lua 建立滑鼠監聽

重要關鍵字  EVENT_MOUSE_SCROLL addEventListenerWithSceneGraphPriority      if IsPc() then --建立滑鼠監聽         local listener = cc.EventListenerMouse...