I want to handle the state of call in ios

痴心易碎 提交于 2019-12-01 08:42:20

问题


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 *phoneNumber = [@"telprompt://" stringByAppendingString:@"9723539389"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

CTCallCenter *callCenter = [[CTCallCenter alloc] init];
callCenter.callEventHandler=^(CTCall* call)
{
    if(CTCallStateDialing)
    {
        NSLog(@"Dialing");
    }
    if(CTCallStateConnected)
    {
        NSLog(@"Connected");
    }
    if(CTCallStateDisconnected)
    {
        NSLog(@"Disconnected");
    }
};

but the problem is that the CTCallCenter block never called... I am currently worked in iOS 7


回答1:


You should check the CTCall's callState property in order to catch it

Use a cellular call’s CTCall object to obtain an identifier for the call and to determine the call’s state.

 extern NSString const *CTCallStateDialing;
 extern NSString const *CTCallStateIncoming;
 extern NSString const *CTCallStateConnected;
 extern NSString const *CTCallStateDisconnected;

are string constants. You for loop doesn't make sense.

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:@"9723539389"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

self.callCenter = [[CTCallCenter alloc] init];

[callCenter setCallEventHandler:^(CTCall* call)
{
    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(@"Incoming");
    }

}];

NOTE:

  @property(nonatomic, strong) CTCallCenter *callCenter;

should be declared in Appdelegate and retain it. Otherwise it will be considered as local variable & deallocated as soon as come out of loop

UPDATES:

TO ANSWER "callCenter is declared in appdelegate, how can i use it with self? and other thing when [] added to that block then it shows error on equal to "expected ']' ""

replace self.callCenter with these lines

   YourApplicationDelegateClass *appDel =(YourApplicationDelegateClass*)[UIApplication sharedApplication].delegate;

   appDel.callCenter =  [[CTCallCenter alloc] init];



回答2:


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 <UIKit/UIKit.h>
#import <CoreTelephony/CTCall.h>
#import <CoreTelephony/CTCallCenter.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@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.



来源:https://stackoverflow.com/questions/24323282/i-want-to-handle-the-state-of-call-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!