-1

让我介绍一下我的App功能,我使用推送通知和地址簿和CoreTelephony框架。

我在我的应用程序中所做的是,当我收到推送通知时,我将 Payload 中的号码保存在 Appdelegate 变量(Incoming_NO)中,如果没有此号码的此类联系人,我将创建新联系人并保存它。当我接到电话时,会出现与我之前添加的相同的联系人姓名,稍后我允许用户编辑联系人,如果他想保存或删除联系人,当他编辑联系人时,然后当我收到推送时具有相同编号的通知我得到了exc_bad_access (Incoming_NO) 我启用了 Zombie 并且我在同一个地方遇到了断点错误..

任何人都可以帮我解决什么问题。

    Received notification: {
        aps =     {
            alert = "Please help me-+918884718240";
            sound = "beep.caf";
        };
    }

代码 :

 NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];

 NSRange range=[alertValue rangeOfString:@":"];
    NSString *param,*msg;
    NSRange range1=[alertValue rangeOfString:@":"];
    if (range1.location != NSNotFound)
    {
        param = [alertValue substringFromIndex:range1.location + range1.length];

        msg=[alertValue substringToIndex:range.location + range.length-1];
    }
    else
    {
        range1=[alertValue rangeOfString:@"-"];
        if (range1.location != NSNotFound)
        {
            param = [alertValue substringFromIndex:range1.location + range1.length];

            msg=[alertValue substringToIndex:range1.location + range1.length-1];
        }


    }
if(range.length!=0)
    {
        parts= [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@":"]];

    }else
    {
        parts = [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@"-"]];
    }

incoming_Number =[parts objectAtIndex:1];        

对于此变量(传入_编号),我在保存联系人后收到推送通知时收到错误消息。

我试图将incoming_Number类型更改为NSString 和NSMutableString,仍然是同样的错误,我猜发生的事情是我指的是释放的对象。但在调试器中,我可以看到它具有价值。

添加联系代码:

- (void)setContacts:(UIImage *) imgdata :(NSString *)incoming_number {


ABRecordRef person=NULL;
ABRecordRef loopingPerson=NULL;
_Bool vizzical_present=false;
CFErrorRef myError = NULL;
NSArray *allContacts;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &myError);
//    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, nil);

__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    //dispatch_release(sema);
}
else { // we're on iOS 5 or older
    accessGranted = YES;
}

