2012-04-17 116 views
1

我有一个方法在我的appDelegate获取经纬度并返回一个字符串,我可以在任何viewController中使用。从AppDelegate获取当前位置

的AppDelegate

-(void)StartUpdating 
{  
    locManager = [[CLLocationManager alloc] init]; 
    locManager.delegate = self; 
    locManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locManager startUpdatingLocation]; 
} 

#pragma mark 
#pragma mark locationManager delegate methods 


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

    float latitude = newLocation.coordinate.latitude; 
    strLatitude = [NSString stringWithFormat:@"%f",latitude]; 
    float longitude = newLocation.coordinate.longitude; 
    strLongitude = [NSString stringWithFormat:@"%f", longitude]; 
    //[self returnLatLongString:strLatitude:strLongitude]; 

} 

-(NSString*)returnLatLongString 
{  
    NSString *str = @"lat="; 
    str = [str stringByAppendingString:strLatitude]; 
    str = [str stringByAppendingString:@"&long="]; 
    str = [str stringByAppendingString:strLongitude]; 

    return str; 
} 

我打电话StartUpdating在应用程序的启动。现在,在我的viewController我调用这个方法:

AppDelegate *appDelegate=[AppDelegate sharedAppDelegate]; 
NSString *str = [appDelegate returnLatLongString]; 

但我在returnLatLongString得到一个崩溃的

str = [str stringByAppendingString:strLatitude]; 

我知道我正在崩溃,因为当时strlatitude没有任何价值。但我该如何解决这个问题?我怎样才能更新纬度和经度的值?我不想在viewControllers中使用locationManager。为了能够在所有viewController中获取当前位置,我在appDelegate中完成了它。

回答

4

坠毁可能是因为str是一个NSString,等于是不可变。在这种情况下将它分配给自己是有问题的。这将是简单的只是使用stringWithFormat

NSString *str = [NSString stringWithFormat: @"lat=%@&long=%@", strLatitude, strLongitude]; 
0

你是做了一个错误与创建AppDelegate类的对象..

创建的对象只是写这样的代码:

AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSString *str = [appDelegate returnLatLongString]; 

它做..

+0

在'AppDelegate中*的appDelegate = [AppDelegate中sharedAppDelegate]'我觉得作者做了正确的事情:从sharedApplication返回委托像'回报(AppDelegate中*)[[UIApplication的sharedApplication]委托];'。 – 2015-04-21 07:25:49

0

导入您AppDelegate.h文件。然后
使用下面的代码:

MyAppDelegate *myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSString *result = [myAppDelegate returnLatLongString]; 
0

我认为你需要在你的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法来初始化这些变量(strLatitude,strLongitude)。如果它第一次触及这一点,它们不会被初始化,所以你会崩溃。

+0

OP很可能已经将strlaitude和strlitude定义为ivars,否则应用程序将无法在第一时间建立。 – 2012-04-18 08:44:20

0

你的代码有两个潜在的问题。

首先确保你申报你的头文件strLatitudestrLongitudestrong属性:

@property (nonatomic, strong) NSString * strLatitude;  
@property (nonatomic, strong) NSString * strLongitude; 

使用@synthesize自动生成他们适当的getter和setter和分配适当的值,对他们说:

float latitude = newLocation.coordinate.latitude; 
self.strLatitude = [NSString stringWithFormat:@"%f",latitude]; 
float longitude = newLocation.coordinate.longitude; 
self.strLongitude = [NSString stringWithFormat:@"%f", longitude]; 

,以确保他们他们被分配一个值后,没有得到释放,因为[NSString stringWithFormat:]回报autoreleased个对象。

其次,在[locManager startUpdatingLocation];之后,系统提交第一个位置更新需要一些时间。因此,在尝试使用它们构造另一个字符串之前,您需要检查strLatitudestrLongitude是否已被赋值。类似的事情这应该做的工作:

-(NSString*)returnLatLongString 
{ 
    if (self.strLatitude == nil || self.strLongitude == nil) 
     return nil; 

    NSString *str = @"lat="; 
    str = [str stringByAppendingString:strLatitude]; 
    str = [str stringByAppendingString:@"&long="]; 
    str = [str stringByAppendingString:strLongitude]; 

    return str; 
} 

那当然视图控制器谁是要使用[AppDelegate returnLatLongString]需要检查返回值不是nil

希望这有助于...