0

我必须使用两个按钮放置一个警报视图,但这些按钮不会打开网址。我不知道错误。请帮忙。

这是代码:

-(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

4 回答 4

1

您必须为您的 urlString 进行 url 编码,因为它包含一些特殊字符。

NSString *ourPath = @"http://maps.apple.com/?q=Plaça+del+Rei+43003+Tarragona";
ourPath=[ourPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
于 2013-07-31T13:57:07.467 回答
0
clickedButtonAtIndex... 

它是的委托方法UIAlertView,该方法用于与所选按钮相关的触发动作UIAlertView

例如 :

- (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)
  }
}

所以,为什么你对两个 URL 使用两个 UIAlertView,将 url 放在单独的按钮块中,在UIAlertView.

于 2013-07-31T13:33:14.353 回答
0

我觉得你的发送 URL 是错误的

它应该是

maps.google.com//?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking

反而

comgooglemaps://?daddr=Plaça+del+Rei+43003+Tarragona&directionsmode=walking
于 2013-07-31T13:36:27.820 回答
0
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]];
于 2013-10-13T05:17:37.247 回答