iOS详解监听系统来电(2016/06)

最近再做视频播放的项目,有个需求就是监听系统来电,遇到一些小坑,找不到合适的资料,于是总结一下。

代码如下:

CoreTelephony/CTCallCenter框架定义了一个在app运行时,监听来电的block,用来app处理在来电时的各种情况。

在回调中,定义了四周类型:

这是一个代码区块。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}

import

import

NS_ASSUME_NONNULL_BEGIN

CORETELEPHONY_EXTERN NSString * const CTCallStateDialing __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
CORETELEPHONY_EXTERN NSString * const CTCallStateIncoming __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
CORETELEPHONY_EXTERN NSString * const CTCallStateConnected __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
CORETELEPHONY_EXTERN NSString * const CTCallStateDisconnected __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);

CORETELEPHONY_CLASS_AVAILABLE(4_0)

这个block,在来电时,会被调用多次,第一次触发CTCallStateIncoming,当无人接听或者挂断,会再次调用block,触发CTCallStateDisconnected,有人接听触发CTCallStateConnected,所以,这个block至少会触发两次,最多触发四次。

注:CTCallCente必须用全局


//
// ViewController.m
// cll
//
// Created by ChangQing Yang on 16/6/1.
// Copyright © 2016年 ChangQing Yang. All rights reserved.
//

import "ViewController.h"

//导入系统的头文件

import

import

@interface ViewController ()

@property(nonatomic,strong)CTCallCenter *callCenter; //必须在这里声明,要不不会回调block

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    _callCenter = [[CTCallCenter alloc] init];

    _callCenter.callEventHandler=^(CTCall* call){

      if([call.callState isEqualToString:CTCallStateDisconnected])
          
      {
          NSLog(@"Call has been disconnected");
      }
      
      else if([call.callState isEqualToString:CTCallStateConnected])
          
      {
          NSLog(@"Callhasjustbeen connected");
      }
      
      else if([call.callState isEqualToString:CTCallStateIncoming])
          
      {
          NSLog(@"Call is incoming");
      }
      
      else if([call.callState isEqualToString:CTCallStateDialing])
          
      {
          NSLog(@"Call is Dialing");
      }
      
      else
      {
          NSLog(@"Nothing is done");
      }
    

    };

}

小结:
根据需求来做相应block里面回调的处理,注意考虑在block里面操作的时候要定义__bolck。
如发现错误或不足,欢迎指正,如有需要,联系qq 1392550437.
谢谢。

你可能感兴趣的:(iOS详解监听系统来电(2016/06))