I want to handle the state of call in ios

前端 未结 2 1068
误落风尘
误落风尘 2021-01-15 04:15

I want to get the state of phone call either it in dialed, connected or disconnected...

I tried my self but i cant get the state.

NSString *phoneNumb         


        
2条回答
  •  梦毁少年i
    2021-01-15 04:55

    Here is how I have done it. As suggested above, I have used it in AppDelegate, but the entire block is also in appDelegate and I don't monitor the state, but check if the call was nil or not.

    In AppDelegate.h file

    #import 
    #import 
    #import 
    
    @interface AppDelegate : UIResponder 
    
    @property (nonatomic, strong) CTCallCenter *center;
    @property (nonatomic, assign) BOOL callWasMade;
    
    @end
    

    In AppDelegate.m file

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        self.center = [[CTCallCenter alloc]init];
        typeof(self) __weak weakSelf = self;
        self.center.callEventHandler = ^(CTCall *call) {
            if(call!=nil) {
                NSLog(@"Call details is not nil, so user must have pressed call button somewhere in the alert view");
                weakSelf.callWasMade = YES;
            }
        };
    }
    

    Using the callWasMade boolean property, you can carry on performing your tasks. This property can be referenced in any ViewController by getting a handle to the AppDelegate. I hope you find this snippet useful.

提交回复
热议问题