我这里有个奇怪的问题。我正在用信标测试一些东西。
以下代码将在Parse DB中添加一行值,当信标接近时,设备显示将变为紫色。
当从Xcode运行时,通过usb - lightning连接设备时,这种方法非常有用。 但是当我在没有电缆和xcode的情况下运行应用程序“独立”时,虽然设备变为紫色,但这些值不会存储在数据库中。
我正在使用以下代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "01122334-4556-6778-899A-ABBCCDDEEFF0")!, identifier: "Estimotes")
// Note: make sure you replace the keys here with your own beacons' Minor Values
let colors = [
1: UIColor(red: 84/255, green: 77/255, blue: 160/255, alpha: 1), // purple
2: UIColor(red: 142/255, green: 212/255, blue: 220/255, alpha: 1),
3: UIColor(red: 162/255, green: 213/255, blue: 181/255, alpha: 1),
4: UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1) //White
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}
locationManager.startRangingBeaconsInRegion(region)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
let knownBeacons = beacons.filter{ $0.proximity == CLProximity.Immediate }
if (knownBeacons.count > 0) {
let closestBeacon = knownBeacons[0] as CLBeacon
self.view.backgroundColor = self.colors[closestBeacon.minor.integerValue]
registerBeacon(closestBeacon.minor.integerValue)
}
else {
self.view.backgroundColor = self.colors[4];
}
}
func registerBeacon(minor: Int) {
let gameScore = PFObject(className:"GameScore")
gameScore["minor"] = minor
gameScore["beacon"] = "01"
gameScore.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The object has been saved.
} else {
// There was a problem, check error.description
}
}
}
}