OStack程序员社区-中国程序员成长平台

标题: ios - 从应用程序打开电话 (iOS) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:06
标题: ios - 从应用程序打开电话 (iOS)

谁能告诉我如何通过单击按钮打开电话?我已经将它用于 safari 并且可以正常工作

  [[UIApplication sharedApplication] openURL:[NSURL URLWithString"http://www.google.com"]];



Best Answer-推荐答案


你只需要:

NSString *phoneNumber = @"4167371111";

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString"tel://"]])
{    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[@"tel://" stringByAppendingString:[phoneNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
}

如果你想做一些更高级的事情,比如查看用户是否调用了电话,你可以使用 CTCallCenter:

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

//Create this property somewhere
@property (strong, nonatomic) CTCallCenter *callCenter;

NSString *phoneNumber = @"4167371111";

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString"tel://"]])
{
    self.callCenter = [[CTCallCenter alloc] init];
    [self.callCenter setCallEventHandler: ^(CTCall* call)
     {
         if ([call.callState isEqualToString: CTCallStateConnected]) {
             NSLog(@"Connected");
         } else if ([call.callState isEqualToString: CTCallStateDialing]) {
             NSLog(@"Dialing");
         } else if ([call.callState isEqualToString: CTCallStateDisconnected]) {
             NSLog(@"Disconnected");
         } else if ([call.callState isEqualToString: CTCallStateIncoming]) {
             NSLog(@"Incoming");
         }
     }];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[@"tel://" stringByAppendingString:[phoneNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
}

关于ios - 从应用程序打开电话 (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209342/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4