这里我有2个观点:
用户在 LoginViewController 中登录后,将转到包含地图视图的 WallViewController 。首先,它会要求用户访问当前位置的权限。如果用户选择允许访问,则不会有任何问题。但是,如果用户选择不允许访问,则用户将从 WallViewController 退回到 LoginViewController 。 我想做的是让用户保留在WallViewController中,即使无法访问当前位置。我该如何实现?以下是我的相关代码。
LoginViewController.m
[PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
// Tear down the activity view in all cases.
[activityView.activityIndicator stopAnimating];
[activityView removeFromSuperview];
if (user) {
//***************call WallViewController here*********
PAWWallViewController *wallViewController = [[PAWWallViewController alloc] initWithNibName:nil bundle:nil];
[(UINavigationController *)self.presentingViewController pushViewController:wallViewController animated:NO];
[self.presentingViewController dismissModalViewControllerAnimated:YES];
//****************************************************
} else {
// Didn't get a user.
NSLog(@"%s didn't get a user!", __PRETTY_FUNCTION__);
// Re-enable the done button if we're tossing them back into the form.
doneButton.enabled = [self shouldEnableDoneButton];
UIAlertView *alertView2 = nil;
if (error == nil) {
// the username or password is probably wrong.
NSLog(@"1");
alertView2 = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Couldn’t log in:\nThe username or password were wrong.", nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
alertView2.tag = kSecondAlertViewTag;
} else {
// Something else went horribly wrong:
NSLog(@"2");
alertView2 = [[UIAlertView alloc] initWithTitle:[[error userInfo] objectForKey:@"error"] message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
alertView2.tag = kSecondAlertViewTag;
}
[alertView2 show];
// Bring the keyboard back up, because they'll probably need to change something.
[usernameField becomeFirstResponder];
}
}];
WallViewController.m
- (void)viewDidLoad {
// ...
[self startStandardUpdates];
}
- (void)startStandardUpdates {
if (nil == locationManager) {
locationManager = [[CLLocationManager alloc] init];
}
locationManager.delegate = self;
// This part was added due to a location authorization issue on iOS8
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
if ([self._locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self._locationManager requestWhenInUseAuthorization];
}
self.mapView.showsUserLocation = YES;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Set a movement threshold for new events.
locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
CLLocation *currentLocation = locationManager.location;
if (currentLocation) {
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.currentLocation = currentLocation;
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(@"%s", __PRETTY_FUNCTION__);
switch (status) {
case kCLAuthorizationStatusAuthorized:
NSLog(@"kCLAuthorizationStatusAuthorized");
// Re-enable the post button if it was disabled before.
self.navigationItem.rightBarButtonItem.enabled = YES;
[locationManager startUpdatingLocation];
break;
//**********This alert will show up, once click Ok it will go back to its caller which is LoginViewController***********
case kCLAuthorizationStatusDenied:
NSLog(@"kCLAuthorizationStatusDenied");
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"This app can’t access your current location.\n\nTo view nearby posts or create a post at your current location, turn on access for this app to your location in the Settings app under Location Services.", nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
[alertView show];
// Disable the post button.
self.navigationItem.rightBarButtonItem.enabled = NO;
}
break;
//**********************************************************************************************************************
case kCLAuthorizationStatusNotDetermined:
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"kCLAuthorizationStatusRestricted");
break;
default:break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%s", __PRETTY_FUNCTION__);
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.currentLocation = newLocation;
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"%s", __PRETTY_FUNCTION__);
NSLog(@"Error: %@", [error description]);
if (error.code == kCLErrorDenied) {
[locationManager stopUpdatingLocation];
} else if (error.code == kCLErrorLocationUnknown) {
// todo: retry?
// set a timer for five seconds to cycle location, and if it fails again, bail and tell the user.
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error retrieving location", nil)
message:[error description]
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Ok", nil), nil];
[alert show];
}
}
鉴于上面的代码,我在登录后发现的是会有一个弹出窗口询问用户是否允许当前位置访问。如果用户选择“否”,则会出现一个警告视图,说明“此应用无法访问您当前的位置。要查看附近的帖子或在您当前位置创建帖子,请将此应用的访问权限设置为您在位置服务下的设置应用程序。“点击”确定“后,它将返回到 LoginViewController 的调用者,但我希望它保留在 WallViewController 中。这里需要一些帮助。提前谢谢。
答案 0 :(得分:0)
我在下面的代码中将4
更改为delegate:self
,它解决了问题。
delegate:nil