I am new in the iBeacon and Swift development and I have some problems with my application. In the following code bellow, I am trying to detect iBeacon and when I detect the minor value of the beacon I attach it to the link I want to play. The problem occurs when I start the program. The video starts playing for a second and then the whole application stops with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .libc++abi.dylib: terminating with uncaught exception of type NSException.
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var avPlayerViewController = AVPlayerViewController()
var avPlayer:AVPlayer?
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: UUID(uuidString: "8492E75F-4FD6-469D-B132-043FE94921D8")!, identifier: "Estimotes")
let videos = [
19987: NSURL ( string: "http://techslides.com/demos/sample-videos/small.mp4"),
]
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
let knownBeacons = beacons.filter{ $0.proximity != CLProximity.unknown }
if (knownBeacons.count > 0) {
let closestBeacon = knownBeacons[0] as CLBeacon
if let url = self.videos[closestBeacon.minor.intValue] {
self.avPlayer = AVPlayer(url: url as! URL)
self.avPlayerViewController.player = self.avPlayer
}
self.present(self.avPlayerViewController,animated: true) { () -> Void in
self.avPlayerViewController.player?.play()
}
}
}
Would you please advice me how to fix that error?