Search code examples
rubygeolocationrubymotion

Rubymotion / bubblewrap geolocation in airplane mode makes the app crash


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


Solution

  • 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