功能开发中,遇到问题会跟随更新,也请各路大神给予意见(开发环境基于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*/
}

本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !