繁体   English   中英

UIAlertView不起作用

[英]UIAlertView doesn't work

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

这是代码:

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

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

请检查我们触发的网址。

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

当我检查两个URL时,它们都无法打开。 您可以使用上面的代码检查URL,是否可以打开URL。

您必须检查URL,尝试以下操作:

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

}

首先,您应该在第二个条件下使用elseif ,因为您只想在第二个条件不正确时触发第二个条件。 其次,它似乎是您在@“ Mapes”中使用的URL ...我对此进行了测试,并且该URL似乎是罪魁祸首。 当我将该URL更改为另一个测试URL时,它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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