问题1:
我在github上找到了代码,我设法将其导入到我的ARC项目中并设置了一个非ARC标志,一切正常。
我想做的就是将代码转换为ARC,因此处理起来会容易得多。

在尝试修复时,


  C指针类型'CFStringRef'的强制转换.....需要桥接强制转换


之前:NSString *notifyname=(NSString *)name;
之后:NSString *notifyname=(NSString *)CFBridgingRelease(name);
-我摆脱了错误。

然后,


  选择器'sharedMessageCenter'的未知类方法未知
  选择器'incomingMessageWithId:'的类方法没有已知的类
  选择器'messageType'的方法没有已知的选择器类方法
  'canonicalFormat'


问题2:
另外,我无法使其在后台阻止所有电话。
应用启用后:我可以阻止所有电话并断开特定的电话号码,
当应用程序处于后台时:我无法阻止所有电话,只能阻止特定的电话号码。

附言-想编写“停止”功能有什么主意吗?

代码是:

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

最佳答案

在LLVM网站上查看ARC documentation。您必须使用__bridge或其他关键字之一。

这是因为Core Foundation对象(CF * Refs)不受ARC控制,只有Obj-C对象受控制。因此,当您在它们之间进行转换时,必须告诉ARC对象的所有权,以便它可以正确清理它们。最简单的情况是__bridge强制转换,为此ARC不会做任何额外的工作(假定您自己处理对象的内存)。

关于ios - 尝试将非ARC'CallHandler'(调用阻止程序)转换为ARC时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17986911/

10-16 11:01