iOS私有API的了解

iOS的API分为四种:

PublishedAPI(公开的API)(或者Documented API

还有两类:私有APIPrivateAPI和未公开APIUnPublished API(或者UndocumentedAPI)

私有API是指放在PrivateFrameworks框架中的API,未公开的API是指虽然放在Frameworks框架中,但是却没有在苹果的官方文档中有使用说明、代码介绍等记录的API

按苹果的说法,未公开的API是还不够成熟,可能还会变动的API,等完全成型了后会变成公开的API,但是目前不对其提供承诺,就是系统版本升级后可能会失效。而私有API是苹果明确不能使用的API。虽然两者有所区别,但是在具体使用方法上是类似的。

私有API会面临审核通不过然后不能上线,但是也有使用私有API通过审核的比如谷歌的“GoogleVoice”,所以具体审核的详情不得而知。

 

示例一:

使用私有API——CoreTelephony获取本机号码

步骤如下:
1)导入CoreTelephony这个private framework
2)在xxx.m中加入红字部分:

#import "xxx.h"

extern NSString*CTSettingCopyMyPhoneNumber();


@implementation
+(NSString *)myNumber{
<span style="white-space:pre">	</span>return CTSettingCopyMyPhoneNumber();
}
@end


3)在其它地方调用NSLog(@"myNumber=%@",[xxxmyNumber]);即是本机号码

另外机子的唯一标识码等一些信息可以用[UIDevice currentDevice]的uniqueIdentifier,name等属性来获取

 

其他资料:

callblocking on iPhone4

//using private API
//CoreTelephony framework is needed
extern NSString* const kCTSMSMessageReceivedNotification;
extern NSString* constkCTSMSMessageReplaceReceivedNotification;
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);
voidCTTelephonyCenterAddObserver(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, CFDictionaryRefuserInfo)
{
NSString *notifyname=(NSString *)name;
if ([notifynameisEqualToString:@"kCTCallStatusChangeNotification"])//phonecall
{
NSDictionary *info = (NSDictionary*)userInfo;
NSString *state=[[info objectForKey:@"kCTCallStatus"]stringValue];
if ([state isEqualToString:@"5"])//disconnect
NSLog(@”unanswered:%@”,state);
}
else if ([notifynameisEqualToString:@"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 ([notifynameisEqualToString:@"kCTRegistrationDataStatusChangedNotification"])
{
}
else if ([notifynameisEqualToString:@"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 ([notifynameisEqualToString:@"kCTMessageReceivedNotification"])//receivedSMS
{
NSDictionary *info = (NSDictionary *)userInfo;
CFNumberRef msgID = (CFNumberRef)[infoobjectForKey:@"kCTMessageIdKey"];
int result;
CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type,&result);

}
else if ([notifynameisEqualToString:@"kCTIndicatorsSignalStrengthNotification"])//signal
{
}
else if ([notifynameisEqualToString:@"kCTRegistrationStatusChangedNotification"])//networkregistration status
{
}
else if ([notifynameisEqualToString:@"kCTRegistrationDataStatusChangedNotification"])
{

}
else if ([notifynameisEqualToString:@"kCTRegistrationCellChangedNotification"])
{

}
else if ([notifynameisEqualToString:@"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();


展开阅读全文

没有更多推荐了,返回首页