IOS私有API的了解
IOS的API分为四种:
Published API(公开的API)(或者Documented API)
还有两类:私有API:Private API和未公开API:UnPublished API(或者Undocumented API)。
私有API是指放在PrivateFrameworks框架中的API,未公开的API是指虽然放在Frameworks框架中,但是却没有在苹果的官方文档中有使用说明、代码介绍等记录的API。
按苹果的说法,未公开的API是还不够成熟,可能还会变动的API,等完全成型了后会变成公开的API,但是目前不对其提供承诺,就是系统版本升级后可能会失效。而私有API是苹果明确不能使用的API。虽然两者有所区别,但是在具体使用方法上是类似的。
私有API会面临审核通不过然后不能上线,但是也有使用私有API通过审核的比如谷歌的“Google Voice”,所以具体审核的详情不得而知。
示例一:
使用私有API——CoreTelephony获取本机号码
步骤如下:
1)导入CoreTelephony这个private framework
2)在xxx.m中加入红字部分:
#import "xxx.h"
extern NSString *CTSettingCopyMyPhoneNumber();
@implementation
+(NSString *)myNumber{
return CTSettingCopyMyPhoneNumber();
}
@end
3)在其它地方调用NSLog(@"myNumber=%@",[xxx myNumber]);即是本机号码
另外机子的唯一标识码等一些信息 可以用[UIDevice currentDevice] 的uniqueIdentifier,name等属性来获取
其他资料:
call blocking on iPhone4
//using private API
//CoreTelephony framework is needed
extern NSString* const kCTSMSMessageReceivedNotification;
extern NSString* const kCTSMSMessageReplaceReceivedNotification;
extern NSString* const kCTSIMSupportSIMStatusNotInserted;
extern NSString* const kCTSIMSupportSIMStatusReady;
typedef struct __CTCall CTCall;
extern NSString *CTCallCopyAddress(void*, CTCall *);
void* CTSMSMessageSend(id server,id msg);
typedef struct __CTSMSMessage CTSMSMessage;
NSString *CTSMSMessageCopyAddress(void *, CTSMSMessage *);
NSString *CTSMSMessageCopyText(void *, CTSMSMessage *);
int CTSMSMessageGetRecordIdentifier(void * msg);
NSString * CTSIMSupportGetSIMStatus();
NSString * CTSIMSupportCopyMobileSubscriberIdentity();
id CTSMSMessageCreate(void* unknow,NSString* number,NSString* text);
void * CTSMSMessageCreateReply(void* unknow,void * forwardTo,NSString* text);
id CTTelephonyCenterGetDefault(void);
void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
void CTTelephonyCenterRemoveObserver(id,id,NSString*,void*);
int CTSMSMessageGetUnreadCount(void);
static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSString *notifyname=(NSString *)name;
if ([notifyname isEqualToString:@"kCTCallStatusChangeNotification"])//phone call
{
NSDictionary *info = (NSDictionary*)userInfo;
NSString *state=[[info objectForKey:@"kCTCallStatus"] stringValue];
if ([state isEqualToString:@"5"])//disconnect
NSLog(@”unanswered:%@”,state);
}
else if ([notifyname isEqualToString:@"kCTCallIdentificationChangeNotification"])
{
// CTCallCenter *center = [[CTCallCenter alloc] init];
// center.callEventHandler = ^(CTCall *call) {
// NSLog(@”call:%@”, [call description]);
// };
NSDictionary *info = (NSDictionary *)userInfo;
CTCall *call = (CTCall *)[info objectForKey:@"kCTCall"];
NSString *caller = CTCallCopyAddress(NULL, call);
NSLog(@”电话号码:%@”,caller);
//CTCallDisconnect(call);
}
else if ([notifyname isEqualToString:@"kCTRegistrationDataStatusChangedNotification"])
{
}
else if ([notifyname isEqualToString:@"kCTSMSMessageReceivedNotification"])
{//api expired
if ([[(NSDictionary *)userInfo allKeys]
containsObject:@”kCTSMSMessage”]) // SMS Message
{
CTSMSMessage *message = (CTSMSMessage *)
[(NSDictionary *)userInfo objectForKey:@"kCTSMSMessage"];
NSString *address = CTSMSMessageCopyAddress(NULL, message);
NSString *text = CTSMSMessageCopyText(NULL, message);
//NSArray *lines = [text componentsSeparatedByString:@"\n"];
//printf(” %s %d\n”, [address cString], [lines count]);
//printf(” %s\n”, [text cString]);
fflush(stdout);
}
}
else if ([notifyname isEqualToString:@"kCTMessageReceivedNotification"])//received SMS
{
NSDictionary *info = (NSDictionary *)userInfo;
CFNumberRef msgID = (CFNumberRef)[info objectForKey:@"kCTMessageIdKey"];
int result;
CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);
}
else if ([notifyname isEqualToString:@"kCTIndicatorsSignalStrengthNotification"])//signal
{
}
else if ([notifyname isEqualToString:@"kCTRegistrationStatusChangedNotification"])//network registration status
{
}
else if ([notifyname isEqualToString:@"kCTRegistrationDataStatusChangedNotification"])
{
}
else if ([notifyname isEqualToString:@"kCTRegistrationCellChangedNotification"])
{
}
else if ([notifyname isEqualToString:@"kCTIndicatorRadioTransmitNotification"])
{
}
//int unread=CTSMSMessageGetUnreadCount();
//if (unread>0)
//NSLog(@”unread SMS:%d”,unread);
NSLog(@”name:%@-detail:%@”,notifyname,userInfo);
}
static void signalHandler(int sigraised)
{
printf(“\nInterrupted.\n”);
exit(0);
}
//—————–
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);
// Handle Interrupts
sig_t oldHandler = signal(SIGINT, signalHandler);
if (oldHandler == SIG_ERR)
{
printf(“Could not establish new signal handler”);
exit(1);
}
// Run loop lets me catch notifications
printf(“Starting run loop and watching for notification.\n”);
//CFRunLoopRun();