didUpdateLocations is not called in ios 9.3.2 - objective-c

didUpdateLocations is never called, instead didFailWithError is called with denied code kCLErrorDenied.
Here are the things that i did:
#import "ViewController.h"
#import CoreLocation;
#interface ViewController () <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
NSLog(#"update");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error
{
NSLog(#"error");
}
#end
Inserted NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to the info.plist of the app. Enabled Background Modes in Capabilities with Location updates selected. Get help from the very famous blog post. I receive Location Access Request popup and allow the app to access location.
But i couldnt managed to get location updates. It works in IPAD with IOS 8 but not work at ipad with IOS version 9.3.2. How can i make location updates work in IOS 9.3.2?

trying following thing may resolve your issue:
go to settings and reset your location services
reset your network settings
restarting the simulator
also, if you have Scheme/Edit Scheme/Options/Allow Location Simulation checked but don't have a default location set.
Add NSLocationAlwaysUsageDescription to your plist as type string if using Always authorization. Also, do the same for NSLocationWhenInUseUsageDescription same way if using WhenInUse authorization.
For detail check answers here:
didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
and IOS 9 Error Domain=kCLErrorDomain Code=0 "(null)"

Related

CLLocationManager startMonitoringSignificantLocationChanges doesn't work

I want my application to get location updates even if it's terminated I tried to sign up for location changes using SignificantLocationChanges but I seems that I'm not getting any updates.
In my AppDelegate.m I have the following setup:
#interface AppDelegate () <RCTBridgeDelegate, CLLocationManagerDelegate>
#property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
#property (nonatomic, strong) CLLocationManager* locationManager;
#property BOOL didShowLocationUpdateNotification;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
/// other setup
[super application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch(status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(#"locationManagerDidChangeAuthorization - kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(#"locationManagerDidChangeAuthorization - kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied:
NSLog(#"locationManagerDidChangeAuthorization - kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(#"locationManagerDidChangeAuthorization - kCLAuthorizationStatusAuthorizedWhenInUse");
break;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(#"didUpdateLocation");
NSLog(#"Locations %#", locations);
}
I have the following Background Modes enabled:
I also have those keys in my info.plist:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Your location is required to track your journeys. By selecting ALWAYS your journeys will record automatically.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is required to track your journeys. </string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required to track your journeys. WARNING: Journeys will not automatically record with 'When In Use' selected</string>
And yet, after I terminate the app, it never wakes up again even though I changed my location drastically.
What else am I mussing here?
I'm testing it on iPhone 11 with iOS 15.

Using CLLocationManager on iOS Simulator to find latitude and longitude [duplicate]

This question already has answers here:
Location Services not working in iOS 8
(26 answers)
Closed 8 years ago.
I am trying to create an application that opens with a map using MKMapView, and then by creating a delegate to CLLocationManger find the current location of the user.
I have posted my code below. The problem that I am having right now is that that although the map does appear when opening the this the simulator it does not give a position or a new heading from the NSLog that should be passing the latitude and longitude.
I am new to Objective - C and App Development, does anyone see if I am missing some of the protocol for CLLocationManger? Thanks for all your help.
Sorry the code is a little sloppy, pasted it in quickly.
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController () <CLLocationManagerDelegate>
#property (nonatomic,strong)CLLocationManager * myLocationManger;
#property (nonatomic,strong) IBOutlet MKMapView *myMapView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"insdie view did load");
// Setting the map to be big as the view
self.myMapView =[[MKMapView alloc]init];
//Initial Property to Map
self.myMapView =[[MKMapView alloc] init];
//Set type to Standard
self.myMapView.mapType = MKMapTypeStandard;
//Set Mask for AutoReszing
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//Add the View!
[self.view addSubview:self.myMapView];
///*Now lets Use my CLLocation Manager to Locate the iPhone's Posistion*////
//Chekcs if Location Services are Enabled
if ([CLLocationManager locationServicesEnabled]) {
self.myLocationManger = [[CLLocationManager alloc] init];
self.myLocationManger.delegate = self;
[self.myLocationManger startUpdatingLocation];
}
else{
//Location Services are available we will need software to ask to turn this On
//The user is SOL if they refuse to turn on Location Services
NSLog(#"Location Services not enabled");
}
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//This method will show us that we recieved the new location
NSLog(#"Latitude = %f",newLocation.coordinate.latitude );
NSLog(#"Longitude =%f",newLocation.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager
didFinishDeferredUpdatesWithError:(NSError *)error{
NSLog(#"Error with Updating");
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
//Failed to recieve user's location
NSLog(#"failed to recived user's locatio");
}
I found that in the new iOS 8, you need two keys added to the Plist. This article explains the process beautifuly.
http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

Why does UILocalNotification with region not fire?

I have followed the example given in the WWDC 2014 session "What's New in iOS Notifications" to set up a UILocalNotification delivery when a region's boundary is crossed.
However, my notification never fires on device nor simulator running iOS beta 3. The notification works fine when I tested using a fireDate instead of a region (not both at same time-- that's not allowed).
I have set NSLocationWhenInUseUsageDescription in my Info.plist.
Here is my Obj-C code:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#interface ViewController () <CLLocationManagerDelegate>
#property (nonatomic, strong) CLLocationManager *locationManager;
#property (nonatomic, strong) CLLocation *notifLocation;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// test points at Apple HQ
self.notifLocation = [[CLLocation alloc] initWithLatitude:37.331741 longitude:-122.030333];
[self setupLocationMonitoring];
}
- (void) setupLocationMonitoring
{
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10; // meters
// iOS 8+ request authorization to track the user’s location
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)registerLocationNotification
{
UILocalNotification *locNotification = [[UILocalNotification alloc] init];
locNotification.alertBody = #"Hello!";
// doesn't work
locNotification.regionTriggersOnce = NO;
locNotification.region = [[CLCircularRegion alloc] initWithCenter:self.notifLocation.coordinate radius:50 identifier:#"PlaceName"];
// works:
// locNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
UIApplication *application = [UIApplication sharedApplication];
[application cancelAllLocalNotifications];
[application scheduleLocalNotification:locNotification];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// check status to see if we’re authorized
BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
if (canUseLocationNotifications) {
[self registerLocationNotification];
}
}
#end
Note: this question is about iOS 8's new Location Notifications, not about region monitoring on older versions of iOS.
Your code works fine for me. Here are the steps on iPhone Simulator:
1) Open Simulator app and choose the following option from the top menu Debug -> Location -> Custom Location and put there the coordinates as you have in code: (37.331741, -122.030333)
2) Run your Xcode project on iPhone Simulator
3) Background the app
4) Open the same top menu item Debug -> Location but change it from Custom Location to Freeway Drive or City Run. Play with City Run/ Freeway Drive and Custom Location
I see the notification when I follow these steps. Looks like it doesn't work in case appropriate coordinate is a starting one but it works when the coordinate changes to appropriate from some others.

Having trouble with mapkit usertrackingwithheading mode on iOS 6

I think I may have discovered a bug in the mapkit API for iOS 6, but since I still consider myself a rookie I thought I'd check here to see if anyone can point out something I may be doing wrong.
I have an app that I've been working on for a few weeks with a mapview in it and utilizing the MKUserTrackingButton to toggle tracking modes. On iOS 5 it was working fine but since upgrading to 6 it has weird behavior. When you put the mapview into The tracking mode that follows user with heading, it does fine if you are relatively stationary, but when you start moving in a car it drops out of the track with heading mode every time back to regular tracking mode. After many frustrating hours trying to figure it out I decided to make a new simple app with the bare minimum mapview and tracking to see if it was just my coding or possible bug. The new app does the same thing. I'm posting all the code below. Hopefully some one can help tell me if I'm doing something wrong or not.
Here's the app delegate header
// iTrackerAppDelegate.h
// iTracker
//
// Created by Victor Hudson on 9/22/12.
// Copyright (c) 2012 Victor Hudson. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TrackerViewController.h"
#interface iTrackerAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) IBOutlet UIWindow *window;
#end
Here's the app delegate implementation
//
// iTrackerAppDelegate.m
// iTracker
//
// Created by Victor Hudson on 9/22/12.
// Copyright (c) 2012 Victor Hudson. All rights reserved.
//
#import "iTrackerAppDelegate.h"
#implementation iTrackerAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TrackerViewController *trackerView = [[TrackerViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:trackerView];
[self.window setRootViewController:navController];
[[self window] makeKeyAndVisible];
return YES;
}
// the other app delegate methods are all empty so i left them out for brevity
#end
Here's my view controller. It has a nib with the worldView in it and a segmented switch for toggling map modes(plain, sat, and hybrid)
TrackerViewController.h
//
// TrackerViewController.h
// iTracker
//
// Created by Victor Hudson on 9/22/12.
// Copyright (c) 2012 Victor Hudson. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface TrackerViewController : UIViewController
#property (strong, nonatomic) IBOutlet MKMapView *worldView;
- (IBAction)toggleMapView:(id)sender;
#end
TrackerViewController.m
//
// TrackerViewController.m
// iTracker
//
// Created by Victor Hudson on 9/22/12.
// Copyright (c) 2012 Victor Hudson. All rights reserved.
//
#import "TrackerViewController.h"
#interface TrackerViewController ()
#end
#implementation TrackerViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// navigation item
[[self navigationItem] setTitle:#"iTracker"];
MKUserTrackingBarButtonItem *trackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.worldView];
[[self navigationItem] setRightBarButtonItem:trackingButton animated:YES];
self.worldView.userTrackingMode = 1;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)toggleMapView:(id)sender {
switch ([sender selectedSegmentIndex]) {
case 0:
{
[self.worldView setMapType:MKMapTypeStandard];
}break;
case 1:
{
[self.worldView setMapType:MKMapTypeSatellite];
}break;
case 2:
{
[self.worldView setMapType:MKMapTypeHybrid];
}break;
}
}
#end
As I said before all seems to work fine but the tracking with heading mode when you are moving very fast. I am running on an iPhone 4, and Ive tried the app with and without ARC to the same results. I would be grateful if someone can point out any mistakes I'm making or if they want to build the project and can confirm it is a bug.
Thanks in advance for any assistance ;-)
I'm currently investigating a similar behaviour in my app when going from ios5 to ios6.
There is a locationmanager created as a singleton as described in
cllocationmanager singleton
Furthermore in the mapview I'm using setusertrackingmode. In ios6 it jumps from MKUserTrackingModeFollowWithHeading to MKUserTrackingModeFollow back and forth. In ios5 it works fine with identical code.
It may be that the two locationmanagers are in conflict with each other as pointed out in
conflict between two locationmanagers

Xcode Interface Builder Not showing App Delegate Object

I am teaching myself Objective-C and iOS programming with "IOS Programming: The Big Nerd Ranch guide (2nd Edition) and I have run into an issue where the tutorial wants me to create connections to an App Delegate object, but this object does not appear in the Objects list in Interface builder for me. I am pretty sure its either a typo or perhaps a version different as the book is slightly behind my version of Xcode (4.2). I have included the code. I am fairly certain that the MOCAppDelegate object is what should be showing up in IB, but I am not yet familiar enough to know what code changes I need to make that happen. Specific question: How do I adjust the code below so that I get an object in the Objects list in IB so that I can perform the connections as instructed in the tutorial graphic?
Note: I researched and found this: Having trouble hooking up instance variables to AppDelegate but this solution did not work for me (or I did not implement it correctly)
Header File
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface MOCAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
}
#property (strong, nonatomic) IBOutlet UIWindow *window;
#end
Implementation File
#import "MOCAppDelegate.h"
#implementation MOCAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Create location manager object
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
//We want all results from the location manager
[locationManager setDistanceFilter:kCLDistanceFilterNone];
//And we want it to be as accurate as possible
//regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//Tell our location manager to start looking for it location immediately
[locationManager startUpdatingLocation];
//We also want to know our heading
if (locationManager.headingAvailable == true) {
[locationManager startUpdatingHeading];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor darkGrayColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%#", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
NSLog(#"%#", newHeading);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(#"Could not find location: %#", error);
}
- (void)dealloc
{
if( [locationManager delegate] == self)
[locationManager setDelegate:nil];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
#end
Drag an instance of NSObject into your .xib and drop it in the Objects section just as in the instructions you've got. Then select it and change its type in the identity inspector (on the right side, where it says "Custom Class") to "MOCAppDelegate" (or whatever class you like).
You should drag an "NSObject" from the Object Library onto your storyboard on the black bar underneath the view controller you want to connect. Then, click on the NSObject and in the identity inspector change the class to AppDelegate. Then you can create the connection to the AppDelegate.
MOCAppDelegate = AppDelegate
The code was generated for you when you named the project. The delegate of the UIApplication object is usually named slightly different depending on the name of the project.
(There's no doubt that any book in print was using an older Xcode.)
Select Files Owner and the Identity Inspector (command-alt-2) to confirm the file's owner is a place holder for the app delegate.