当应用程序输入后台时,使用BubbleWrap :: HTTP发送设备的位置

时间:2012-12-21 18:24:58

标签: ios5 ios6 cllocationmanager rubymotion

在我的应用中,我想在网络应用的后台提交设备的位置。目前我正在使用BubbleWrap作为POST请求。

请注意,如果需要,我可以使用除BubbleWrap之外的任何其他内容。

目前的行为如下:

  1. 我启动应用
  2. 我把它带到后台
  3. [在Web应用上等待请求,看日志,没有任何反应]
  4. 我将应用程序带到前台
  5. 应用程序发送设备的位置
  6. 会发生什么:

    1. 我启动应用
    2. 我把它带到后台
    3. 应用程序发送设备的位置
    4. 应用程序会持续侦听重要的位置更改,并在重大更改时发送其位置
    5. 这是代码:

      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']中声明了以下内容。

      有关其原因的任何提示或提示可能无效?

1 个答案:

答案 0 :(得分:1)

我的猜测是因为BW :: HTTP.post(...)是异步的,你的应用程序会在调用块之前退出。