美文网首页
iOS电话拨打和短信发送

iOS电话拨打和短信发送

作者: 你的小福蝶 | 来源:发表于2019-01-10 10:04 被阅读8次
    功能开发中,遇到问题会跟随更新,也请各路大神给予意见(开发环境基于iOS9.0+,Xcode10.1,Objective-C)

    拨打电话

    iOS10之前使用CTCallCenter

    //iOS10之前
    #import <CoreTelephony/CTCall.h>
    #import <CoreTelephony/CTCallCenter.h>
    //iOS10之后
    #import <CallKit/CXCall.h>
    #import <CallKit/CXCallObserver.h>
    
    @interface GridmanagerViewController ()<CXCallObserverDelegate>
    
    @property (nonatomic , strong) CTCallCenter *callCenter;
    
    @property (nonatomic , strong) CXCallObserver* callObserver;  //注意这里需要设置为全局
    
    @end
    
    @implementation GridmanagerViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self callUserMethodWith:@{@"phoneNum":@"10086"}];
    
    }
    
    //CallUser
    - (void)callUserMethodWith:(NSDictionary *)modelDic{
        //拨打
        _phone = [modelDic objectForKey:@"phoneNum"]; 
        NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",_phone ? _phone : @""];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
        [self.view addSubview:callWebview];
        //设置监听
        if (@available(iOS 11.0, *)) {
            self.callObserver = [CXCallObserver new];
            [self.callObserver setDelegate:self queue:dispatch_get_main_queue()];
        }else{
            self.callCenter = [CTCallCenter new];
            
            @VPWeakObj(self);
            self.callCenter.callEventHandler = ^(CTCall *call) {
                @VPStrongObj(self);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    if ([call.callState isEqualToString:CTCallStateDialing]) {
                        NSLog(@"电话拨出");
                        self.callStatus = @"3";
                    } else if ([call.callState isEqualToString:CTCallStateConnected]) {
                        NSLog(@"电话接通");
                        self.callStatus = @"5";
                    } else if ([call.callState isEqualToString:CTCallStateDisconnected]) {
                        NSLog(@"电话挂断");
                        if (self.callStatus) {
                            if (![self.callStatus isEqualToString:@"5"]) {
                                self.callStatus = @"3";
                            }
                        }else{
                            self.callStatus = @"3";
                        }
                        [self requestSendCallStatus];
                        
                    } else if ([call.callState isEqualToString:CTCallStateIncoming]) {
                        NSLog(@"电话被叫");
                    } else {
                        NSLog(@"电话其他状态");
                        self.callStatus = @"4";
                    }
                });
            };
        }
    }
    
    #pragma mark - CXCallObserverDelegate
    
    - (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
        
        NSLog(@"outgoing :%d  onHold :%d   hasConnected :%d   hasEnded :%d",call.outgoing,call.onHold,call.hasConnected,call.hasEnded);
        //    "联系状态(1. 未联系 2.挂断 3.未接通  4.未知  5.已拨通)"
        if (call.outgoing) {
            self.callStatus = @"3";
            if (call.hasConnected) {
                self.callStatus = @"5";
            }
            if (call.hasEnded){
                if (call.hasConnected) {
                    
                }else{
                    self.callStatus = @"3";
                }
                [self requestSendCallStatus];
            }
            
        }else{
            self.callStatus = @"3";
        }
        
        /*
         拨打:  outgoing :1  onHold :0   hasConnected :0   hasEnded :0
         拒绝:  outgoing :1  onHold :0   hasConnected :0   hasEnded :1
         链接:  outgoing :1  onHold :0   hasConnected :1   hasEnded :0
         挂断:  outgoing :1  onHold :0   hasConnected :1   hasEnded :1
         对方未接听时挂断:  outgoing :1  onHold :0   hasConnected :0   hasEnded :1
         
         新来电话:    outgoing :0  onHold :0   hasConnected :0   hasEnded :0
         保留并接听:  outgoing :1  onHold :1   hasConnected :1   hasEnded :0
         另一个挂掉:  outgoing :0  onHold :0   hasConnected :1   hasEnded :0
         保持链接:    outgoing :1  onHold :0   hasConnected :1   hasEnded :1
         对方挂掉:    outgoing :0  onHold :0   hasConnected :1   hasEnded :1
         */
    }
    
    //向后端记录接听状态
    - (void)requestSendCallStatus{
        //_callStatus  &  _phone
    }
    
    -(void)dealloc{
        [_callObserver setDelegate:nil queue:dispatch_get_main_queue()];
        _callObserver = nil;
    }
    
    参考文章:

    https://www.jianshu.com/p/fc8a950c013d
    http://www.zhimengzhe.com/IOSkaifa/217965.html


    发送短信

    #import <MessageUI/MFMailComposeViewController.h>
    #import <MessageUI/MFMessageComposeViewController.h>
    
    -(void)sendSysMsg{
        if( [MFMessageComposeViewController canSendText] )// 判断设备能不能发送短信
        {
            NSString *phone = @"10086";
            MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
            // 设置短信内容
            vc.body = @"查询余额";
            // 设置收件人列表
            vc.recipients = @[phone];  // 号码数组
            // 设置代理
            vc.messageComposeDelegate = self;
            // 显示控制器
            [self presentViewController:vc animated:YES completion:nil];
        }
    }
    
    //发送短信回调
    - (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result
    {
        // 关闭短信界面
        [controller dismissViewControllerAnimated:YES completion:nil];
        switch (result){
            case MessageComposeResultSent:
                NSLog(@"发送成功");
                [self requestSendMsgStatus];    //通知后端发送成功
                break;
            default:
                break;
        }/*发送结果:
            MessageComposeResultCancelled,
            MessageComposeResultSent,
            MessageComposeResultFailed*/
    }
    
    

    相关文章

      网友评论

          本文标题:iOS电话拨打和短信发送

          本文链接:https://www.haomeiwen.com/subject/whjorqtx.html