Get device location (only country) in iOS

NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode]; will get you an identifier like e.g. “US” (United States), “ES” (Spain), etc. In Swift 3: let countryCode = NSLocale.current.regionCode In Swift 2.2: let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as String Compared to a solution based on CLLocationManager this approach has pros and cons. The primary con is that it … Read more

didFailWithError: Error Domain=kCLErrorDomain Code=0 “The operation couldn’t be completed. (kCLErrorDomain error 0.)”

If you are using the simulator: Press command + shift + , in Xcode to open the scheme editor Select the Run scheme Go to the Options tab Check ✅ Allow Location Simulation Select a Default Location in the dropdown Selecting None as your Default Location may have caused the problem.

How to retrieve user’s current city name?

As of iOS 5 MKReverseGeoCoder is Deprecated! So you want to use CLGeocoder with CLLocationManager, very simple and works with block. Example: – (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [self.locationManager stopUpdatingLocation]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark *placemark in placemarks) { …. = [placemark … Read more

Periodic iOS background location updates

It seems that stopUpdatingLocation is what triggers the background watchdog timer, so I replaced it in didUpdateLocation with: [self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers]; [self.locationManager setDistanceFilter:99999]; which appears to effectively power down the GPS. The selector for the background NSTimer then becomes: – (void) changeAccuracy { [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [self.locationManager setDistanceFilter:kCLDistanceFilterNone]; } All I’m doing is periodically toggling the accuracy … Read more

Behaviour for significant change location API when terminated/suspended?

Since I asked this question, I have done a fair bit of testing (mostly on the train between home and work) and have confirmed that the behaviour for suspended apps is as I suspected at the end of the question. That is, your suspended app is woken up, you don’t receive any callbacks on your … Read more

How do I get a background location update every n minutes in my iOS application?

I found a solution to implement this with the help of the Apple Developer Forums: Specify location background mode Create an NSTimer in the background with UIApplication:beginBackgroundTaskWithExpirationHandler: When n is smaller than UIApplication:backgroundTimeRemaining it will work just fine. When n is larger, the location manager should be enabled (and disabled) again before there is no … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)