iOS 直接撥打號碼

來源:互聯網
上載者:User

iOS 直接撥打號碼

直接代碼:

 

// 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 type = [[NSTextCheckingResult phoneNumberCheckingResultWithRange:NSMakeRange(0, phoneString.length)

phoneNumber:phoneString] resultType];

return type == NSTextCheckingTypePhoneNumber;

}

 

 

#pragma mark - Notifications

 

- (void)setNotifications

{

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(applicationDidEnterBackground:)

name:UIApplicationDidEnterBackgroundNotification

object:nil];

 

[[NSNotificationCenter defaultCenter] 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

[[NSNotificationCenter defaultCenter] 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();

}

}

 

調用

 

- (IBAction)connectUs:(id)sender {

[ACETelPrompt callPhoneNumber:@"020-38880808080"

call:^(NSTimeInterval duration) {

NSLog(@"User made a call of %.1f seconds", duration);

 

} cancel:^{

NSLog(@"User cancelled the call");

}];

}



 

@end



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.