通过不同视图从AppDelegate调用用户位置

时间:2012-12-16 00:26:37

标签: objective-c ios xcode cllocationmanager

我正在尝试使用我的appDelegate从另一个视图中获取用户的位置。我可以将它保存在NSUserDefaults中,但我想知道是否有更好的方法来实现它。

我的AppDelegate包含:

- (NSString *)deviceLocation
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
locationManager.location.coordinate.latitude, 
locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];

return userCoordinates;

}

然后我想用另一个页面调用用户的位置:

//Get the User's Location
NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];

1 个答案:

答案 0 :(得分:1)

这不起作用,因为AppDelegate中的deviceLocation方法不返回任何内容。

如果要提醒当前位置,deviceLocation应该返回一些内容(即带有当前位置的NSString)。

然后用这样的东西显示它

NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];

//Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Location" 
message:location delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, 
nil];
[alert show];