Reputation: 13
I have to put an alert view with the two buttons, but the buttons don't open the urls. I don't know the error. Help please.
Here's the code:
-(IBAction)showAlertView {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Obrir en..."
message:@"Es pot requirir la aplicació de Google Maps"
delegate:self
cancelButtonTitle:@"Millor no..."
otherButtonTitles:@"Mapes",@"Google Maps",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Mapes"])
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
}
if([title isEqualToString:@"Google Maps"])
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = @"comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
}
}
Upvotes: 0
Views: 160
Reputation: 1
NSString *enc = [match.location stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *location = [NSString stringWithFormat:@"%@%@", @"http://maps.google.com/maps?q=",enc];
NSLog(@"%@", location);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:location]];
Upvotes: 0
Reputation: 577
You have to do url encoding for your urlString because it contain some special character.
NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona";
ourPath=[ourPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Upvotes: 1
Reputation:
I thing your send URL is wrong
it should be
maps.google.com//?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking
instead
comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking
Upvotes: 0
Reputation: 47049
clickedButtonAtIndex...
It is delegate method of UIAlertView
, This method is use for fire action related to selected button of UIAlertView
for example :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
/// write code for button 1 of UIAlertView; (here put 1 URL)
}
else if(buttonIndex == 2)
{
/// write code for button 2 of UIAlertView; (here put 2 URL)
}
}
So, why you use two UIAlertView for TWO URL, put url in separated block of button clicked in delegate method of UIAlertView
.
Upvotes: 0