Working with iOS Location Manager - objective-c

I am developing an app that has many view. Into my app sometimes the user arrives to a view where he can ask for his position clicking over a button. I am trying to follow the Apple guide lines to only ask for the user position if the user allows to do it. What should I do, use the next first code into the app delegate and declare a location manager attribute into any view that the user invokes, passing the location manager attribute to the new view and from the old view and asking with the second next code anytime that the user clicks the button to locate himself?; or just use the second code, declaring a location manager attribute only into the views that allow to get the user location with a button, to check if the location services are enable?
First snippet.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
return YES;
}
Second snippet.
- (IBAction)locateUser:(id)sender {
if([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
} else {
[[[[UIAlertView alloc] initWithTitle:#"Location services."
message:#"Location services are disabled."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
}
Thanks for reading.

CoreLocation will handle all the alerts for you. If locations services are disabled and that you ask for the location, CoreLocation will show an alert telling so to the user with a button to go directly to Settings.app.
If you want to know what append you can check for the delegate call
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
The error here contains a code that will be kCLErrorDenied if the user doesn't let the app use location services.
Also, you should use CoreLocation when the user need it. It's not necessary to check for location services at launch and the overhead of multiple CLLocationManager is almost inexistent.

Related

AddressBook privacy settings not enforced

I'm doing some working getting an app to line up with the new privacy settings in iOS 8. I've completed the requirements satisfactorily for camera access and now I'm taking a look at how this app access the address book. I'm new to working with address book APIs so these questions may have obvious answers.
As with camera access, I was thinking that access to contacts would behave similarly with respect to the status of the privacy settings granted to the app. The thing is, so far, no matter what the value of ABAuthorizationStatus is - kABAuthorizationStatusNotDetermined or kABAuthorizationStatusDenied - access is always allowed.
Also, when the status is kABAuthorizationStatusNotDetermined, the 'Okay/Don't Allow' dialog is never displayed to the user (I've erased the phone and resinstalled the app to confirm). Furthermore, the app never shows up under the privacy settings. I assume this is because the status is always kABAuthorizationStatusNotDetermined.
The code to initiate access to contacts is below. The controller is shown (read only) and contacts' information can be obtained. Delegate code not shown.
// Debug - Value is always denied or not determined.
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// Existing code since iOS 7 - always works despite status.
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
It's great that everything seems to be working but I'd like what, if anything, I'm doing wrong.
iPhone 6,
iOS 8.0.2,
Xcode 6
Thanks!
From what I've found (and I could be wrong) while dealing with updating a code base to correctly deal with the new privacy settings in iOS 8 is that such settings are not respected uniformly across features. For example, without changes to address privacy settings for locations, access to CLLocationManager won't work. Depending on the app, this may appear to the user as though nothing is happening or an error message may appear. However, if no such changes are made for address book privacy settings, access is always granted.
I figured out my error after stumbling upon some extremely helpful example code posted by Apple. Below is code I add/modified so that privacy settings are respected properly in the app I work on. When I find the link to the code example I will post it.
-(void) presentAddressBookPicker {
switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusAuthorized:
[self accessGrantedForAddressBook];
break;
case kABAuthorizationStatusNotDetermined:
[self requestAccessToAddressBook];
break;
case kABAuthorizationStatusRestricted:
case kABAuthorizationStatusDenied:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Unable to access address book"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
break;
default:
// Unlikely but log it anyway.
DLog(#"Unknown address book status.");
break;
}
}
-(void) accessGrantedForAddressBook {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
-(void) requestAccessToAddressBook {
__weak MyWebViewController* weakSelf = self;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf accessGrantedForAddressBook];
});
}
CFRelease(addressBook);
});
}

App crashes when using AHAlertView project

I'm trying to display a custom UIAlertView and I'm using AHAlertView:
https://github.com/warrenm/AHAlertView
I added both AHAlertView.m and .h to my project and add the following to the viewDidLoad method:
NSString *title = #"Alert View Title";
NSString *message = #"This is a message that might prompt you to do something.";
AHAlertView *alert = [[AHAlertView alloc] initWithTitle:title message:message];
__weak AHAlertView *weakAlert = alert;
[alert setCancelButtonTitle:#"Cancel" block:^{
weakAlert.dismissalStyle = AHAlertViewDismissalStyleTumble;
}];
[alert addButtonWithTitle:#"OK" block:^{
weakAlert.dismissalStyle = AHAlertViewDismissalStyleZoomDown;
}];
[alert show];
The problem is when I'm tapping either one of the buttons, the app crashes with:
"Application windows are expected to have a root view controller at the end of application launch"
I don't know what I did wrong, I looked at the sample project and this is the way the alert is being used.
How can I implement it correctly?
Did you activate -fno-objc-arc option ? If you did, deactivate it, AHAlertView support ARC

iOS facebook SDK: Login already authenticated

