我需要我的应用程序在任何时候调用一个方法是从不在前台“带回来”。我目前正在使用 UIApplicationDidBecomeActiveNotification 执行此操作,如下所示。该方法似乎可以正确调用,因为如果我在 appReturnsActive 方法中放置警报,它会发出警报。
但是,在 appReturnsActive 中,我尝试根据用户的位置更新 UI,而不是简单的警报。我将用户的地理坐标传递给 PHP 文件,并返回 1 或 0。如果 PHP 文件返回 1,我想显示按钮 1 和 2。如果 PHP 文件返回 0,我想显示按钮 3和 2。
这似乎不是每次都正确更新。这是因为在应用程序有机会找到用户位置之前 UI 正在更新吗?任何帮助都会很棒!
谢谢!
ViewDidAppear:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification
object:nil];
应用程序恢复时调用的方法:
- (void)appReturnsActive{
NSString *userLatitude =[(AppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(AppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
NSString *placeLatitude = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedLatitude"];
NSString *placeLongitude = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedLongitude"];
NSString *distanceURL = [NSString
stringWithFormat:@"http://www.website.com/location.php?
lat1=%@&lon1=%@&lat2=%@&lon2=%@",userLatitude, userLongitude, placeLatitude,
placeLongitude];
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
NSString *distanceInFeet = [[NSString alloc] initWithData:distanceURLResult
encoding:NSUTF8StringEncoding];
if ([distanceInFeet isEqualToString:@"1"])
{
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Button 1"
style:UIBarButtonItemStyleBordered target:self action:@selector(buttoneOneAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button 2"
style:UIBarButtonItemStyleBordered target:self action:@selector(buttonTwoAction)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo, btnGoTwo,
nil];
}
if ([distanceInFeet isEqualToString:@"0"])
{
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Button 3"
style:UIBarButtonItemStyleBordered target:self action:@selector(buttonThreeAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button 2"
style:UIBarButtonItemStyleBordered target:self action:@selector(buttonTwoAction)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo, btnGoTwo,
nil];
}
}
AppDelegate 中的纬度和经度方法:
- (NSString *)getUserLatitude
{
NSString *userLatitude = [NSString stringWithFormat:@"%f",
locationManager.location.coordinate.latitude];
return userLatitude;
}
- (NSString *)getUserLongitude
{
NSString *userLongitude = [NSString stringWithFormat:@"%f",
locationManager.location.coordinate.longitude];
return userLongitude;
}
App Delegate 中的位置管理器:
- (NSString *)getUserCoordinates
{
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;
}