转自:http://www.tuicool.com/articles/niQvMr
这里的实现原理是获取到来电系统通知,然后根据本地通话的电话号码,把对应电话的联系人.
情况一 来电号码未在通讯录联系人中, 这时候要先在通讯录添加一个临时的联系人(就是新建一个联系人,并保存对应的 RecordID )
情况二 已在通讯录的联系人
然后 把对应本次通话的电话标签修改成归属地字符
原文地址
http://blog.csdn.net/leewolf130/article/details/38921921
好了 ,先看看怎么获取系统电话通知
需要的类
//创建电话对象
代码一:
#import
@import CoreTelephony;
// private API
typedef NS_ENUM ( short , CTCallStatus) {
kCTCallStatusConnected = 1 , // 已接通
kCTCallStatusCallOut = 3 , // 拨出去
kCTCallStatusCallIn = 4 , // 打进来
kCTCallStatusHungUp = 5 // 挂断
};
@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
#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 引用 这个框架
/**
- ( NSString *)wcRot13
{
const char *source = [ self cStringUsingEncoding : NSASCIIStringEncoding ];
char *dest = ( char *) malloc (( self . length + 1 ) * sizeof ( char ));
if (!dest) {
return nil ;
}
NSUInteger i = 0 ;
for ( ; i < self . length ; i++) {
char c = source[i];
if (c >= 'A' && c <= 'Z' ) {
c = (c - 'A' + 13 ) % 26 + 'A' ;
}
else if (c >= 'a' && c <= 'z' ) {
c = (c - 'a' + 13 ) % 26 + 'a' ;
}
dest[i] = c;
}
dest[i] = '