I have had the Facebook iOS SDK running in an app I've been working on for a few months. At this point I am mostly using the SDK for SSO purposes.
Since I have started using iOS 6.0 I have been seeing an issue where the "Login to use your FB account with MY_APP" modal is blocking my current view.
The odd thing is that the user is already authenticated and apparently authorized to use my app. This is proven by the fact that I can get the user's email address and such.
It is also important to note that if I click the "X" it will close and everything is fine (user is authenticated/authorized). If I login as it tells me, it shows the "MY_APP is already authorized" modal which I cannot click the "Okay" button but I can click the "X" which again drops me back into my view with the user authenticated and authorized.
Here you can see the it:
To authenticate I am calling the following method in the appdelegate:
[appDelegate openSessionWithAllowLoginUI:YES];
The following are the relevant FB methods in my appdelegate:
// FACEBOOK STUFF
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [NSArray arrayWithObjects: #"email", nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
}];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState)state
error:(NSError *)error {
// FBSample logic
// Any time the session is closed, we want to display the login controller (the user
// cannot use the application unless they are logged in to Facebook). When the session
// is opened successfully, hide the login controller and show the main UI.
switch (state) {
case FBSessionStateOpen: {
FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
[cacheDescriptor prefetchAndCacheForSession:session];
[self sendAuthenticationStatusChangedNotification];
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// FBSample logic
// Once the user has logged in, we want them to be looking at the root view.
[FBSession.activeSession closeAndClearTokenInformation];
//[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// FBSample logic
// We need to handle URLs by passing them to FBSession in order for SSO authentication
// to work.
return [FBSession.activeSession handleOpenURL:url];
}
EDIT 1
I noticed that this is happing on one of my phones but not the other. The phone that wasn't working had an older version of the FB app installed. Updating the FB app stopped this issue from happening. I am still interested in a fix to avoid others from experiencing the same issue.
EDIT 2
The issue went away for a little but now is back even with the new facebook app installed. Please help!
The problem for me actually turned out to be a silly one. I was calling
[appDelegate openSessionWithAllowLoginUI:YES];
two times due to a button click event that was wired up twice. As soon as it was only being called once, the issue went away!

MKMapView not showing current location, or rather, any location (in simulator)

I have a MKMapView with it's delegate set to my controller (m) class.
Code below:
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create location manager object
locationManager = [[CLLocationManager alloc] init];
// Make this instance of WhereamiAppDelegate the delegate
// it will send its messages to our WhereamiApplDelegate
[locationManager setDelegate:self];
// We want all results from the location manager
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[mapView setShowsUserLocation:YES];
[window makeKeyAndVisible];
return YES;
}
However, all I am seeing is a map of the world without a blue annotation dot?
The simulator does not "really" support that. If you bypass enough checks, it will display Apple's HQ. Any GPS testing you need a real device.
EDIT: I believe that you can drop a pin and it will display that but as far as callbacks from CLLocationManager (via CLLocationManagerDelegate), it ain't happin in the simulator; at least the most recent ones. I can't speak for anything prior to 3.0...
self.mapview.mapType = MKMapTypeStandard;
self.mapview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapview.showsUserLocation = YES;
This is all you need if you have a pointer to your map called mapView, but as Merky said, you need a real device. What you can do though is, when the simulator is running, go to "Debug ->location -> own location" and input the coords of your desired coordinate.

Checking for iOS Location Services

I have a view with a map and a button (like the Maps app once) that allows the user to center and zoom his current location on the map. If I can not use the locationServicesEnabled method (always returns YES), should I create a BOOL attribute to check if the didFailWithError method is called and know if I can call the button method?
Thanks for reading.
Edited:
This code does not work for me. I am using the simulator. I am always getting YES when asking locationServicesEnabled.
// Gets the user present location.
- (IBAction)locateUser:(id)sender {
if([CLLocationManager locationServicesEnabled]) {
CLLocationCoordinate2D coordinate;
coordinate.latitude = self.mapView.userLocation.location.coordinate.latitude;
coordinate.longitude = self.mapView.userLocation.location.coordinate.longitude;
[self zoomCoordinate:coordinate];
} else {
[[[[UIAlertView alloc] initWithTitle:#"Warning." message:#"Location services are disabled."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease] show];
}
}
In Preferences you have two options to disable the location services. The first option is a global switch to disable the location service for all apps "[CLLocationManager locationServicesEnabled]". The second option let you disable the location service for some apps but not for all apps.
To check if its disabled globally and if its disabled for your app use following:
if([CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
...
}
"locationServicesEnabled" checks if the user has enabled Location Services in Preferences. Your MapView probably checks this value already and should not set any values to "self.mapView.userLocation" if Location Services are not available. This SO question might give you some more info.
I run into this problem too and still be finding the answer.
take care that authorizationStatus requires iOS4.2+ and + (BOOL)locationServicesEnabled requires iOS4.0... And for previous iOS versions, it is - (BOOL)locationServicesEnabled...
- (BOOL) enableLocationServices
{
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.distanceFilter = 10;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
[self.mapview setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
return YES;
}
else
{
return NO;
}
}