uibutton onclick不会调用/打开网页

时间:2010-12-29 01:51:07

标签: iphone uibutton

我定义了两个按钮。其中一个单击时必须打开一个撰写邮件屏幕,另一个单击时必须调用。我将此定义如下。但按下按钮时,它不会打开

-(IBAction) phoneButtonPressed:(id) sender{
    NSString *phoneNumber = [[NSString alloc] initWithString:@"4216483330"];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

-(IBAction) mailButtonPressed:(id) sender{

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:askalibrarian@mail.pitt.edu?subject=ULS Library"]];  
}

1 个答案:

答案 0 :(得分:6)

对于手机号码,网址应以“tel:”为前缀,如下所示:

NSString *phoneNumber = [[NSString alloc] initWithString:@"tel:4216483330"];


对于邮件,问题是你在url字符串中有一个空格(在“ULS库”中)需要在将它提供给NSURL之前进行转义:

NSString *urlString = @"mailto:askalibrarian@mail.pitt.edu?subject=ULS Library";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[urlString 
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 

请参阅Apple URL Scheme Reference