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
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.
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];