gpt4 book ai didi

iphone - 调用用户坐标、加载 PHP 文件并根据 UIApplicationDidBecomeActiveNotification 更新 UI

转载 作者:行者123 更新时间:2023-11-29 03:53:44 28 4
gpt4 key购买 nike

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

应用程序委托(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;

}

最佳答案

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

您需要实现位置管理器的委托(delegate)并在那里进行更新,例如使用 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。然后,当您实例化位置管理器时,设置委托(delegate):

locationManager.delegate = self;

看看这里,特别是关于如何正确设置委托(delegate)的第一个答案:Set delegate of CLLocationManager (Two different ways, are they equal?)

关于iphone - 调用用户坐标、加载 PHP 文件并根据 UIApplicationDidBecomeActiveNotification 更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16765005/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com