0

我需要我的应用程序在任何时候调用一个方法是从不在前台“带回来”。我目前正在使用 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;

}
4

1 回答 1

1

获取用户位置的过程是异步的,所以只有在获取到用户位置后才需要更新UI。从您当前的实现来看,UI 可能会在获取位置之前更新。

您需要实现位置管理器的委托并在那里进行更新,例如使用 NSNotificationCenter:

//iOS 5 and before
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.currentLocation = newLocation;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CurrentLocationObtained" object:nil];
}

//iOS 6
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {

    self.currentLocation = [locations objectAtIndex:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CurrentLocationObtained" object:nil];
}

根据您当前的代码,您需要将 AppDelegate 设置为 CLLocationManagerDelegate。然后当您实例化位置管理器时,设置委托:

locationManager.delegate = self;

看看这里,特别是关于如何正确设置委托的第一个答案:设置 CLLocationManager 的委托(两种不同的方式,它们是否相等?)

于 2013-05-27T02:41:26.653 回答