Have you ever encountered a situation where you want to build some record or music app ? But you need to somehow react on a phone call which in many cases break recording or playing in more advanced app (mostly when it comes to CoreAudio) or switch UI in case of call? CoreTelephony is a great library which will help in most of the situations.
To detect phone call on your iPhone or iPad app (this may be the case now when we use continuity) simply use this piece of code:
#import <CoreTelephony/CTCall.h>#import <CoreTelephony/CTCallCenter.h>#import <CoreTelephony/CTCarrier.h>#import <CoreTelephony/CTTelephonyNetworkInfo.h>@interfaceMMCallNotificationManager()@property(nonatomic,strong)CTCallCenter*callCenter;@property(nonatomic)BOOLcallWasStarted;@end@implementationMMCallNotificationManager-(instancetype)init{self=[superinit];if(self){self.callCenter=[[CTCallCenteralloc]init];self.callWasStarted=NO;__weak__typeof__(self)weakSelf=self;[self.callCentersetCallEventHandler:^(CTCall*call){if([[callcallState]isEqual:CTCallStateIncoming]||[[callcallState]isEqual:CTCallStateDialing]){if(weakSelf.callWasStarted==NO){weakSelf.callWasStarted=YES;NSLog(@"Call was started.");}}elseif([[callcallState]isEqual:CTCallStateDisconnected]){if(weakSelf.callWasStarted==YES){weakSelf.callWasStarted=NO;NSLog(@"Call was ended.");}}}];}returnself;}@end
Another example of usage may be detecting if we have simcard installed on our device. For example our application allows user to call someone within the app, but we want to inform if the card is removed. Of course we can use some iOS urls for that, such as: telprompt: but they likely breaks UI e.g: in iOS8 it blinks twice which is not a nice effect…
So to detect if simcard is in place, use this code:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>#import <CoreTelephony/CTCarrier.h>-(IBAction)handleCallButtonPress:(id)sender{CTTelephonyNetworkInfo*networkInfo=[[CTTelephonyNetworkInfoalloc]init];NSString*code=[networkInfo.subscriberCellularProvidermobileCountryCode];//this is nil if you take out sim card.if(code==nil){UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:NSLocalizedString(@"aler.error",nil)message:NSLocalizedString(@"alert.message.no_sim_card",nil)delegate:nilcancelButtonTitle:NSLocalizedString(@"alert.button_dimiss",nil)otherButtonTitles:nil];[alertViewshow];return;}//make regular phone prompt (with call confirmation)NSURL*phoneUrl=[NSURLURLWithString:[NSStringstringWithFormat:@"telprompt://%@",phoneNumberString]];if([[UIApplicationsharedApplication]canOpenURL:phoneUrl]){[[UIApplicationsharedApplication]openURL:phoneUrl];}}
I hope this will help you reduce potential edge cases in your future apps.