if (accessGranted) {
   //     int count = (int) ABAddressBookGetPersonCount(addressBook);


   allContacts = (__bridge_transfer NSArray
                            *)ABAddressBookCopyArrayOfAllPeople(addressBook);

  for(CFIndex i = 1; i < allContacts.count; i++)
    {

        loopingPerson = (__bridge ABRecordRef)allContacts[i];

        CFStringRef firstName;
        // char *lastNameString, *firstNameString;
        firstName = ABRecordCopyValue(loopingPerson, kABPersonFirstNameProperty);
        if([(__bridge NSString *)(firstName) isEqualToString:@"VizziCal"]){
            vizzical_present=true;
        }
        ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(loopingPerson, kABPersonPhoneProperty);

        //     NSMutableArray *numbersArray = [[NSMutableArray alloc] init];

       // CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, 0 );
        CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, 0 );
        NSString* noSpaces =
        [[(__bridge NSString *)phoneNumberValue componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
        if(phoneNumberValue!=NULL){
        NSString* noHypen =
        [noSpaces stringByReplacingOccurrencesOfString:@"-" withString:@""];
       // CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );
       // NSString *addPlus=[[NSString alloc]initWithFormat:@"%@",incoming_number] ;

            if([incoming_number isEqual:noHypen] || ([incoming_number rangeOfString:noHypen].location!=NSNotFound)){
       NSLog(@"%@  and %@ and %ld",incoming_number,noHypen,i);
                person=loopingPerson;
            break;

            }
        }

    }

    if(person!=NULL){
    CFErrorRef error = nil;
    CFDataRef imageData = ABPersonCopyImageData(person);
        NSData* imageData1 = (__bridge NSData*)ABPersonCopyImageData(person);


    UIImage *image = [UIImage imageWithData:(__bridge NSData *)(imageData)];

        UIImage *image1 = [UIImage imageWithData:(NSData *)(imageData1)];


    NSData *dataRef;
    UIImage  *mergedImage;
    if(image!=NULL)
    {
        dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   savedImagePath= [self saveImage:image1 forPerson:@"image"];

                 });


        dataRef = UIImagePNGRepresentation(imgdata);

        CFRelease(imageData);
    }
    else{

        if(imgdata.size.height <480 && imgdata.size.width<320)

        {

        UIImage *image = [UIImage imageNamed:@"blank_image.png"];


        mergedImage=[self mergeTwoImages:imgdata :image];


        CGFloat width = imgdata.size.width;
        CGFloat height = imgdata.size.height;

        NSLog(@"Height:%f and Width =%f",width,height);
        dataRef = UIImagePNGRepresentation(mergedImage);
        }

        else{

         dataRef = UIImagePNGRepresentation(imgdata);
        }

    }
    CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

    if (ABPersonHasImageData(person)) {
        ABPersonRemoveImageData(person, &error);
        ABAddressBookSave(addressBook, &error);

    }
    //ABRecordSetValue(person, kABPersonFirstNameProperty, @"Don Juan", NULL);

    // ABAddressBookAddRecord(addressBook, person, &error);

    if (ABPersonSetImageData(person, cfDataRef, &error)) {
        if (ABAddressBookHasUnsavedChanges(addressBook)) {
            NSLog(@"has unsaved changes");
        } else {
            NSLog(@"nothing to save");
        }
        if (ABAddressBookSave(addressBook, &error)) {
            NSLog(@"saved");
        } else {
            NSLog(@"not saved");
        }
    }

    }

    else{

        if(!vizzical_present)
        {
        ABRecordRef newPerson = ABPersonCreate();

        ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"VizziCal", &myError);

            CFDataRef dataRef = (__bridge CFDataRef)(UIImagePNGRepresentation(imgdata));
            [prefs setBool:YES forKey:@"contact-created"];


        //Phone number is a list of phone number, so create a multivalue
        ABMutableMultiValueRef phoneNumberMultiValue =
        ABMultiValueCreateMutable(kABPersonPhoneProperty);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(incoming_number),kABPersonPhoneMobileLabel, NULL);
   // ...
            // Set other properties
            ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneNumberMultiValue, &myError);
            // ...
            ABAddressBookAddRecord(addressBook, newPerson, &myError);



        ABAddressBookSave(addressBook, &myError);

        if (myError != NULL)
        {
            CFStringRef errorDesc = CFErrorCopyDescription(myError);
            NSLog(@"Contact not saved: %@", errorDesc);
            CFRelease(errorDesc);        
        }



        if (ABPersonSetImageData(newPerson, dataRef, &myError)) {
            if (ABAddressBookHasUnsavedChanges(addressBook)) {
                NSLog(@"has unsaved changes");
            } else {
                NSLog(@"nothing to save");
            }
            if (ABAddressBookSave(addressBook, &myError)) {
                NSLog(@"saved");
            } else {
                NSLog(@"not saved");
            }
        }

        CFRelease(newPerson);
        CFRelease(addressBook);

        CFRelease(phoneNumberMultiValue);
    }
        else
        {
            ABRecordRef newPerson = ABPersonCreate();


            for(CFIndex i = 1; i < allContacts.count; i++)
            {

                loopingPerson = (__bridge ABRecordRef)allContacts[i];

                CFStringRef firstName;
                // char *lastNameString, *firstNameString;
                firstName = ABRecordCopyValue(loopingPerson, kABPersonFirstNameProperty);
                NSString *name=(__bridge NSString *)(firstName);

                if([name isEqualToString:@"VizziCal"])
                    break;



            }
            if(loopingPerson !=NULL )
            {
                      [prefs setBool:YES forKey:@"contact-created"];

                    CFDataRef dataRef = (__bridge CFDataRef)(UIImagePNGRepresentation(imgdata));


                    //Phone number is a list of phone number, so create a multivalue
                    ABMutableMultiValueRef phoneNumberMultiValue =
                    ABMultiValueCreateMutable(kABPersonPhoneProperty);
                    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(incoming_number),kABPersonPhoneMobileLabel, NULL);
                    // ...
                    // Set other properties
                    ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneNumberMultiValue, &myError);
                    // ...
                    ABAddressBookAddRecord(addressBook, newPerson, &myError);


                    ABAddressBookSave(addressBook, &myError);

                    if (myError != NULL)
                    {
                        CFStringRef errorDesc = CFErrorCopyDescription(myError);
                        NSLog(@"Contact not saved: %@", errorDesc);
                        CFRelease(errorDesc);
                    }



                    if (ABPersonSetImageData(newPerson, dataRef, &myError)) {
                        if (ABAddressBookHasUnsavedChanges(addressBook)) {
                            NSLog(@"has unsaved changes");
                        } else {
                            NSLog(@"nothing to save");
                        }
                        if (ABAddressBookSave(addressBook, &myError)) {
                            NSLog(@"saved");
                        } else {
                            NSLog(@"not saved");
                        }
                    }

                    CFRelease(newPerson);
                    CFRelease(addressBook);

                    CFRelease(phoneNumberMultiValue);

            }
        }

    }

}
}

我在这里做的是,我正在检查人是否存在,如果不存在,我正在检查默认联系人“VizziCal”,如果它不存在,那么我将创建新联系人作为“VizziCal”。

4

1 回答 1

0

首先宁愿做所有这些东西来获取Incoming No,尝试自定义有效负载

很简单。

看看推送通知编程指南

你可以试试:

{ "alert": "Please help me", "phone": "9999999999" }

注意:这将在服务器端创建。

于 2013-10-17T12:26:27.923 回答