2011-11-24 63 views
5

当我在iphone中安装我的应用程序并首次运行时,它会询问用户对核心位置服务的权限。这里是模拟器的图像。iPhone:核心位置弹出问题

在我的应用程序中,我的第一个应用程序视图需要当前位置,并根据位置列出了一些事件。如果应用程序无法获取位置,则会显示默认的事件列表。

所以,我想知道是否有可能持有应用程序流程,直到用户点击“Don't allow”或“ok”按钮?
我知道如果用户点击“不允许”,那么kCLErrorDenied错误将被解雇。

当前会发生什么,如果用户没有点击任何按钮,应用程序将显示带有默认列表(无位置)的列表页面。之后,如果用户点击“ok”按钮,则没有任何反应!如何在“ok”按钮点击后刷新页面?

谢谢...。

enter image description here

回答

1

是的,只是不直到调用这些委托方法做任何事情。当他们点击“确定”时,这只是Cocoa的一个信号,然后尝试检索用户的位置 - 您应该构建应用程序,以便在CLLocationManager有位置或无法获取位置时,您的应用程序会继续。

你不会说,暂停你的应用程序,直到位置返回/失败;这不是面向对象的开发。

0

在您的视图逻辑中等待,直到调用didUpdateToLocation或didFailWithError的CoreLocation委托。让这些方法调用/ init你的列表和UI数据填充。

样品控制器:

部首

@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 

代码

#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