简体   繁体   English

UIAlertView不起作用

[英]UIAlertView doesn't work

I have an alert view with the two buttons, but the buttons don't open the urls. 我有两个按钮的警报视图,但是按钮不能打开URL。 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. 我认为地址中的特殊字符(ç)导致NSURL丢掉了。 Try using the stringByAddingPercentEscapesUsingEncoding: method of NSString to encode it before passing it to the NSURL initializer. 在将其传递给NSURL初始化程序之前,请尝试使用NSString的stringByAddingPercentEscapesUsingEncoding:方法对其进行编码。

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. 当我检查两个URL时,它们都无法打开。 You can check the URL with above code whether URL is able to open or not. 您可以使用上面的代码检查URL,是否可以打开URL。

You have to check the URL's, try this: 您必须检查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.");

}

First, you should use elseif in your second condition, because you only want to fire the second if the first isn't true. 首先,您应该在第二个条件下使用elseif ,因为您只想在第二个条件不正确时触发第二个条件。 Second, it seems to be the URL you're using in @"Mapes"... I tested this and that URL seems to be the culprit. 其次,它似乎是您在@“ Mapes”中使用的URL ...我对此进行了测试,并且该URL似乎是罪魁祸首。 When I changed that URL to another test URL, it worked. 当我将该URL更改为另一个测试URL时,它起作用了。

暂无
暂无

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

相关问题 UIAlertView为什么不起作用? - Why doesn't the UIAlertView work? UIAlertView +模态视图控制器不起作用 - UIAlertView + Modal View Controller doesn't work iOS 5,ARC:UIAlertView在@catch块中不起作用 - iOS 5, ARC: UIAlertView doesn't work in @catch block 无法使UIAlertView工作 - Can't get UIAlertView to work UIAlertView中的UITextField在第二次显示时不响应剪切/复制/粘贴 - UITextField in UIAlertView doesn't respond to cut/copy/paste on second showing 当键盘出现在firstFirstResponder上时,UIAlertView不会自动向上滑动 - UIAlertView doesn't slide up Automatically when keyboard appears on becomeFirstResponder 以编程方式解除iOS 5上的UIAlertView不会调用didDismiss委托方法 - Programmatically dismissing a UIAlertView on iOS 5 doesn't call didDismiss delegate method iPhone iOS8:从iOS7.1 sdk构建并在8.0及更高版本的设备上运行时,UIAlertView的上下旋转无法正常工作 - iPhone iOS8: UIAlertView upside down rotation doesn't work fine when build from iOS7.1 sdk and run on 8.0 and above devices 从登录 UIAlertView 取消时,应用内购买不发送 SKPaymentCancelled - In-App Purchase doesn't send SKPaymentCancelled when cancelling from login UIAlertView 如果操作从UIAlertView委托开始,则从导航控制器中弹出UIViewController后不会释放它 - UIViewController doesn't get deallocated after being popped from a navigation controller, if the action starts from a UIAlertView delegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM