Reputation: 13
I have 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: 249
Reputation: 12369
You have to check the URL's, try this:
-(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
{
//0 is cancel
if(buttonIndex == 1)
{
[self openURLWithString:@"https://www.google.com/maps/preview#!q=Pla%C3%A7a+del+Rei+43003+Tarragona&data=!1m4!1m3!1d4618!2d1.2582895!3d41.1168719!4m11!1m10!4m8!1m3!1d26081603!2d-95.677068!3d37.0625!3m2!1i1024!2i768!4f13.1!17b1"];
}
if(buttonIndex == 2)
{
[self openURLWithString:@"https://www.google.com/maps/preview#!data=!1m4!1m3!1d18473!2d1.258181!3d41.1168316!4m13!3m12!1m0!1m1!1sPla%C3%A7a+del+Rei+43003+Tarragona!3m8!1m3!1d26081603!2d-95.677068!3d37.0625!3m2!1i1024!2i768!4f13.1&fid=0"];
}
}
- (void)openURLWithString:(NSString *)string{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = string;
NSURL *ourURL = [NSURL URLWithString:ourPath];
if([ourApplication canOpenURL:ourURL])
[ourApplication openURL:ourURL];
else
NSLog(@"URL is not valid.");
}
Upvotes: 1
Reputation: 5186
Please check the URL which we fired.
if([ourApplication canOpenURL:ourURL])
[ourApplication openURL:ourURL];
else
NSLog(@"URL is not valid.");
As I check with both URL, they are not able to open. You can check the URL with above code whether URL is able to open or not.
Upvotes: 1
Reputation: 633
I think the special character (ç) in the address is throwing the NSURL off. Try using the stringByAddingPercentEscapesUsingEncoding:
method of NSString to encode it before passing it to the NSURL initializer.
Upvotes: 5
Reputation: 3368
First, you should use elseif
in your second condition, because you only want to fire the second if the first isn't true. Second, it seems to be the URL you're using in @"Mapes"... I tested this and that URL seems to be the culprit. When I changed that URL to another test URL, it worked.
Upvotes: 0