应用崩溃与exc_bad_access代码

时间:2013-10-17 12:12:55

标签: ios exception-handling exc-bad-access appdelegate

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

我在我的应用程序中正在做的是,当我收到推送通知时,我在Appdelegate变量(Incoming_NO)中保存Payload中的号码,如果没有此号码的此类联系人,我会创建新联系人并保存它。                                                                                                        当我收到电话时,会显示我之前添加的相同联系人姓名,稍后我允许用户编辑联系人,如果他想要保存联系人或删除,当他编辑联系人时以及稍后我收到推送时相同号码的通知我得到 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),我在保存联系人后收到推送通知时收到错误。

我试图将 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”。

1 个答案:

答案 0 :(得分:0)

首先,为获取Incoming No而做所有这些事情,请尝试自定义有效负载

很简单。

看看Push Notification Programming Guide

你可以尝试:

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

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