在我的swift 3應用程序中,我有2個iBeacons應該執行不同的任務。其中一個應該打開並播放視頻文件,另一個應該向用戶發送通知。當我關閉iPhone到播放音頻文件的信標時,一切正常,但當我測試另一個發送通知的信標時,應用程序只發送1個通知,並突然停止工作。我假設我試圖無限次地顯示相同的通知,但我不知道如何解決這個問題。這是我的錯誤:如何在檢測到iBeacon後向用戶顯示通知,而應用程序正在運行且關閉時?
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <Audio.DEViewController: 0x149f05190>.'
請看看我的代碼波紋管:
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"),
3542: UIAlertController(title:"Permission Required", message:"Location services permission is required.", preferredStyle:.alert)
]
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] {
let ok = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
}
url.addAction(ok)
self.present(url as UIViewController, animated:true){}
// only execute this code once, if avPlayer not created yet
if self.avPlayer == nil {
self.avPlayer = AVPlayer(url: url as! URL)
self.avPlayerViewController.player = self.avPlayer
self.present(self.avPlayerViewController,animated: true) {() -> Void in
self.avPlayerViewController.player?.play()
}
}
}
}
而且CN如何啓用發送,即使用戶關閉應用程序的通知。我在
info.plist
但仍當我關閉應用程序,並嘗試顯示通知,什麼也沒發生加入
Privacy - Location Always Usage Description
。
if let url = self.colors[closestBeacon.minor.intValue] {
if (NSDate.timeIntervalSinceReferenceDate - self.lastNotificationTime > 10) {
self.lastNotificationTime = NSDate.timeIntervalSinceReferenceDate
let ok = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
}
url.addAction(ok)
self.present(url as UIViewController, animated:true){}
}
您可以在設置通知的位置顯示您的代碼並將其發送給用戶? – davidgyoung
當然,請看最後一段 – Dakata