2010-02-11 5 views
0

Я создаю новый контакт программно. он работает хорошо, кроме адреса. Ниже код для добавления контактаiPhone: Обновить Все адреса (Главная, Работа, Другое) контакта

ABAddressBookRef libroDirec = ABAddressBookCreate(); 
ABRecordRef persona = ABPersonCreate(); 
ABRecordSetValue(persona, kABPersonFirstNameProperty, tempSingle.firstName , nil); 
     ABRecordSetValue(persona, kABPersonLastNameProperty, tempSingle.lastName, nil); 
     ABRecordSetValue(persona, kABPersonMiddleNameProperty, tempSingle.middleName, nil); 

if([tempSingle.homeStreet1 length]>0 || [tempSingle.homeStreet2 length]>0 || [tempSingle.homeCity length]>0 || [tempSingle.homeState length]>0 || [tempSingle.homePostal length]>0 || [tempSingle.homeCountry length]>0) 
     { 
      ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 

      NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init]; 
      NSString *homeStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.homeStreet1,tempSingle.homeStreet2]; 
      [addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey]; 
      [addressDictionary setObject:tempSingle.homeCity forKey:(NSString *)kABPersonAddressCityKey]; 
      [addressDictionary setObject:tempSingle.homeState forKey:(NSString *)kABPersonAddressStateKey]; 
      [addressDictionary setObject:tempSingle.homePostal forKey:(NSString *)kABPersonAddressZIPKey]; 
      [addressDictionary setObject:tempSingle.homeCountry forKey:(NSString *)kABPersonAddressCountryKey]; 
      //[addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryCodeKey]; 
      bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL); 
      if(didAddHome) 
      { 
       ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL); 
      } 
      [addressDictionary release]; 
     } 

     if([tempSingle.workStreet1 length]>0 || [tempSingle.workStreet2 length]>0 || [tempSingle.workCity length]>0 || [tempSingle.workState length]>0 || [tempSingle.workPostal length]>0 || [tempSingle.workCountry length]>0) 
     { 
      ABMutableMultiValueRef multiWork = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 

      NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init]; 
      NSString *workStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.workStreet1,tempSingle.workStreet2]; 
      [addressDictionary1 setObject:workStreetAddress forKey:(NSString *) kABPersonAddressStreetKey]; 
      [addressDictionary1 setObject:tempSingle.workCity forKey:(NSString *)kABPersonAddressCityKey]; 
      [addressDictionary1 setObject:tempSingle.workState forKey:(NSString *)kABPersonAddressStateKey]; 
      [addressDictionary1 setObject:tempSingle.workPostal forKey:(NSString *)kABPersonAddressZIPKey]; 
      [addressDictionary1 setObject:tempSingle.workCountry forKey:(NSString *)kABPersonAddressCountryKey]; 
      bool didAddWork = ABMultiValueAddValueAndLabel(multiWork, addressDictionary1, kABWorkLabel, NULL); 
      if(didAddWork) 
      { 
       ABRecordSetValue(persona, kABPersonAddressProperty, multiWork, NULL); 
      } 
      [addressDictionary1 release]; 
     } 

     if([tempSingle.otherStreet1 length]>0 || [tempSingle.otherStreet2 length]>0 || [tempSingle.otherCity length]>0 || [tempSingle.otherState length]>0 || [tempSingle.otherPostal length]>0 || [tempSingle.otherCountry length]>0) 
     { 
      ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 

      NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init]; 
      NSString *otherStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.otherStreet1,tempSingle.otherStreet2]; 
      [addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey]; 
      [addressDictionary2 setObject:tempSingle.otherCity forKey:(NSString *)kABPersonAddressCityKey]; 
      [addressDictionary2 setObject:tempSingle.otherState forKey:(NSString *)kABPersonAddressStateKey]; 
      [addressDictionary2 setObject:tempSingle.otherPostal forKey:(NSString *)kABPersonAddressZIPKey]; 
      [addressDictionary2 setObject:tempSingle.otherCountry forKey:(NSString *)kABPersonAddressCountryKey]; 
      bool didAddOther = ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABOtherLabel, NULL); 
      if(didAddOther) 
      { 
       ABRecordSetValue(persona, kABPersonAddressProperty, multiOther, NULL); 
      } 
      [addressDictionary2 release]; 
     } 
ABAddressBookAddRecord(libroDirec, persona, nil); 

     CFRelease(persona); 
ABAddressBookSave(libroDirec, nil); 
    CFRelease(libroDirec); 

Если я сохранить только домашний адрес или только рабочий адрес или только другой адрес, то код работает хорошо. но если я сохраню адрес (домашний, рабочий и другой), то только последний адрес сохранить для контактов. Пожалуйста, предложите, как я могу решить эту ошибку , пожалуйста, предложите, что не так?

