Google在iOS背景扫描上的API附近API

问题描述:

我已经在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上的后台扫描工作可靠,那将是非常棒的。

这是由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