2013-10-17 152 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

相關問題