Rubymotion / bubblewrap geolocation in airplane mode makes the app crash

为君一笑 提交于 2019-12-22 00:33:25

问题


Trying to get an clean geoloc implementation in some app; quite struggling with the limitations of what bubblewrap provide for locations

   def get_location
        begin
          BW::Location.get_once do |location|
            if location
              self.latitude   = location.latitude
              self.longitude  = location.longitude
            end
          end
        rescue
          puts "can't geoloc"
        end
      end

With this code (sorry bu I show the 'tests' that - i expect - would work as fallbacks with ruby code), I just get a plain app crash if i set the airplane mode on the phone

Thanks if any experiences on making it works in this mode


回答1:


I believe the geolocation stuff runs in a separate thread, so you would want to put your begin/rescue code within the block:

BW::Location.get_once do |location|
  begin
    if location
      self.latitude   = location.latitude
      self.longitude  = location.longitude
    end
  rescue
    puts "can't geoloc"
  end
end

However, Bubblewrap will actually let you know if there was an error by setting location to a hash with one value, {error: type}. Since you're using get_once, the return value could be either a Hash or a CLLocation object, so you do need to check the object type to avoid an error:

BW::Location.get_once do |location|
  if location.is_a?(Hash) && location[:error]
    puts "can't geoloc"
  else
    self.latitude   = location.latitude
    self.longitude  = location.longitude
  end
end


来源:https://stackoverflow.com/questions/22530828/rubymotion-bubblewrap-geolocation-in-airplane-mode-makes-the-app-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!