IOS receives a call/call system notification to reach the call/call location effect (1)

Source: Internet
Author: User

IOS receives a call/call system notification to reach the call/call location effect (1)

The implementation principle here is to get the incoming call system notification, and then according to the local call phone number, the corresponding phone contact.

Case 1: the incoming call number is not in the address book contact. In this case, add a temporary contact to the address book (that is, create a contact and save the corresponding RecordID)

Scenario 2: contacts in the address book

Then, change the phone label corresponding to the current call to a local character.

 

 

Okay. Let's see how to get the system call notification first.

Required class

 

// Create a telephone object

Code 1:

 

# Import

@ Import CoreTelephony;

 

// Private API

Typedef NS_ENUM (short, CTCallStatus ){

KCTCallStatusConnected = 1, // Enabled

KCTCallStatusCallOut = 3, // dial out

KCTCallStatusCallIn = 4, // call in

KCTCallStatusHungUp = 5 // hung up

};

 

@ Interface WCCall: NSObject

 

@ Property (nonatomic, assign) CTCallStatus callStatus;

@ Property (nonatomic, copy) NSString * phoneNumber;

@ Property (nonatomic, retain) CTCall * internalCall;

 

@ End




Code 2:

# Import WCCall. h

 

@ Implementation WCCall

 

@ End



 

// Listen for phone notifications

Code 3:

 

# Import

# Import WCCall. h

 

@ Interface WCCallCenter: NSObject

 

// Listen for incoming call events

@ Property (nonatomic, strong) void (^ callEventHandler) (WCCall * call );

 

// Hang up the phone

-(Void) disconnectCall :( WCCall *) call;

 

@ End


 

Code 4:

 

# 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]

 

// Here we need to extend the string NSString

// # Import Reference this framework


 

/**

 

-(NSString *) wcRot13

{

Const char * source = [selfcStringUsingEncoding: 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] = '';

 

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: rot13options: 0]; // 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] callback value];

 

If ([[calldescription] rangeOfString: @ status = 196608]. location = NSNotFound ){

// After that, you will perform operations on the region information.

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

 

 

// Handle local call operations
Code 5:

# Import

@ Interface WCCallInspector: NSObject

 

+ (Instancetype) sharedInspector;

-(Void) startInspect; // start O (∩ _ likelihood) O ~~

 

@ End


Code 6:

# Import WCCallInspector. h

# Import WCCallCenter. h

# Import

 

@ 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 calls out, calls in, connects, and hangs up

-(Void) handleCallEvent :( WCCall *) call {

// How can I perform this operation based on my own situation ......

// Print the call attributes to see the result.

// KCTCallStatusConnected = 1, // Enabled

// KCTCallStatusCallOut = 3, // dial out

// KCTCallStatusCallIn = 4, // call in

// KCTCallStatusHungUp = 5 // hang up

}

// StartInspect // This method needs to be registered at the startup of the program

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions

{

 

/**

* Your Code

**/

 

[[WCCallInspectorsharedInspector] startInspect];

Return YES;

}


// Reference this class library # import

 

When the program is started, you can call/power-off/power-on/stop-up. The article is over now. Please wait for a contact to add a location and modify the contact operation blog .....

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.