UIAlertView不起作用

时间:2013-08-05 23:25:35

标签: iphone ios ios6 uialertview

我有两个按钮的警报视图,但按钮不会打开网址。我不知道错误。请帮助。

以下是代码:

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

4 个答案:

答案 0 :(得分:5)

我认为地址中的特殊字符(ç)会关闭NSURL。在将其传递给NSURL初始化程序之前,请尝试使用NSString的stringByAddingPercentEscapesUsingEncoding:方法对其进行编码。

答案 1 :(得分:1)

请检查我们解雇的网址。

if([ourApplication canOpenURL:ourURL])
    [ourApplication openURL:ourURL];
else
    NSLog(@"URL is not valid.");

当我检查两个网址时,他们无法打开。 您可以使用上面的代码检查URL,无论URL是否能够打开。

答案 2 :(得分:1)

你必须检查网址,试试这个:

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

}

答案 3 :(得分:0)

首先,您应该在第二个条件中使用elseif,因为如果第一个不是真的,您只想要触发第二个条件。其次,它似乎是您在@“Mapes”中使用的URL ...我测试了这个并且该URL似乎是罪魁祸首。当我将该URL更改为另一个测试URL时,它可以正常工作。