这里的原理是获得到呼叫系统通知,然后根据本地呼叫电话号码,相应的电话联系.
一世。 来电显示是不是在地址簿中的联系人, 这是当第一个加入一个临时的联系人在您的电话簿(它是创建一个新的接触,并保存相应的RecordID)
情况二 已在通讯录的联系人
然后 把相应本次通话的电话标签改动成归属地字符
原文地址 http://blog.csdn.net/leewolf130/article/details/38921921
好了 ,先看看怎么获取系统电话通知
须要的类
//创建电话对象
代码一:
#import <Foundation/Foundation.h>
@import CoreTelephony;
// private API
typedef NS_ENUM(short, CTCallStatus) {
kCTCallStatusConnected = ,
//已接通
kCTCallStatusCallOut = ,
//拨出去
kCTCallStatusCallIn = ,
//打进来
kCTCallStatusHungUp =
//挂断
};
@interface WCCall :
NSObject
@property (nonatomic,assign)
CTCallStatus callStatus;
@property (nonatomic,copy)
NSString *phoneNumber;
@property (nonatomic,retain)
CTCall *internalCall;
@end
#import "WCCall.h"
@implementation WCCall
@end
//监听电话通知
代码三:
#import <Foundation/Foundation.h>
#import "WCCall.h"
@interface WCCallCenter :
NSObject
// 监听来电事件
@property (nonatomic,strong)
void (^callEventHandler)(WCCall *call);
// 挂断电话
- (void)disconnectCall:(WCCall *)call;
@end
代码四:
#import "WCCallCenter.h"
#import "WCUtil.h"
// encrypted string's
#define ENCSTR_kCTCallStatusChangeNotification [@"n0AHD2SfoSA0LKE1p0AbLJ5aMH5iqTyznJAuqTyiot==" wcDecryptString]
#define ENCSTR_kCTCall [@"n0AHD2SfoN==" wcDecryptString]
#define ENCSTR_kCTCallStatus [@"n0AHD2SfoSA0LKE1pj==" wcDecryptString]
#define ENCSTR_CTTelephonyCenterGetDefault [@"D1EHMJkypTuioayQMJ50MKWUMKERMJMuqJk0" wcDecryptString]
#define ENCSTR_CTTelephonyCenterAddObserver [@"D1EHMJkypTuioayQMJ50MKWOMTECLaAypaMypt==" wcDecryptString]
#define ENCSTR_CTTelephonyCenterRemoveObserver [@"D1EHMJkypTuioayQMJ50MKWFMJ1iqzICLaAypaMypt==" wcDecryptString]
#define ENCSTR_CTCallCopyAddress [@"D1EQLJkfD29jrHSxMUWyp3Z=" wcDecryptString]
#define ENCSTR_CTCallDisconnect [@"D1EQLJkfETymL29hozIwqN==" wcDecryptString]
//这里须要对字符串 NSString 进行拓展方法
//#import <dlfcn.h> 引用 这个框架
/**
- (NSString *)wcRot13
{
const char *source = [selfcStringUsingEncoding:NSASCIIStringEncoding];
)
* sizeof(char));
if (!dest) {
return nil;
}
;
for ( ; i < self.length; i++) {
char c = source[i];
if (c >= 'A' && c <='Z') {
c = (c - ) % +
'A';
}
else if (c >='a' && c <=
'z') {
c = (c - ) % +
'a';
}
dest[i] = c;
}
dest[i] = '\0';
NSString *result = [[NSStringalloc]
initWithCString:destencoding:NSASCIIStringEncoding];
free(dest);
return result;
}
- (NSString *)wcDecryptString
{
NSString *rot13 = [selfwcRot13];
NSData *data;
if ([NSDatainstancesRespondToSelector:@selector(initWithBase64EncodedString:options:)]) {
data = [[NSDataalloc]
initWithBase64EncodedString]; // iOS 7+
} else {
data = [[NSData
alloc] initWithBase64Encoding:rot13]; // pre iOS7
}
return [[NSStringalloc]
initWithData:dataencoding:NSUTF8StringEncoding];
}
**/
// private API
//extern NSString *CTCallCopyAddress(void*, CTCall *);
typedef NSString *(*PF_CTCallCopyAddress)(void*,CTCall *);
//extern void CTCallDisconnect(CTCall *);
typedef void (*PF_CTCallDisconnect)(CTCall *);
//extern CFNotificationCenterRef CTTelephonyCenterGetDefault();
typedef CFNotificationCenterRef (*PF_CTTelephonyCenterGetDefault)();
typedef void (*PF_CTTelephonyCenterAddObserver)(CFNotificationCenterRef center,
constvoid *observer,
CFNotificationCallback callBack,
CFStringRef name,
constvoid *object,
CFNotificationSuspensionBehavior suspensionBehavior);
typedef void (*PF_CTTelephonyCenterRemoveObserver)(CFNotificationCenterRef center,
constvoid *observer,
CFStringRef name,
constvoid *object);
@interface
WCCallCenter ()
- (void)handleCall:(CTCall *)call withStatus:(CTCallStatus)status;
@end
@implementation WCCallCenter
- (id)init
{
self = [superinit];
if (self) {
[selfregisterCallHandler];
}
return
self;
}
- (void)dealloc
{
[selfderegisterCallHandler];
}
- (void)registerCallHandler
{
staticPF_CTTelephonyCenterAddObserver AddObserver;
staticPF_CTTelephonyCenterGetDefault GetCenter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
AddObserver = [WCDLloadSymbol:ENCSTR_CTTelephonyCenterAddObserver];
GetCenter = [WCDLloadSymbol:ENCSTR_CTTelephonyCenterGetDefault];
});
AddObserver(GetCenter(),
(__bridge
void *)self,
&callHandler,
(__bridgeCFStringRef)(ENCSTR_kCTCallStatusChangeNotification),
NULL,
CFNotificationSuspensionBehaviorHold);
}
- (void)deregisterCallHandler
{
staticPF_CTTelephonyCenterRemoveObserver RemoveObserver;
staticPF_CTTelephonyCenterGetDefault GetCenter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RemoveObserver = [WCDLloadSymbol:ENCSTR_CTTelephonyCenterRemoveObserver];
GetCenter = [WCDLloadSymbol:ENCSTR_CTTelephonyCenterGetDefault];
});
RemoveObserver(GetCenter(),
(__bridge
void *)self,
(__bridgeCFStringRef)(ENCSTR_kCTCallStatusChangeNotification),
NULL);
}
- (void)handleCall:(CTCall *)call withStatus:(CTCallStatus)status
{
staticPF_CTCallCopyAddress CopyAddress;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CopyAddress = [WCDL
loadSymbol:ENCSTR_CTCallCopyAddress];
});
if (!self.callEventHandler || !call) {
return;
}
WCCall *wcCall = [[WCCallalloc]
init];
wcCall.phoneNumber = CopyAddress(NULL, call);
wcCall.phoneNumber = wcCall.phoneNumber;
wcCall.callStatus = status;
wcCall.internalCall = call;
self.callEventHandler(wcCall);
}
static void callHandler(CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const
void *object,
CFDictionaryRef userInfo)
{
if (!observer) {
return;
}
NSDictionary *info = (__bridgeNSDictionary *)(userInfo);
CTCall *call = (CTCall *)info[ENCSTR_kCTCall];
CTCallStatus status = (CTCallStatus)[info[ENCSTR_kCTCallStatus]shortValue];
if ([[calldescription]
rangeOfString:@"status = 196608"].location==NSNotFound)
{
//这里之后就是你对归属地信息的操作了
WCCallCenter *wcCenter = (__bridgeWCCallCenter*)observer;
[wcCenter handleCall:call
withStatus:status];
}
}
- (void)disconnectCall:(WCCall *)call
{
staticPF_CTCallDisconnect Disconnect;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Disconnect = [WCDL
loadSymbol:ENCSTR_CTCallDisconnect];
});
CTCall *ctCall = call.internalCall;
if (!ctCall) {
return;
}
Disconnect(ctCall);
}
@end
#import <Foundation/Foundation.h>
@interface WCCallInspector :
NSObject
+ (instancetype)sharedInspector;
- (void)startInspect;//启动 O(∩_∩)O~~
@end
#import "WCCallInspector.h"
#import "WCCallCenter.h"
#import <AudioToolbox/AudioToolbox.h>
@interface
WCCallInspector ()
@property (nonatomic,strong)
WCCallCenter *callCenter;
@end
@implementation WCCallInspector
+ (instancetype)sharedInspector
{
static WCCallInspector *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[WCCallInspector
alloc] init];
});
return instance;
}
- (id)init
{
self = [superinit];
if (self) {
}
return
self;
}
#pragma mark - Call Inspection
- (void)startInspect
{
if (self.callCenter) {
return;
}
self.callCenter = [[WCCallCenteralloc]
init];
__weak WCCallInspector *weakSelf =self;
self.callCenter.callEventHandler = ^(WCCall
*call) {
[weakSelf handleCallEvent:call];
};
}
#pragma mark 呼出,呼入,接通,挂断
- (void)handleCallEvent:(WCCall *)call{
//这里 想怎么操作 依据自己情况而定啊......
//能够打印call的属性看看结果
// kCTCallStatusConnected = 1, //已接通
// kCTCallStatusCallOut = 3, //拨出去
// kCTCallStatusCallIn = 4, //打进来
// kCTCallStatusHungUp = 5 //挂断
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/**
* Your Code
**/
[[WCCallInspectorsharedInspector]
startInspect];
return
YES;
}
//引用这个类库
#import <AudioToolbox/AudioToolbox.h>
转载请标明 原文地址 http://blog.csdn.net/leewolf130/article/details/38921921
当程序启动 来电/传出/接通/挂断 能够获得的, 这样就完成了文章, 敬请关注 添加联系人归属, 联系方式变更的操作博客.....