简体   繁体   English

无法拨打电话

[英]Not able to make phone call

I want to make a phone call and I use below code for that. 我想拨打电话,我使用下面的代码。

- (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];
}

Before making a call I ask user to choose an option using UIActionSheet . 在打电话之前,我要求用户使用UIActionSheet选择一个选项。 When user selects an option, based on that I place a call. 当用户选择一个选项时,根据我拨打电话。

Issue is that above code works fine if I don't display UIActionSheet and directly place a call. 问题是如果我不显示UIActionSheet并直接拨打电话,上面的代码工作正常。 It prompts an alert asking user's confirmation to make a call. 它会提示警告,要求用户确认拨打电话。 But after displaying UIActionSheet if I call above method then it does not display confirmation alert. 但是在显示UIActionSheet如果我调用上面的方法,那么它不显示确认警报。

Weird thing is that after this if I stop app from Xcode, then app is put ion background as it should. 奇怪的是,在此之后,如果我从Xcode停止应用程序,那么应用程序就应该放置离子背景。 But also I see that confirmation alert on device's home screen. 但我也在设备主屏幕上看到确认提醒。 How it is displayed on device's home screen and not in app? 它是如何在设备的主屏幕上显示而不是在应用程序中显示的?

Has anyone faced it before? 以前有人遇到过吗? What can be the reason? 可能是什么原因?

EDIT : I also tried below code. 编辑:我也试过下面的代码。 But it has same issue. 但它有同样的问题。 Whenever above webview code works below code also works. 每当上面的webview代码工作时,代码也可以工作。 Same for when it does not work. 当它不起作用时相同。

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

Edit : Adding code for action sheet and delegate. 编辑:添加操作表和委托的代码。

I first display an alert. 我首先显示警报。 On pressing alert button I display action sheet. 按下警告按钮我显示操作表。 And choosing option from action sheet I place a call. 从行动表中选择选项我打电话。

# 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"];
    }
}

If there are any whitespace in your number it will not work so this needs to be removed, also when using tel: and telprompt: it needs to be tel:// or telprompt:// note the extra // . 如果您的号码中有任何空格,那么它将无法工作,因此需要删除,当使用tel:telprompt:它需要是tel://telprompt://注意额外的//

I have commented my code with explanation of each step if you need anything else just comment and I will provide more. 如果您还需要其他任何评论,我已经评论了我的代码并解释了每一步,我会提供更多。

- (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];
        }
    }
}

Edit Extra Notes 编辑额外备注

Have a read of the Apple documentation around this here 这里阅读Apple文档

I will note that the Apple Documentation says that tel: and telprompt: should work and no need for the extra // but I have found that it still works with the extra // . 我会注意到Apple文档说tel:telprompt:应该工作,不需要额外的//但我发现它仍然适用于额外的// This does confuse me a little as technically tel: and telprompt: are URL Schemes and the extra // are required in a `URL Schemes. 这确实让我感到困惑,因为技术上tel:telprompt:URL Schemes并且`URL Schemes中需要额外的// Here is a good tutorial on them by iPhone Development 101 这是iPhone开发101的一个很好的教程

Here is a bit of additional information 是一些额外的信息

Use OpenURL to make call using URL. 使用OpenURL使用URL进行呼叫。

- (void)callPhoneNumber:(NSString *)phoneNumber
{
    // Remove non-digits from phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Make a call
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
    [[UIApplication sharedApplication] openURL:url];
}

Instead of using UIWebView, try the following: 请尝试以下方法,而不是使用UIWebView:

NSURL *phoneUrl = [NSURL URLWithString:[NSString  stringWithFormat:@"telprompt:%@",phoneNumber]];

if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
    [[UIApplication sharedApplication] openURL:phoneUrl];
}

Edited: Check whether the phone calling function is called or not by placing the breakpoint. 编辑:通过放置断点检查是否调用了电话呼叫功能。 If that phone calling function is not called, replace the action sheet's button index checking function as follows: 如果未调用该电话呼叫功能,请更换操作表的按钮索引检查功能,如下所示:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
         //Code for cancel button
    }
    if (buttonIndex == 1)
    {
       //Code for calling phone call
       [self callPhoneNumber:@"1234567890"];
    }
}

Edited 2: 编辑2:

Now put it this way: 现在这样说吧:

  - (void)checkCallAlert :(NSInteger)index
 {
    UIAlertView *checkCallAlert = [[UIAlertView alloc] initWithTitle:@"Check?" message:@"Are you sure you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil];
    [checkCallAlert setTag:index];
    [checkCallAlert show];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
         //Code for cancel button
    }
    if (buttonIndex == 1)
    {

       [self checkCallAlert : buttonIndex];


    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if(alertView.tag == 1){
   //Destination 1 clicked
    //Code for calling phone call
       [self callPhoneNumber:@"1234567890"];
}
}

This would be my final solution for this. 这将是我的最终解决方案。

Try using following instead of [self callPhoneNumber ... ] to invoke callPhoneNumber after completion current update loop: 在完成当前更新循环后,尝试使用以下代替[self callPhoneNumber ... ]来调用callPhoneNumber

[self performSelector:@selector(callPhoneNumber:) withObject:@"1234567890" afterDelay:0];

Also the UIActionSheet reference document states: 此外, UIActionSheet参考文件还指出:

In iOS 4.0 and later, action sheets are not dismissed automatically when an application moves to the background 在iOS 4.0及更高版本中,当应用程序移动到后台时,操作表不会自动关闭

So you need to check in applicationWillResignActive if actionSheet is being displayed and dismiss it programmatically. 因此,如果正在显示actionSheet则需要检入applicationWillResignActive并以编程方式将其解除。

NSString * telephoneNumber;

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telephoneNumber]];

[telephoneNumber release];

尝试调用didDismissWithButtonIndex: ,以便UIActionSheet已被解雇,也许它阻止了某些东西。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM