質問

I'm developing an app that will be able to sense the user's location and then call a specific phone-number depending on that location.

I have this method for the actual phone call, - (IBAction)phone { [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"tel:123"]];

Then I have this method for getting the user's location.

 NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
    if (error == nil && [placemarks count] > 0) {
        placemark = [placemarks lastObject];
        _addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                             placemark.subThoroughfare, placemark.thoroughfare,
                             placemark.postalCode, placemark.locality,
                             placemark.administrativeArea,
                             placemark.country];
    } else {
        NSLog(@"%@", error.debugDescription);
    }


} ];

Depending on the user's "placemark.locality", I want to make the phone call to a different number. Any ideas? Thank you in advance

役に立ちましたか?

解決

You could have a NSDictionary mapping localities with phone numbers, for example:

NSDictionary *localityToPhoneNumber = @{"Paris": @"+338123710", @"London": @"+412345678"};

Then you can get the phone number that belongs to a locality and dial it:

NSString *phoneNumber = [localityToPhoneNumber objectForKey:placemark.locality];
NSString *tel = [NSString stringWithFormat:@"tel:%@", phoneNumber];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:tel];

他のヒント

Supposing this is a best-practice question: Map to an NSDictionary, then select by placemark.locality, then place the call.

NSDictionary *map = @{
                      @"New York" : @"+1 212-226-3126",
                      @"San Francisco" : @"+1 415-392-0202"
                      };

if ([[map allKeys] containsObject:placemark.locality])
{ phoneNumber = [map objectForKey:placemark.locality]; }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top