4

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

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

6 回答 6

1

如果您的号码中有任何空格,它将不起作用,因此需要将其删除,在使用时也是如此,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];
        }
    }
}

编辑额外注释

在此处阅读 Apple 文档

我会注意到 Apple 文档说明了这一点tel:并且telprompt:应该可以工作并且不需要额外的//,但我发现它仍然适用于额外的//. 这确实让我在技术上有点困惑,tel:并且在“URL 方案”中需要额外的东西。这是iPhone Development 101的一个很好的教程telprompt:URL Schemes//

是一些附加信息

于 2014-05-12T10:42:27.437 回答
0

尝试调用 in didDismissWithButtonIndex:,以便 UIActionSheet 已经被解除,也许它正在阻止某些东西。

于 2014-05-18T21:38:00.600 回答
0

尝试使用以下而不是在完成当前更新循环后[self callPhoneNumber ... ]调用:callPhoneNumber

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

UIActionSheet参考文件还指出:

在 iOS 4.0 及更高版本中,当应用程序移至后台时,操作表不会自动关闭

因此,您需要检查applicationWillResignActive是否actionSheet正在显示并以编程方式将其关闭。

于 2014-05-12T10:04:35.437 回答
0

使用 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];
}
于 2014-05-08T11:38:21.537 回答
0
NSString * telephoneNumber;

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

[telephoneNumber release];
于 2014-05-15T06:32:46.483 回答
0

而不是使用 UIWebView,请尝试以下操作:

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

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

编辑: 通过放置断点检查电话呼叫功能是否被调用。如果没有调用该电话呼叫功能,请替换操作表的按钮索引检查功能,如下所示:

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

编辑2:

现在这样说:

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

这将是我对此的最终解决方案。

于 2014-05-08T11:39:28.773 回答