简体   繁体   中英

UIAlertView doesn't work

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];
    }
}

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.

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.

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.");

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM