我有一个视图控制器,我想检查用户是否允许应用程序使用他们的位置。如果他们接受,那么应用程序从 RootViewController 移动到 OffersViewController,如果他们拒绝,那么他们应该继续使用 OffersNoGPSViewController。
目前,如果用户说“允许”,如果他们说“不允许”,它会保留在 RootViewController 上。
当他们在触摸“确定”并允许应用程序使用他们的位置后移动到 OffersViewController 时,我也会收到这个“开始/结束外观的不平衡调用”警告。
谢谢您的帮助。这是我的代码:
- (void)locationUpdate:(CLLocation *)location {
locLabel.text = [location description];
NSLog(@"auth status is %u", [CLLocationManager authorizationStatus]);
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
[self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
} else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
[self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
}
- (void)locationError:(NSError *)error {
locLabel.text = [error description];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"SegueOffersNoGPS"])
{
OffersViewController *vc = [segue destinationViewController];
//vc.dataThatINeedFromTheFirstViewController = self.theDataINeedToPass;
NSLog(@"auth status no GPS");
}
if ([[segue identifier] isEqualToString:@"SegueOffersGPS"])
{
OffersViewController *vc = [segue destinationViewController];
//vc.dataThatINeedFromTheFirstViewController = self.theDataINeedToPass;
NSLog(@"auth status is GPS");
}
}
我完整的 RootViewController 代码:
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize CLController;
- (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.
}
-(void) viewWillAppear:(BOOL)animated {
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
NSLog(@"RootViewController");
[CLController.locMgr startUpdatingLocation];
//
// if ([CLLocationManager authorizationStatus] ==3) {
// [self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
//
//
// }
// if ([CLLocationManager authorizationStatus] !=3) {
// [self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
// }
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusDenied) {
// denied
[self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
[CLController.locMgr stopUpdatingLocation];
}
else if (status == kCLAuthorizationStatusAuthorized) {
// allowed
[self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
[CLController.locMgr stopUpdatingLocation];
}
}
- (void)locationUpdate:(CLLocation *)location {
//locLabel.text = [location description];
NSLog(@"auth status is %u", [CLLocationManager authorizationStatus]);
if ([CLLocationManager authorizationStatus] ==3) {
[self performSegueWithIdentifier: @"SegueOffersGPS" sender: self];
}
if ([CLLocationManager authorizationStatus] !=3) {
[self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
}
}
- (void)locationError:(NSError *)error {
[self performSegueWithIdentifier: @"SegueOffersNoGPS" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"SegueOffersNoGPS"])
{
self.navigationController.toolbarHidden=YES;
NSLog(@"auth status no GPS");
}
if ([[segue identifier] isEqualToString:@"SegueOffersGPS"])
{
self.navigationController.toolbarHidden=NO;
NSLog(@"auth status is GPS");
}
}
-(void)viewDidDisappear:(BOOL)animated {
[CLController.locMgr stopUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end