在我的应用中,我想在网络应用的后台提交设备的位置。目前我正在使用BubbleWrap作为POST请求。
请注意,如果需要,我可以使用除BubbleWrap之外的任何其他内容。
目前的行为如下:
会发生什么:
这是代码:
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
其他信息:
app.info_plist['UIBackgroundModes'] = ['location']
中声明了以下内容。有关其原因的任何提示或提示可能无效?
答案 0 :(得分:1)
我的猜测是因为BW :: HTTP.post(...)是异步的,你的应用程序会在调用块之前退出。