CS5530 Mobile/Wireless Systems More on Swift Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang UC. Colorado Springs CS5530 Ref. CN5E, NT@UW, WUSTL
Overview • Optional • WiFi interface • Location manager CS5530 2 Ref. CN5E, NT@UW, WUSTL
Optionals • Some variables (other than vars or constants) either have a value, or nil • To define an optional o var optionalFloat: Float? • Optional with forced unwrapping CS5530 3 Ref. CN5E, NT@UW, WUSTL
WiFi interface • interfaces = CNCopySupportedInterfaces() o Return all the available interfaces on your device (e.g., en0) o As an array • interfaceInfo = CNCopyCurrentNetworkInfo(interfaces) { BSSID = "6c:f3:7f:ea:44:f1"; SSID = "UCCS-Wireless"; SSIDDATA = <55434353 2d576972 656c6573 73>; } CS5530 4 Ref. CN5E, NT@UW, WUSTL
WiFi interface • ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String o UCCS-Wireless • bssid = interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String o 6c:f3:7f:ea:44:f1 CS5530 5 Ref. CN5E, NT@UW, WUSTL
Code example func getSSID() -> String? { var ssid, bssid, info: String? if let interfaces = CNCopySupportedInterfaces() as NSArray? { for interface in interfaces { print(interfaces) if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? { print(interfaceInfo) ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String bssid = interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String info = ssid! + ": " + bssid! break } } } return info } CS4500/5500 6 Ref. MOS4E, OS@Austin, Columbia, UWisc
iOS/Android devices • 3 iPhones, 3 iPads • 2 Android phones, 2 Android tabs • Check out @Ali CS5530 7 Ref. CN5E, NT@UW, WUSTL
Location Manager • Core Location Framework o var locationManager: CLLocationManager = CLLocationManager() • Location access permission o locationManager.requestWhenInUseAuthorization() o locationManager.requestAlwaysAuthorization() CS5530 8 Ref. CN5E, NT@UW, WUSTL
Recommend
More recommend