IOS direct dialing number

Source: Internet
Author: User

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



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.