iOS開(kāi)發(fā)通話狀態(tài)

通過(guò)CTCallcallEventHandler回調(diào)block來(lái)獲得通話狀態(tài)的改變.

代碼如下:

#import <CoreTelephony/CTCall.h>
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@interface MMCallNotificationManager()
@property (nonatomic, strong) CTCallCenter *callCenter;
@property (nonatomic) BOOL callWasStarted;
@end

@implementation MMCallNotificationManager

- (instancetype)init
{
    self = [super init];

    if (self) {

        self.callCenter = [[CTCallCenter alloc] init];
        self.callWasStarted = NO;

        __weak __typeof__(self) weakSelf = self;

        [self.callCenter setCallEventHandler:^(CTCall *call) {

            if ([[call callState] isEqual:CTCallStateIncoming] ||
                [[call callState] isEqual:CTCallStateDialing]) {

                if (weakSelf.callWasStarted == NO) {

                    weakSelf.callWasStarted = YES;

                    NSLog(@"Call was started.");
                }

            } else if ([[call callState] isEqual:CTCallStateDisconnected]) {

                if (weakSelf.callWasStarted == YES)
                {
                    weakSelf.callWasStarted = NO;

                    NSLog(@"Call was ended.");
                }
            }
        }];
    }

    return self;
}

@end

sim卡問(wèn)題:

如果應(yīng)用會(huì)讓用戶去撥打電話,但是當(dāng)設(shè)備沒(méi)有sim卡的時(shí)候, 使用iOS 的URLstelprompt:會(huì)導(dǎo)致在iOS8下界面會(huì)閃兩下.

可以用如下代碼檢查sim卡是否插上了:

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

- (IBAction)handleCallButtonPress:(id)sender
{
    CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];

    NSString *code = [networkInfo.subscriberCellularProvider mobileCountryCode];

    //this is nil if you take out sim card.
    if (code == nil) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"aler.error",nil)
                                                            message:NSLocalizedString(@"alert.message.no_sim_card",nil)
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"alert.button_dimiss", nil)
                                                  otherButtonTitles:nil];

        [alertView show];

        return;
    }

    //make regular phone prompt (with call confirmation)
    NSURL *phoneUrl = [NSURL URLWithString:[NSString  stringWithFormat:@"telprompt://%@",phoneNumberString]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {

        [[UIApplication sharedApplication] openURL:phoneUrl];
    }
}

參考: Working With Core Telephony Framework

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容