iOS開發通話狀態

通過CTCallcallEventHandler回調block來獲得通話狀態的改變.

代碼如下:

#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卡問題:

如果應用會讓用戶去撥打電話,但是當設備沒有sim卡的時候, 使用iOS 的URLstelprompt:會導致在iOS8下界面會閃兩下.

可以用如下代碼檢查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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容