微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 无法拨打电话

我想拨打电话,我使用下面的代码.

- (void)callPhoneNumber:(Nsstring *)phoneNumber
{
    UIWebView * webView2 = [[UIWebView alloc] init];

    // Remove non-digits from phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Make a call
    NSURL * url = [NSURL URLWithString:[Nsstring stringWithFormat:@"tel:%@",phoneNumber]];
    [webView2 loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView2];
}

在打电话之前,我要求用户使用UIActionSheet选择一个选项.当用户选择一个选项时,根据我拨打电话.

问题是如果我不显示UIActionSheet并直接拨打电话,上面的代码工作正常.它会提示警告,要求用户确认拨打电话.但是在显示UIActionSheet后,如果我调用上面的方法,那么它不显示确认警报.

奇怪的是,在此之后,如果我从Xcode停止应用程序,那么应用程序就应该放置离子背景.但我也在设备主屏幕上看到确认提醒.它是如何在设备的主屏幕上显示而不是在应用程序中显示的?

以前有人遇到过吗?可能是什么原因?

编辑:我也试过下面的代码.但它有同样的问题.每当上面的webview代码工作时,代码也可以工作.当它不起作用时相同.

Nsstring *phoneNumberURL = [Nsstring  stringWithFormat:@"telprompt:%@",phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURL]];

编辑:添加操作表和委托的代码.

我首先显示警报.按下警告按钮我显示操作表.从行动表中选择选项我打电话.

# pragma mark - ALERT VIEW DELEGATE

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == MY_TAG)
    {
        // Ask to select option
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Option" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Option1",@"Option2",nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
        [actionSheet showInView:self.view];   
    }
}


#pragma mark - UIActionSheet Methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // If not pressed cancel button.i.e. selected one of the options
    if (buttonIndex != actionSheet.cancelButtonIndex)
    {
        // Place a call
        [self callPhoneNumber:@"1234567890"];
    }
}

解决方法

如果您的号码中有任何空格,那么它将无法工作,因此需要删除,当使用tel:和telprompt时:它需要是tel://或telprompt://注意额外的//.

如果您还需要其他任何评论,我已经评论了我的代码并解释了每一步,我会提供更多.

- (void)callPhoneNumber:(Nsstring *)phoneNumber
{
    // If statement to check we actually have something
    if(phoneNumber) {
        // Remove any unwanted whitespace,if there is any whitespace it will not work
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
        // Create an NSURL instance of your number
        // Note the extra `//` in the string
        // Also note you can also use: `telprompt://` instead of `tel://`
        NSURL *urlPhoneNumber = [NSURL URLWithString:[Nsstring stringWithFormat:@"tel://%@",phoneNumber]];
        // Check to make sure we can open this because iPad and iPod will not respond to phone calls
        if([[UIApplication sharedApplication] canopenURL:urlPhoneNumber]) {
            // If we can (So if iPhone really) make the call.
            [[UIApplication sharedApplication] openURL:urlPhoneNumber];
        }
    }
}

编辑额外备注

阅读有关此here的Apple文档

我会注意到Apple文档说tel:和telprompt:应该工作,不需要额外的//但我发现它仍然适用于额外的//.这确实让我感到困惑,因为技术上tel:和telprompt:是URL Schemes,并且`URL Schemes中需要额外的//.这是iPhone Development 101之前的一个很好的教程

Here是一些额外的信息

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