iOS 直接拨打号码

直接代码:

// ACETelPrompt.h



#import <Foundation/Foundation.h>


@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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值