2016-07-23 44 views
0

我已經在Swift應用中設置了附近的API,當應用處於前臺時我可以收到消息。繼instructions in the docs 我嘗試包括在適當的地方params.allowInBackground = true,但我得到一個錯誤:Google在iOS背景掃描上的API附近API

Value of type 'GNSBeaconStrategyParams' has no member 'allowInBackground' 

所以,我不能這樣做,我GNSSubscription物體看起來是這樣的:

subscription = messageManager.subscriptionWithMessageFoundHandler(
     messageFoundHandler, messageLostHandler: messageLostHandler, 
     paramsBlock: { (params: GNSSubscriptionParams!) in 
      params.deviceTypesToDiscover = .BLEBeacon 
      params.permissionRequestHandler = { (permissionHandler: GNSPermissionHandler!) in 
       // TODO: Show custom dialog here, and call permissionHandler after it is dismissed 
       // show the dialogue 
      } 
      params.beaconStrategy = GNSBeaconStrategy(paramsBlock: { (params: GNSBeaconStrategyParams!) in 
        params.includeIBeacons = true 
        //params.allowInBackground = true //*** THIS DOESN'T WORK *** 
       }) 
    }) 

我messageHandlers看起來像這樣:

messageFoundHandler = {[unowned self](message: GNSMessage!) -> Void in 
     print("Found handler:", message.type, "->", String(data: message.content!, encoding:NSUTF8StringEncoding)!) 
     if UIApplication.sharedApplication().applicationState != .Active { 
      let localNotification = UILocalNotification() 
      localNotification.alertBody = "Message received" + String(data: message.content!, encoding:NSUTF8StringEncoding)! 
      UIApplication.sharedApplication().presentLocalNotificationNow(localNotification) 
     } 
    } 

    messageLostHandler = {(message: GNSMessage!) -> Void in 
     print("Lost Handler:", message.type, "->", String(data: message.content, encoding:NSUTF8StringEncoding)!) 
    } 

有了這個設置和範圍內的艾迪斯通燈塔我現在在後臺得到通知!既然這是我想要的,我應該感到高興。但是,如果我離開連接,在我開始看到這樣的消息流的背景下應用的Xcode設備(好像是大約每5秒):

2016-07-23 19:35:08.243 Hoc[1269:622746] Can't endBackgroundTask: no background task exists with identifier 2f3, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 

我使用v0.10.0 NearbyMessages。如果任何人都可以指出我的正確方向,讓iOS上的後臺掃描工作可靠,那將是非常棒的。

回答

2

這是由Cocoapods的問題引起的。儘管我用$ pod update和類似的方法從我的項目中刪除/重新添加了NearbyMessages cocoaPod,出於某種原因,我無法獲得已安裝的附近SDK的最新版本(1.0.1)。解決方案是手動從Github下載規範並覆蓋~/.cocoapods/repos/master中的文件。

然後爲了確保我安裝了最新版本,我將podfile更改爲包含pod 'NearbyMessages', '~> 1.0.1'這些工作。

現在我可以在GNSBeaconStrategy對象上設置適當的參數,並且可以在iOS背景中對Eddystone信標進行背景掃描。 :-)

我希望這會有所幫助。

+0

James,你最初在你的Podfile中有什麼?如果你有'pod'NearbyMessages','〜> 0.10.0'',你不會得到帶有後臺支持的最新CocoaPod。請注意,通過使用'pod'NearbyMessages','〜> 1.0.1'',您將獲得1.0版本的bug修復更新,但當它出現時您將無法獲得1.1版本。 –

+0

謝謝@DanWebb我最初只有'pod'NearbyMessages''沒有版本號。我會改變它來獲取更新,但是我的cocoapod/git設置有些奇怪,所以我可能無法得到它們。謝謝 – James