How to get a call event using CTCallCenter:setCallEventHandler: that occurred while the app was suspended?

后端 未结 2 1533
别跟我提以往
别跟我提以往 2020-11-29 02:46

The documentation for CTCallCenter:setCallEventHandler: states that:

However, call events can also take place while your application is suspended.

相关标签:
2条回答
  • 2020-11-29 03:09

    In my case, I was working on an enterprise app which doesn't need to be approved by Apple's app market - so if you develop an enterprise app this solution is for you.

    Also, the chosen answer didn't work while the app is the background.

    The solution is simple, basically you just need to add 2 capabilities (VOIP & Background fetch) in the Capabilities tab:

    • Your project target -> Capabilities -> Background Modes -> mark Voice over IP & Background fetch

    Now, your app is registered to the iOS framework calls "delegate" so the OP code snip solution:

    [self.callCenter setCallEventHandler:^(CTCall *call)
         {
             NSLog(@"Event handler called");
             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(@"Incomming");
             }
         }];  
    

    Would defiantly work and you will get notifications even if your app is in the background.

    0 讨论(0)
  • 2020-11-29 03:15

    I've found a solution but I have no idea why it's working. Only thing I can think of is a bug in GCD and/or CoreTelephony.

    Basically, I allocate two instances of CTCallCenter like this

    void (^block)(CTCall*) = ^(CTCall* call) { NSLog(@"%@", call.callState); };
    
    -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    {
        callCenter1 = [[CTCallCenter alloc] init];
        callCenter1.callEventHandler = block;
    
        callCenter2 = [[CTCallCenter alloc] init];
        callCenter2.callEventHandler = block;
    
        return YES;
    }
    

    Similar Code in Swift:

    func block (call:CTCall!) {
            println(call.callState)
        }
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            //Declare callcenter in the class like 'var callcenter = CTCallCenter()'
            callcenter.callEventHandler = block
    
            return true
        }
    

    To test this I made a call, answered it and then hanged up it while app was in background. When I launched it I received 3 call events: incoming, connected, disconnected.

    0 讨论(0)
提交回复
热议问题