Programmatically Handling iPhone interruptions

本文介绍如何通过编程方式处理iPhone上的应用中断问题,如来电、短信及USSD消息等。通过监听核心电信事件并适当关闭关键进程,确保用户体验不受影响。

Programmatically Handling iPhone interruptions


When it comes to the iPhone, phone function comes first. Incoming calls, SMS, and USSD (unstructured supplementary service data, including account balance notifications for prepaid plans) messages will supersede your application. You can program around this by watching for core telephony events and shutting down key processes as needed. The iPhone's core telephony system sends out distributed notifications to warn about these status changes.

To add core telephony notification to your program, include the following code and compile linking to the Core Telephony framework. This code adds your program as a registered observer.

id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);

To monitor the incoming notifications, you need to build the callback routine you supplied as an argument to the add observer call:

static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { ... }

The notification is sent as the name in this callback and supporting data is sent as a dictionary of information (userInfo). Use the toll-free bridging between CFStringRef and NSString to apply a simple isEqualToString: method and check which notification was sent.

The keys you will want to watch for are as follows:

kCTCallStatusChangeNotification A call was received or ended.

kCTSMSMessageReceivedNotification An SMS message was received.

kCTUSSDSessionBeginNotification, kCTUSSDSessionStringNotification, and kCTUSSDSessionEndNotification USSD message sessions may use all three of these notifications.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值