iPhone:Core Location popup issue

2019-07-22 11:28发布

When I install my app in iphone and run first time then it ask user permission for core location service. Here is the image for simulator.

In my app, my first view of application needs current location and based on location it lists some events. If application couldn't get location it shows the default list of events.

So, I would like to know that Is it possible to hold the application flow until user click on either " Don't allow" or "ok " button ?
I know if user clicks on "Don't allow" then kCLErrorDenied error will get fired.

Currently what happens, if user does not click on any of buttons, application displays listing page with default list (without location). And after that if user clicks on "ok " button then nothing happens !!! How can I refresh the page upon "ok" button click ?

Thanks….

enter image description here

2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-22 11:43

Yes, just don't do anything until those delegate methods are invoked. When they click 'OK', that's just a signal for Cocoa to go and then try to retrieve the user's location - you should structure your app so that when the CLLocationManager has a location or can't get one, your app then continues.

You wouldn't want to say, pause your app until the location returns/fails; that's not what object oriented development is about.

查看更多
Juvenile、少年°
3楼-- · 2019-07-22 11:51

In your view logic wait until the CoreLocation delegates of didUpdateToLocation or didFailWithError are called. Have those methods call/init your list and UI data fill.

Sample Controller:

Header

@interface MyCLController : NSObject <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
}

@property (nonatomic, retain) CLLocationManager *locationManager;  

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error;

@end

Code

#import "MyCLController.h"

@implementation MyCLController

@synthesize locationManager;

- (id) init {
    self = [super init];
    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self; // send loc updates to myself
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"Location: %@", [newLocation description]);

    // FILL YOUR VIEW or broadcast a message to your view.

}

- (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", [error description]);

    // FILL YOUR VIEW or broadcast a message to your view.
}

- (void)dealloc {
    [self.locationManager release];
    [super dealloc];
}

@end
查看更多
登录 后发表回答