问题1: 我在github上找到了代码,我设法将它导入我的ARC项目并设置了非ARC标志,一切正常。 我正在尝试将代码转换为ARC,因此处理它会更容易。
试图修复,
C指针类型'CFStringRef'的演员......需要一个桥接演员
之前:NSString *notifyname=(NSString *)name;
之后:NSString *notifyname=(NSString *)CFBridgingRelease(name);
- 我摆脱了错误。
然后,
选择器'sharedMessageCenter'没有已知的类方法未知 选择器'incomingMessageWithId的类方法:'没有已知的类 选择器'messageType'的方法没有选择器的已知类方法 'canonicalFormat'
问题2: 在旁边,我无法阻止后台的所有电话。 当应用处于活动状态时:我可以屏蔽所有电话并断开特定的电话号码, 当应用程序处于后台时:我无法阻止所有电话,只能阻止特定的电话号码。
P.S。 - 有什么想法写'停止'功能吗?
代码是:
CallHandler.h:
//
// CallHandler.h
// PhoneCallBlock
//
// Created by Hui Li on 12-5-11.
// Copyright (c) 2012年 hust. All rights reserved.
//
#import <Foundation/Foundation.h>
#define kCTIndicatorsSignalStrengthNotification @"kCTIndicatorsSignalStrengthNotification"
#define kCTRegistrationStatusChangedNotification @"kCTRegistrationStatusChangedNotification"
#define kCTRegistrationDataStatusChangedNotification @"kCTRegistrationDataStatusChangedNotification"
#define kCTRegistrationCellChangedNotification @"kCTRegistrationCellChangedNotification"
#define kCTIndicatorRadioTransmitNotification @"kCTIndicatorRadioTransmitNotification"
@interface CallHandler : NSObject
{
}
+ (void)start;
static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo);
static void signalHandler(int sigraised);
@end
CallHandler.m:
//
// CallHandler.m
// PhoneCallBlock
//
// Created by Hui Li on 12-5-11.
// Copyright (c) 2012年 hust. All rights reserved.
//
#import "CallHandler.h"
@implementation CallHandler
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 *);
extern void CTCallDisconnect(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/*always 0*/,NSString* number,NSString* text);
void * CTSMSMessageCreateReply(void* unknow/*always 0*/,void * forwardTo,NSString* text);
id CTTelephonyCenterGetDefault(void);
void CTTelephonyCenterAddObserver(id,id,CFNotificationCallback,NSString*,void*,int);
void CTTelephonyCenterRemoveObserver(id,id,NSString*,void*);
int CTSMSMessageGetUnreadCount(void);
#pragma mark - Call Block Methods
+ (void)start
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Initialize listener by adding CT Center observer implicit
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver( ct, NULL, callback,NULL,NULL,
CFNotificationSuspensionBehaviorHold);
// Handle Interrupts
sig_t oldHandler = signal(SIGINT, signalHandler);
if (oldHandler == SIG_ERR)
{
NSLog(@"Could not establish new signal handler");
exit(1);
}
// Run loop lets me catch notifications
NSLog(@"Starting run loop and watching for notification.\n");
CFRunLoopRun();
// Shouldn't ever get here. Bzzzt
NSLog(@"Unexpectedly back from CFRunLoopRun()!\n");
[pool release];
}
static void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSString *notifyname=(NSString *)name;
if ([notifyname isEqualToString:@"kCTCallStatusChangeNotification"])//电话
{
NSDictionary *info = (NSDictionary*)userInfo;
NSString *state=[info[@"kCTCallStatus"] stringValue];
if ([state isEqualToString:@"5"])//disconnect
NSLog(@"未接:%@",state);
}
else if ([notifyname isEqualToString:@"kCTCallIdentificationChangeNotification"])
{
NSDictionary *info = (NSDictionary *)userInfo;
CTCall *call = (CTCall *)info[@"kCTCall"];
NSString *caller = CTCallCopyAddress(NULL, call);
NSLog(@"电话号码:%@",caller);
if ([caller isEqualToString:@"1800123456"])
{
//disconnect this call
NSLog(@"挂雷冰");
CTCallDisconnect(call);
}
}
else if ([notifyname isEqualToString:@"kCTMessageReceivedNotification"])//收到短信
{
/*
kCTMessageIdKey = "-2147483636";
kCTMessageTypeKey = 1;
*/
NSDictionary *info = (NSDictionary *)userInfo;
CFNumberRef msgID = (CFNumberRef)info[@"kCTMessageIdKey"];
int result;
CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);
Class CTMessageCenter = NSClassFromString(@"CTMessageCenter");
id mc = [CTMessageCenter sharedMessageCenter];
id incMsg = [mc incomingMessageWithId: result];
int msgType = (int)[incMsg messageType];
if (msgType == 1) //experimentally detected number
{
id phonenumber = [incMsg sender];
NSString *senderNumber = (NSString *)[phonenumber canonicalFormat];
id incMsgPart = [incMsg items][0];
NSData *smsData = [incMsgPart data];
NSString *smsText = [[NSString alloc] initWithData:smsData encoding:NSUTF8StringEncoding];
}
}
else if ([notifyname isEqualToString:@"kCTIndicatorsSignalStrengthNotification"])//信号
{
/*
kCTIndicatorsGradedSignalStrength = 2;
kCTIndicatorsRawSignalStrength = "-101";
kCTIndicatorsSignalStrength = 19;
*/
}
else if ([notifyname isEqualToString:@"kCTRegistrationStatusChangedNotification"])//网络注册状态
{
/*
kCTRegistrationInHomeCountry = 1;
kCTRegistrationStatus = kCTRegistrationStatusRegisteredHome;
*/
}
else if ([notifyname isEqualToString:@"kCTRegistrationDataStatusChangedNotification"])
{
/*
kCTRegistrationDataActive = 1;
kCTRegistrationDataAttached = 1;
kCTRegistrationDataConnectionServices = (
kCTDataConnectionServiceTypeInternet,
kCTDataConnectionServiceTypeWirelessModemTraffic,
kCTDataConnectionServiceTypeWirelessModemAuthentication
);
kCTRegistrationDataContextID = 0;
kCTRegistrationDataIndicator = kCTRegistrationDataIndicator3G;
kCTRegistrationDataStatus = kCTRegistrationDataStatusAttachedAndActive;
kCTRegistrationDataStatusInternationalRoaming = 1;
kCTRegistrationRadioAccessTechnology = kCTRegistrationRadioAccessTechnologyUTRAN;
*/
}
else if ([notifyname isEqualToString:@"kCTRegistrationCellChangedNotification"])
{
/*
kCTRegistrationGsmCellId = 93204174;
kCTRegistrationGsmLac = 55583;
kCTRegistrationInHomeCountry = 1;
kCTRegistrationRadioAccessTechnology = kCTRegistrationRadioAccessTechnologyUTRAN;
*/
}
else if ([notifyname isEqualToString:@"kCTIndicatorRadioTransmitNotification"])
{
/*
kCTRadioTransmitDCHStatus = 1;
*/
}
//NSLog(@"名字:%@-详细:%@",notifyname,userInfo);
}
static void signalHandler(int sigraised)
{
NSLog(@"\nInterrupted.\n");
exit(0);
}
@end
答案 0 :(得分:1)
查看LLVM网站上的ARC documentation。您必须使用__bridge或其他关键字之一。
这是因为Core Foundation对象(CF * Refs)不受ARC控制,只有Obj-C对象。因此,当您在它们之间进行转换时,您必须告诉ARC有关对象的所有权,以便它可以正确地清理它们。最简单的情况是__bridge演员,ARC不会做任何额外的工作(它假设你自己处理对象的内存)。