+0

Что такое сообщение об ошибке Ваше получение ?? – satish

+0

Сообщение об ошибке отсутствует. Превосходный код работает хорошо. в первую очередь извините за неполное описание. Я также добавляю работу и другой адрес после домашнего адреса, поэтому я получаю только последний адрес (другой адрес), когда я вижу контакты. теперь я редактирую свое оригинальное сообщение – Rupesh

ответ

0

Свойство, идентифицированное kABPersonAddressProperty, может принимать только одно значение, которое имеет тип ABMutableMultiValueRef, и этот многозначный может содержать много словарей в нем с некоторой меткой.

Что вы, возможно, потребуется, вероятно, это ..

ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 

if([tempSingle.homeStreet1 length]>0 || [tempSingle.homeStreet2 length]>0 || [tempSingle.homeCity length]>0 || [tempSingle.homeState length]>0 || [tempSingle.homePostal length]>0 || [tempSingle.homeCountry length]>0) 
        { 
            NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init]; 
            NSString *homeStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.homeStreet1,tempSingle.homeStreet2]; 
            [addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey]; 
            [addressDictionary setObject:tempSingle.homeCity forKey:(NSString *)kABPersonAddressCityKey]; 
            [addressDictionary setObject:tempSingle.homeState forKey:(NSString *)kABPersonAddressStateKey]; 
            [addressDictionary setObject:tempSingle.homePostal forKey:(NSString *)kABPersonAddressZIPKey]; 
            [addressDictionary setObject:tempSingle.homeCountry forKey:(NSString *)kABPersonAddressCountryKey]; 
            //[addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryCodeKey]; 
            bool didAddHome = ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABHomeLabel, NULL); 
            if(!didAddHome) 
            { 
                NSLog(@"unable to add address"); 
            } 
            [addressDictionary release]; 
        } 

        if([tempSingle.workStreet1 length]>0 || [tempSingle.workStreet2 length]>0 || [tempSingle.workCity length]>0 || [tempSingle.workState length]>0 || [tempSingle.workPostal length]>0 || [tempSingle.workCountry length]>0) 
        { 
            NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init]; 
            NSString *workStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.workStreet1,tempSingle.workStreet2]; 
            [addressDictionary1 setObject:workStreetAddress forKey:(NSString *) kABPersonAddressStreetKey]; 
            [addressDictionary1 setObject:tempSingle.workCity forKey:(NSString *)kABPersonAddressCityKey]; 
            [addressDictionary1 setObject:tempSingle.workState forKey:(NSString *)kABPersonAddressStateKey]; 
            [addressDictionary1 setObject:tempSingle.workPostal forKey:(NSString *)kABPersonAddressZIPKey]; 
            [addressDictionary1 setObject:tempSingle.workCountry forKey:(NSString *)kABPersonAddressCountryKey]; 
            bool didAddWork = ABMultiValueAddValueAndLabel(multiAddress, addressDictionary1, kABWorkLabel, NULL); 
            if(!didAddWork) 
            { 
                NSLog(@"unable to add address"); 
            } 
            [addressDictionary1 release]; 
        } 

        if([tempSingle.otherStreet1 length]>0 || [tempSingle.otherStreet2 length]>0 || [tempSingle.otherCity length]>0 || [tempSingle.otherState length]>0 || [tempSingle.otherPostal length]>0 || [tempSingle.otherCountry length]>0) 
        { 
            NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init]; 
            NSString *otherStreetAddress=[NSString stringWithFormat:@"%@ %@",tempSingle.otherStreet1,tempSingle.otherStreet2]; 
            [addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey]; 
            [addressDictionary2 setObject:tempSingle.otherCity forKey:(NSString *)kABPersonAddressCityKey]; 
            [addressDictionary2 setObject:tempSingle.otherState forKey:(NSString *)kABPersonAddressStateKey]; 
            [addressDictionary2 setObject:tempSingle.otherPostal forKey:(NSString *)kABPersonAddressZIPKey]; 
            [addressDictionary2 setObject:tempSingle.otherCountry forKey:(NSString *)kABPersonAddressCountryKey]; 
            bool didAddOther = ABMultiValueAddValueAndLabel(multiAddress, addressDictionary2, kABOtherLabel, NULL); 
            if(!didAddOther) 
            { 
                NSLog(@"unable to add address"); 
            } 
            [addressDictionary2 release]; 
        } 
ABRecordSetValue(persona, kABPersonAddressProperty, multiAddress, NULL); 
ABAddressBookAddRecord(libroDirec, persona, nil); 

CFRelease(persona); 
ABAddressBookSave(libroDirec, nil); 
CFRelease(libroDirec);