在我的应用程序中,我想在后台向网络应用程序提交设备的位置。目前我正在为 POST 请求使用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']
。
关于为什么这可能不起作用的任何提示或提示?