2013-10-17 122 views
-1

让我来介绍我的应用程序的功能,我使用推送通知&地址簿和CoreTelephony框架。应用程序崩溃与exc_bad_access代码

我在我的应用程序中做的是,当我得到推送通知时,我将有效载荷中的数字保存在一个Appdelegate变量(Incoming_NO)中,如果没有这样的号码联系,我会创建新的联系人并保存。 当我收到电话时,同样的联系人姓名出现在我之前添加的,稍后我允许用户编辑联系人,如果他想保存联系人或删除,当他编辑联系人,并且以后当我收到与相同数量的我收到EXC_BAD_ACCESS到(Incoming_NO)我已经启用了僵尸和我在同一个地方得到了断点错误推送通知..

谁能帮我有什么问题。

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”。

+0

应用程序崩溃与exc_bad_access由于你是释放objcet,然后仍然在使用这就是为什么发生此错误。 – Jitendra

+0

[应用程序崩溃与exc \ _bad \ _access代码异常]的可能重复(http://stackoverflow.com/questions/19396173/app-crash-with-exc-bad-access-code-exception) – Kerni

+2

请勿发布同样的问题多次! – Kerni

回答

1

首先做所有这些东西获取Incoming No,尝试为此自定义有效载荷。

很简单。

看看Push Notification Programming Guide

你可以尝试:

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

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

+0

我收到有效负载请看下面的内容:收到通知:{ aps = { alert =“Let us talk- + 918884718240”; sound =“哔哔。“ }; } – user236242

+0

我知道你得到了什么,现在从这里你得到的数字,而是使用自定义有效载荷,所以它不会有崩溃或任何对象释放的问题 –

+0

但如何调用didReceiveRemoteNotification?without Push? – user236242

相关问题