IOS direct dialing number
Direct code:
Class
// ACETelPrompt. h
# Import
@ Interface ACETelPrompt: NSObject
Typedef void (^ ACETelCallBlock) (NSTimeInterval duration );
Typedef void (^ ACETelCancelBlock) (void );
+ (BOOL) callPhoneNumber :( NSString *) phoneNumber
Call :( ACETelCallBlock) callBlock
Cancel :( ACETelCancelBlock) cancelBlock;
@ End
// ACETelPrompt. m
# Import "ACETelPrompt. h"
# Import "AppDelegate. h"
// The time required to launch the phone app and come back (will be substracted to the duration)
# Define kCallSetupTime 3.0
@ Interface ACETelPrompt ()
@ Property (nonatomic, strong) NSDate * callStartTime;
@ Property (nonatomic, copy) ACETelCallBlock callBlock;
@ Property (nonatomic, copy) ACETelCancelBlock cancelBlock;
@ End
@ Implementation ACETelPrompt
+ (Instancetype) sharedInstance
{
Static ACETelPrompt * _ instance = nil;
Static dispatch_once_t oncePredicate;
Dispatch_once (& oncePredicate, ^ {
_ Instance = [[self alloc] init];
});
Return _ instance;
}
+ (BOOL) callPhoneNumber :( NSString *) phoneNumber
Call :( ACETelCallBlock) callBlock
Cancel :( ACETelCancelBlock) cancelBlock
{
If ([self validPhone: phoneNumber]) {
ACETelPrompt * telPrompt = [ACETelPrompt sharedInstance];
// Observe the app notifications
[TelPrompt setNotifications];
// Set the blocks
TelPrompt. callBlock = callBlock;
TelPrompt. cancelBlock = cancelBlock;
// Clean the phone number
NSString * simplePhoneNumber =
[[PhoneNumber componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet] componentsJoinedByString: @ ""];
// Call the phone number using the telprompt scheme
NSString * stringURL = [@ "telprompt: //" stringByAppendingString: simplePhoneNumber];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: stringURL];
Return YES;
}
Return NO;
}
+ (BOOL) validPhone :( NSString *) phoneString
{
NSTextCheckingType = [[NSTextCheckingResult phoneNumberCheckingResultWithRange: NSMakeRange (0, phoneString. length)
PhoneNumber: phoneString] resultType];
Return type = NSTextCheckingTypePhoneNumber;
}
# Pragma mark-Events
-(Void) setconfigurications
{
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (applicationDidEnterBackground :)
Name: UIApplicationDidEnterBackgroundNotification
Object: nil];
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (applicationDidBecomeActive :)
Name: UIApplicationDidBecomeActiveNotification
Object: nil];
}
# Pragma mark-Events
-(Void) applicationDidEnterBackground :( NSNotification *) notification
{
// Save the time of the call
Self. callStartTime = [NSDate date];
}
-(Void) applicationDidBecomeActive :( NSNotification *) notification
{
// Now it's time to remove the observers
[[Nsicationcenter center defacenter center] removeObserver: self];
If (self. callStartTime! = Nil ){
// I'm coming back after a call
If (self. callBlock! = Nil ){
Self. callBlock (-([self. callStartTime timeIntervalSinceNow])-kCallSetupTime );
}
// Reset the start timer
Self. callStartTime = nil;
} Else if (self. cancelBlock! = Nil ){
// User didn't start the call
Self. cancelBlock ();
}
}
Call
-(IBAction) connectUs :( id) sender {
[ACETelPrompt callPhoneNumber: @ "020-38880808080"
Call: ^ (NSTimeInterval duration ){
NSLog (@ "User made a call of %. 1f seconds", duration );
} Cancel: ^ {
NSLog (@ "User canceled the call ");
}];
}
@ End