Iphone 从AppDelegate获取当前位置

Iphone 从AppDelegate获取当前位置,iphone,ios,cllocationmanager,Iphone,Ios,Cllocationmanager,我在appDelegate中有一个方法,它获取纬度和经度,并返回一个字符串,我可以在任何viewController中使用该字符串 AppDelegate: -(void)StartUpdating { locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; locManager.distanceFilter = kCLDistanceFilterNone; // whe

我在appDelegate中有一个方法,它获取纬度和经度,并返回一个字符串,我可以在任何viewController中使用该字符串

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];  
但我在车里撞车了

str = [str stringByAppendingString:strLatitude];  
returnLatLongString

我知道我正在崩溃,因为那时的strlatude没有任何价值。但是我怎么才能解决这个问题呢?我怎么还能有更新后的纬度和经度值


另外,我不想在ViewController中使用locationManager。为了在所有ViewController中获取当前位置,我在appDelegate中进行了此操作

创建
AppDelegate
类的对象时出错

要创建对象,只需编写以下代码:

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

已完成。

您在创建
AppDelegate
类的对象时犯了一个错误

要创建对象,只需编写以下代码:

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

已完成..

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

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

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

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

崩溃可能是因为
str
是一个NSString,因此是不可变的。在这种情况下,将其分配回自身是有问题的。只需使用
stringWithFormat

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

崩溃可能是因为
str
是一个NSString,因此是不可变的。在这种情况下,将其分配回自身是有问题的。只需使用
stringWithFormat

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

我认为您需要在
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
方法。如果它第一次到达这一点,它们就不会初始化,所以会崩溃。

我认为您需要在
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
方法。如果它第一次到达这一点,它们就不会被初始化,因此会崩溃。

代码中有两个潜在的问题

首先,确保在头文件中将
strlatude
strlongite
声明为
strong
属性:

@property (nonatomic, strong) NSString * strLatitude;    
@property (nonatomic, strong) NSString * strLongitude;
使用
@synthesis
自动为它们生成适当的getter和setter,并将适当的值分配给它们,如下所示:

float latitude = newLocation.coordinate.latitude;
self.strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
self.strLongitude = [NSString stringWithFormat:@"%f", longitude];
确保它们在被赋值后不会被释放,因为
[nsstringwithformat:
返回
自动释放的
对象

其次,在
[locManager startUpdatingLocation]之后在系统交付第一次位置更新之前需要一段时间。因此,您需要检查
strlatude
strLongitude
是否已被赋值,然后再尝试用它们构造另一个字符串。类似于此的操作应该可以完成此工作:

-(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


希望这有帮助…

代码中有两个潜在问题

首先,确保在头文件中将
strlatude
strlongite
声明为
strong
属性:

@property (nonatomic, strong) NSString * strLatitude;    
@property (nonatomic, strong) NSString * strLongitude;
使用
@synthesis
自动为它们生成适当的getter和setter,并将适当的值分配给它们,如下所示:

float latitude = newLocation.coordinate.latitude;
self.strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
self.strLongitude = [NSString stringWithFormat:@"%f", longitude];
确保它们在被赋值后不会被释放,因为
[nsstringwithformat:
返回
自动释放的
对象

其次,在
[locManager startUpdatingLocation]之后在系统交付第一次位置更新之前需要一段时间。因此,您需要检查
strlatude
strLongitude
是否已被赋值,然后再尝试用它们构造另一个字符串。类似于此的操作应该可以完成此工作:

-(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


希望这有帮助……

OP可能已经将strLatitude和strLongtude定义为IVAR,否则应用程序不会首先生成。OP可能已经将strLatitude和strLongtude定义为IVAR,否则应用程序不会首先生成。in
AppDelegate*AppDelegate=[AppDelegate sharedAppDelegate]
我认为作者做了正确的事情:从sharedApplication返回委托,如
return(AppDelegate*)[[UIApplication sharedApplication]delegate]
.In
AppDelegate*AppDelegate=[AppDelegate sharedAppDelegate]
我认为作者做了正确的事情:从sharedApplication返回委托,如
return(AppDelegate*)[[UIApplication sharedApplication]delegate]