Ios5 使用BubbleWrap::HTTP发送设备';应用程序进入后台时的位置
在我的应用程序中,我想在后台向web应用程序提交设备的位置。目前我使用的是POST请求 注意,如果需要,我可以使用BubbleWrap以外的任何东西。 当前行为如下所示:Ios5 使用BubbleWrap::HTTP发送设备';应用程序进入后台时的位置,ios5,ios6,cllocationmanager,rubymotion,Ios5,Ios6,Cllocationmanager,Rubymotion,在我的应用程序中,我想在后台向web应用程序提交设备的位置。目前我使用的是POST请求 注意,如果需要,我可以使用BubbleWrap以外的任何东西。 当前行为如下所示: 我启动应用程序 我把它带到后台 [在web应用程序上等待请求,查看日志,什么都没有发生] 我将应用程序带到前台 应用程序会发送设备的位置 应该发生什么: 我启动应用程序 我把它带到后台 应用程序会发送设备的位置 应用程序会持续监听重要的位置更改,并在发生重大更改时发送其位置 代码如下: class AppDelegate
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
[ommitted]
UIApplication.sharedApplication.registerForRemoteNotificationTypes(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)
true
end
def applicationDidEnterBackground(application)
@locationManager.startMonitoringSignificantLocationChanges
end
def applicationDidEnterForeground
@locationManager.stopMonitoringSignificantLocationChanges
end
def application(app, didRegisterForRemoteNotificationsWithDeviceToken:deviceToken)
@device_token = deviceToken.description.gsub(" ", "").gsub("<", "").gsub(">", "")
# Log the push notification to the console
puts @device_token
end
def application(app, didFailToRegisterForRemoteNotificationsWithError:error)
show_alert "Error when registering for device token", "Error, #{error}"
end
def device_token
@device_token
end
def location_manager
if @locationManager.nil?
@locationManager = CLLocationManager.alloc.init
@locationManager.setDesiredAccuracy(KCLLocationAccuracyBest)
@locationManager.delegate = self
end
@locationManager
end
# iOS >= 4
def locationManager(manager, didUpdateToLocation:current_location, fromLocation:last_location)
puts "Location #{current_location} [iOS 5]"
end
# iOS >= 6
def locationManager(manager, didUpdateLocations:locations)
data = {latitude: locations.last.coordinate.latitude,
longitude: locations.last.coordinate.longitude,
device_token: @device_token }
BW::HTTP.post("http://192.168.1.100:4567/", {payload: data}) do |response|
if response.ok?
#json = BW::JSON.parse(response.body.to_str)
#p json['id']
else
App.alert(response.error_message)
end
end
end
def locationManager(manager, didFailWithError:error)
puts "failed"
end
def show_alert(title, message)
alert = UIAlertView.new
alert.title = title
alert.message = message
alert.show
end
end
类AppDelegate
def应用程序(应用程序,DidFinishLaunchWithOptions:launchOptions)
[注]
UIApplication.sharedApplication.RegisterForremoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)
真的
结束
def应用程序标识符背景(应用程序)
@locationManager.StartMonitoring重要位置更改
结束
def应用程序标识符前方地面
@locationManager.Stop监视重要位置更改
结束
def应用程序(应用程序,DidRegisterForRemotionTificationswithDeviceToken:deviceToken)
@device_token=deviceToken.description.gsub(“,”).gsub(“,”)
#将推送通知记录到控制台
放置@device\u令牌
结束
def应用程序(应用程序,didFailToRegisterForRemoteNotificationsWithError:错误)
显示_警报“注册设备令牌时出错”,“错误,#{Error}”
结束
def设备令牌
@设备令牌
结束
def位置管理器
如果@locationManager.nil?
@locationManager=CLLocationManager.alloc.init
@locationManager.setDesiredAccuracy(KCLLocationAccuracyBest)
@locationManager.delegate=self
结束
@位置经理
结束
#iOS>=4
def位置管理器(管理器,didUpdateToLocation:当前位置,fromLocation:最后位置)
放置“位置#{current_Location}[iOS 5]”
结束
#iOS>=6
def位置管理器(管理器,didUpdateLocations:位置)
数据={latitude:locations.last.coordinate.latitude,
经度:locations.last.coordinate.longitude,
设备令牌:@device\u令牌}
BW::HTTP.post(“http://192.168.1.100:4567/“,{payload:data})do |响应|
如果有,好吗?
#json=BW::json.parse(response.body.to_str)
#p json['id']
其他的
应用程序警报(响应错误消息)
结束
结束
结束
def locationManager(管理器,错误:错误)
“失败”
结束
def显示警告(标题、消息)
alert=UIAlertView.new
alert.title=标题
alert.message=消息
警报。显示
结束
结束
其他信息:
- 我已经在我的Rakefile
中声明了以下内容app.info\u plist['UIBackgroundModes']=['location']
有什么提示或提示说明这可能不起作用吗?我的猜测是,因为BW::HTTP.post(…)是异步的,所以你的应用程序会在调用块之前退出。我的猜测是,因为BW::HTTP.post(…)是异步的,您的应用程序在调用块之前退出。您建议什么样的解决方案/替代方案?这可能会有所帮助?你有什么建议/解决方案/替代方案?这可能会有帮助吗?