2017-07-28 91 views
0

我写了下面的代码,这是我能想到的最基本的Swift 3代码,用来检查我的应用程序是否可以检测到iBeacon是否可用。 但是,didEnterRegion只在蓝色月亮中被激发一次。我可能在手机上运行了20次,而且它只发射了两次。问题似乎并不是灯塔本身,因为当我使用App Store中的应用程序时,它总是检测到我的iBeacon。任何想法为什么这个回调很少被调用? PS:我已经添加了隐私 - 位置始终使用情况说明到Info.plist中didEnterRegion有时只被解雇

import UIKit 
import CoreLocation 

class ViewController: UIViewController { 

let locationManager = CLLocationManager() 

@IBOutlet weak var textLabel: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager.requestAlwaysAuthorization() 
    locationManager.delegate = self 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func startMonitoring(_ sender: Any) { 
    if let uuid = UUID(uuidString: "10F86430-1346-11E4-9191-0800200C9A66") { 
     let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 1, minor: 1,identifier: "iBeacon") 
     locationManager.startMonitoring(for: beaconRegion) 
     textLabel.text = "Monitoring" 
    } 
} 
} 

extension ViewController: CLLocationManagerDelegate { 
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { 
    textLabel.text = "\(error.localizedDescription)" 
} 

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
    textLabel.text = "\(error.localizedDescription)" 
} 

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
     textLabel.text = "Found a new beacon" 
} 
} 

回答

1

您必须退出该地区,等待30秒左右,并再次输入有它触发。如果你留在该地区,它不会触发。

+0

这是最可能的解释。 – davidgyoung