2013-05-27 39 views
0

我需要我的應用程序調用任何時間從前臺不「回來」的方法。我正在使用UIApplicationDidBecomeActiveNotification來完成此操作,如下所示。該方法似乎調用正確,因爲如果我在我的appReturnsActive方法中放置一個警報,它警報就好了。呼叫用戶座標,加載PHP文件,並在UIApplicationDidBecomeActiveNotification上更新UI

但是,在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; 
} 
在應用程序委託

位置經理:

- (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; 

} 

回答

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; 

看看這裏特別是關於如何正確設置委託的第一個答案:Set delegate of CLLocationManager (Two different ways, are they equal?)

+0

感謝我使用iOS6的。是我會放在視圖控制器或我的應用程序委託? – Brandon

+0

@Brandon它取決於您設置位置管理器的代理的位置。 –

+0

您好,我已經更新了我的問題,以包含來自我的應用程序代理的搭配管理器代碼。 - 這有助於說明我可能需要做些什麼來解決這個問題嗎? – Brandon