2016-12-20 24 views
0

在我的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){} 
      } 
+0

您可以在設置通知的位置顯示您的代碼並將其發送給用戶? – davidgyoung

+0

當然,請看最後一段 – Dakata

回答

1

問題是,您不能多次展示視圖控制器。這是我在這裏回答的同樣的基本問題:How to play a video after iBeacon detection?

爲了澄清,雖然問題提到了「通知」,但問題實際上是一個警告對話框。 (iOS上的通知通常它是指NSNotificaitonCenter物品,其可顯示在鎖屏上的一個更具體的含義。)

當類負載的時候let videos初始化警報對話框實例構造一次。然後它會在unc locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)方法內部呈現給用戶,該方法每秒調用一次。

您無法每秒重新呈現完全相同的警報對話框實例。你只能出示一次,否則你會看到顯示的錯誤。只有當它不再顯示給用戶時,才能再次顯示它。

+0

感謝您的幫助,我添加了一個計時器,您可以查看我更新後的代碼,但現在的問題是每當我收到通知時。 UIAlert動作「OK」被重新呈現,並且在每次通知後,我都會收到+1 OK,這是一個錯誤,因爲在某些時候,我會收到1條包含10個OK動作的通知。我該如何解決這個問題? – Dakata

相關問題