2012-04-17 126 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

希望這有助於...