iPhone:Core Location popup issue

会有一股神秘感。 提交于 2019-12-11 00:17:09

问题


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….


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/8252988/iphonecore-location-popup-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!