【问题标题】:Get current location from AppDelegate从 AppDelegate 获取当前位置
【发布时间】:2012-04-17 12:32:23
【问题描述】:

我的 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.

我知道我会崩溃,因为当时 strLatitude 没有任何价值。但是我该如何解决这个问题?我怎样才能获得更新的纬度和经度值?

另外,我不想在 viewControllers 中使用 locationManager。为了让我可以在所有 viewControllers 中获取当前位置,我在 appDelegate 中完成了它。

【问题讨论】:

    标签: iphone ios cllocationmanager


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      导入您的 AppDelegate.h 文件。那么
      使用以下代码:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 很可能是 OP 已将 strLatitude 和 strLongitude 定义为 ivars,否则应用程序将不会首先构建。
        【解决方案4】:

        您的代码中有两个潜在问题。

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

        @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

        希望这会有所帮助...

        【讨论】:

          【解决方案5】:

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

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

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

          完成了..

          【讨论】:

          • AppDelegate *appDelegate=[AppDelegate sharedAppDelegate]; 中,我认为作者做了正确的事:像return (AppDelegate *)[[UIApplication sharedApplication] delegate]; 一样从sharedApplication 返回委托。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-22
          • 1970-01-01
          • 1970-01-01
          • 2011-07-15
          • 2016-12-26
          • 2022-10-05
          相关资源
          最近更新 更多