在我的应用中,我想在网络应用程序中提交设备的位置。目前,我正在使用BubbleWrap进行发布请求。
请注意,如果需要的话,我可以使用BubbleWrap以外的任何东西。
当前行为如下:
- 我启动应用程序
- 我把它带到后台
- [等待Web应用程序上的请求,观看日志,什么也不会发生]
- 我将应用程序带到前景
- 该应用发送设备的位置
应该发生什么:
- 我启动应用程序
- 我把它带到后台
- 该应用发送设备的位置
- 该应用程序一直在聆听重大的位置更改,并在重大更改上发送其位置
这是代码:
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
附加信息:
- 我在我的rakefile
app.info_plist['UIBackgroundModes'] = ['location']
中声明了以下内容。
关于为什么可能不起作用的任何提示或提示?
我的猜测是因为bw :: http.post(...)是异步的,因此您的应用在块被称为之前退出。