使用UIApplication sharedApplication打电话

3
我创建了一个按钮,并将其链接到以下openPhone方法。但是它没有起作用。错误消息显示“Thread 1:Program received signal: "SIGABRT"。” 我需要做些什么让它工作吗?谢谢。
-(IBAction)openPhone{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://+886227637978"]];
}

我不认为崩溃是由于你的这种方法。你能展示一下控制台中显示的信息吗? - Dip Dhingani
2个回答

4

请尝试以下代码:self.phone是包含电话号码的NSString。

    NSString *telephoneString=[self.phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSMutableString *str1=[[NSMutableString alloc] initWithString:telephoneString];
    [str1 setString:[str1 stringByReplacingOccurrencesOfString:@"(" withString:@""]];
    [str1 setString:[str1 stringByReplacingOccurrencesOfString:@")" withString:@""]];
    [str1 setString:[str1 stringByReplacingOccurrencesOfString:@"-" withString:@""]];
    [str1 setString:[str1 stringByReplacingOccurrencesOfString:@" " withString:@""]];
    telephoneString = [@"tel://" stringByAppendingString:str1];
    [str1 release];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telephoneString]];

0
-(IBAction)openPhone:(id)sender{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://+886227637978"]];
}

你可能还需要在 tel:// 中删除 +。不过我没有测试过这些。

电话号码看起来也有点长。不过可能只是一个国家的事情。

另外,请确保它已经在你的 IB 中连接,并且在你的 .h 文件中指定了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接