1

次の github のコードを使用しています。コードの一部を投稿しているだけです。完全なコードを取得してください。すべての連絡先のバックアップをローカル データベースに取得しようとしているリンク を次に示します。問題なく正常に動作しています。しかし、連絡先を削除するか、新しい連絡先リストを追加すると、アプリが最初の関数で SIGBART でクラッシュします - (id) initWithRecord: (ABRecordRef) aRecord WITH THREAD 1 助けてください!!!!

#import "ABContact.h"
#import "ABContactsHelper.h"
#import "ABContactStringConstants.h"
#import "Base64.h"

#define CFAutorelease(obj) ({CFTypeRef _obj = (obj); (_obj == NULL) ? NULL : [(id)CFMakeCollectable(_obj) autorelease]; })

@implementation ABContact
@synthesize record;

// Thanks to Quentarez, Ciaran

- (id) initWithRecord: (ABRecordRef) aRecord
{
    if (self = [super init]) record = CFRetain(aRecord);
    return self;
}

+ (id) contactWithRecord: (ABRecordRef) person
{
    return [[[ABContact alloc] initWithRecord:person] autorelease];
}

+ (id) contactWithRecordID: (ABRecordID) recordID
{
    ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
    ABRecordRef contactrec = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
    ABContact *contact = [self contactWithRecord:contactrec];

    // CFRelease(contactrec); // Thanks Gary Fung
    return contact;
}

// Thanks to Ciaran
+ (id) contact
{
    ABRecordRef person = ABPersonCreate();
    id contact = [ABContact contactWithRecord:person];
    CFRelease(person);
    return contact;
}

- (void) dealloc
{
    if (record) CFRelease(record);
    [super dealloc];
}

#pragma mark utilities
+ (NSString *) localizedPropertyName: (ABPropertyID) aProperty
{
    return [(NSString *)ABPersonCopyLocalizedPropertyName(aProperty) autorelease];
}

+ (ABPropertyType) propertyType: (ABPropertyID) aProperty
{
    return ABPersonGetTypeOfProperty(aProperty);
}

// Thanks to Eridius for suggestions re switch
+ (NSString *) propertyTypeString: (ABPropertyID) aProperty
{
    switch (ABPersonGetTypeOfProperty(aProperty))
    {
        case kABInvalidPropertyType: return @"Invalid Property";
        case kABStringPropertyType: return @"String";
        case kABIntegerPropertyType: return @"Integer";
        case kABRealPropertyType: return @"Float";
        case kABDateTimePropertyType: return DATE_STRING;
        case kABDictionaryPropertyType: return @"Dictionary";
        case kABMultiStringPropertyType: return @"Multi String";
        case kABMultiIntegerPropertyType: return @"Multi Integer";
        case kABMultiRealPropertyType: return @"Multi Float";
        case kABMultiDateTimePropertyType: return @"Multi Date";
        case kABMultiDictionaryPropertyType: return @"Multi Dictionary";
        default: return @"Invalid Property";
    }
}

+ (NSString *) propertyString: (ABPropertyID) aProperty
{
    /* switch (aProperty) // Sorry, this won't compile
    {
        case kABPersonFirstNameProperty: return FIRST_NAME_STRING;
        case kABPersonMiddleNameProperty: return MIDDLE_NAME_STRING;
        case kABPersonLastNameProperty: return LAST_NAME_STRING;

        case kABPersonPrefixProperty: return PREFIX_STRING;
        case kABPersonSuffixProperty: return SUFFIX_STRING;
        case kABPersonNicknameProperty: return NICKNAME_STRING;

        case kABPersonFirstNamePhoneticProperty: return PHONETIC_FIRST_STRING;
        case kABPersonMiddleNamePhoneticProperty: return PHONETIC_MIDDLE_STRING;
        case kABPersonLastNamePhoneticProperty: return PHONETIC_LAST_STRING;

        case kABPersonOrganizationProperty: return ORGANIZATION_STRING;
        case kABPersonJobTitleProperty: return JOBTITLE_STRING;
        case kABPersonDepartmentProperty: return DEPARTMENT_STRING;

        case kABPersonNoteProperty: return NOTE_STRING;

        case kABPersonKindProperty: return KIND_STRING;

        case kABPersonBirthdayProperty: return BIRTHDAY_STRING;
        case kABPersonCreationDateProperty: return CREATION_DATE_STRING;
        case kABPersonModificationDateProperty: return MODIFICATION_DATE_STRING;

        case kABPersonEmailProperty: return EMAIL_STRING;
        case kABPersonAddressProperty: return ADDRESS_STRING;
        case kABPersonDateProperty: return DATE_STRING;
        case kABPersonPhoneProperty: return PHONE_STRING;
        case kABPersonInstantMessageProperty: return SMS_STRING;
        case kABPersonURLProperty: return URL_STRING;
        case kABPersonRelatedNamesProperty: return RELATED_STRING;          
    } */

    if (aProperty == kABPersonFirstNameProperty) return FIRST_NAME_STRING;
    if (aProperty == kABPersonMiddleNameProperty) return MIDDLE_NAME_STRING;
    if (aProperty == kABPersonLastNameProperty) return LAST_NAME_STRING;

    if (aProperty == kABPersonPrefixProperty) return PREFIX_STRING;
    if (aProperty == kABPersonSuffixProperty) return SUFFIX_STRING;
    if (aProperty == kABPersonNicknameProperty) return NICKNAME_STRING;

    if (aProperty == kABPersonFirstNamePhoneticProperty) return PHONETIC_FIRST_STRING;
    if (aProperty == kABPersonMiddleNamePhoneticProperty) return PHONETIC_MIDDLE_STRING;
    if (aProperty == kABPersonLastNamePhoneticProperty) return PHONETIC_LAST_STRING;

    if (aProperty == kABPersonOrganizationProperty) return ORGANIZATION_STRING;
    if (aProperty == kABPersonJobTitleProperty) return JOBTITLE_STRING;
    if (aProperty == kABPersonDepartmentProperty) return DEPARTMENT_STRING;

    if (aProperty == kABPersonNoteProperty) return NOTE_STRING;

    if (aProperty == kABPersonKindProperty) return KIND_STRING;

    if (aProperty == kABPersonBirthdayProperty) return BIRTHDAY_STRING;
    if (aProperty == kABPersonCreationDateProperty) return CREATION_DATE_STRING;
    if (aProperty == kABPersonModificationDateProperty) return MODIFICATION_DATE_STRING;

    if (aProperty == kABPersonEmailProperty) return EMAIL_STRING;
    if (aProperty == kABPersonAddressProperty) return ADDRESS_STRING;
    if (aProperty == kABPersonDateProperty) return DATE_STRING;
    if (aProperty == kABPersonPhoneProperty) return PHONE_STRING;
    if (aProperty == kABPersonInstantMessageProperty) return SMS_STRING;
    if (aProperty == kABPersonURLProperty) return URL_STRING;
    if (aProperty == kABPersonRelatedNamesProperty) return RELATED_STRING;

    if (aProperty == kABPersonSocialProfileProperty) return SOCIAL_STRING;

    return nil;
}

+ (BOOL) propertyIsMultivalue: (ABPropertyID) aProperty;
{
    if (aProperty == kABPersonFirstNameProperty) return NO;
    if (aProperty == kABPersonMiddleNameProperty) return NO;
    if (aProperty == kABPersonLastNameProperty) return NO;

    if (aProperty == kABPersonPrefixProperty) return NO;
    if (aProperty == kABPersonSuffixProperty) return NO;
    if (aProperty == kABPersonNicknameProperty) return NO;

    if (aProperty == kABPersonFirstNamePhoneticProperty) return NO;
    if (aProperty == kABPersonMiddleNamePhoneticProperty) return NO;
    if (aProperty == kABPersonLastNamePhoneticProperty) return NO;

    if (aProperty == kABPersonOrganizationProperty) return NO;
    if (aProperty == kABPersonJobTitleProperty) return NO;
    if (aProperty == kABPersonDepartmentProperty) return NO;

    if (aProperty == kABPersonNoteProperty) return NO;

    if (aProperty == kABPersonKindProperty) return NO;

    if (aProperty == kABPersonBirthdayProperty) return NO;
    if (aProperty == kABPersonCreationDateProperty) return NO;
    if (aProperty == kABPersonModificationDateProperty) return NO;

    return YES;
    /*
    if (aProperty == kABPersonEmailProperty) return YES;
    if (aProperty == kABPersonAddressProperty) return YES;
    if (aProperty == kABPersonDateProperty) return YES;
    if (aProperty == kABPersonPhoneProperty) return YES;
    if (aProperty == kABPersonInstantMessageProperty) return YES;
    if (aProperty == kABPersonURLProperty) return YES;
    if (aProperty == kABPersonRelatedNamesProperty) return YES;
    if (aProperty == kABPersonSocialProfileProperty) return YES;
     */
}

+ (NSArray *) arrayForProperty: (ABPropertyID) anID inRecord: (ABRecordRef) record
{
    // Recover the property for a given record
    CFTypeRef theProperty = ABRecordCopyValue(record, anID);
    NSArray *items = (NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty);
    CFRelease(theProperty);
    return [items autorelease];
}

+ (id) objectForProperty: (ABPropertyID) anID inRecord: (ABRecordRef) record
{
    return [(id) ABRecordCopyValue(record, anID) autorelease];
}

+ (NSDictionary *) dictionaryWithValue: (id) value andLabel: (CFStringRef) label
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (value) [dict setObject:value forKey:@"value"];
    if (label) [dict setObject:(NSString *)label forKey:@"label"];
    return dict;
}

+ (NSDictionary *) addressWithStreet: (NSString *) street withCity: (NSString *) city
                           withState:(NSString *) state withZip: (NSString *) zip
                         withCountry: (NSString *) country withCode: (NSString *) code
{
    NSMutableDictionary *md = [NSMutableDictionary dictionary];
    if (street) [md setObject:street forKey:(NSString *) kABPersonAddressStreetKey];
    if (city) [md setObject:city forKey:(NSString *) kABPersonAddressCityKey];
    if (state) [md setObject:state forKey:(NSString *) kABPersonAddressStateKey];
    if (zip) [md setObject:zip forKey:(NSString *) kABPersonAddressZIPKey];
    if (country) [md setObject:country forKey:(NSString *) kABPersonAddressCountryKey];
    if (code) [md setObject:code forKey:(NSString *) kABPersonAddressCountryCodeKey];
    return md;
}

+ (NSDictionary *) smsWithService: (CFStringRef) service andUser: (NSString *) userName
{
    NSMutableDictionary *sms = [NSMutableDictionary dictionary];
    if (service) [sms setObject:(NSString *) service forKey:(NSString *) kABPersonInstantMessageServiceKey];
    if (userName) [sms setObject:userName forKey:(NSString *) kABPersonInstantMessageUsernameKey];
    return sms;
}

+ (NSDictionary *) socialWithService: (CFStringRef) service andUser: (NSString *) userName andUserIdentifier: (NSString *) userIdentifier
{
    NSMutableDictionary *social = [NSMutableDictionary dictionary];
    if (service) [social setObject:(NSString *) service forKey:(NSString *) kABPersonSocialProfileServiceKey];
    if (userName) [social setObject:userName forKey:(NSString *) kABPersonSocialProfileUsernameKey];
    if (userIdentifier) [social setObject:userIdentifier forKey:(NSString *) kABPersonSocialProfileUserIdentifierKey];
    return social;
}

// Thanks to Eridius for suggestions re: error
- (BOOL) removeSelfFromAddressBook: (NSError **) error
{
    ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
    if (!ABAddressBookRemoveRecord(addressBook, self.record, (CFErrorRef *) error)) return NO;
    return ABAddressBookSave(addressBook,  (CFErrorRef *) error);
}

#pragma mark Record ID and Type
- (ABRecordID) recordID {return ABRecordGetRecordID(record);}
- (ABRecordType) recordType {return ABRecordGetRecordType(record);}
- (BOOL) isPerson {return self.recordType == kABPersonType;}

#pragma mark Getting Single Value Strings
- (NSString *) getRecordString:(ABPropertyID) anID
{
    return [(NSString *) ABRecordCopyValue(record, anID) autorelease];
}
- (NSString *) firstname {return [self getRecordString:kABPersonFirstNameProperty];}
- (NSString *) middlename {return [self getRecordString:kABPersonMiddleNameProperty];}
- (NSString *) lastname {return [self getRecordString:kABPersonLastNameProperty];}

- (NSString *) prefix {return [self getRecordString:kABPersonPrefixProperty];}
- (NSString *) suffix {return [self getRecordString:kABPersonSuffixProperty];}
- (NSString *) nickname {return [self getRecordString:kABPersonNicknameProperty];}

- (NSString *) firstnamephonetic {return [self getRecordString:kABPersonFirstNamePhoneticProperty];}
- (NSString *) middlenamephonetic {return [self getRecordString:kABPersonMiddleNamePhoneticProperty];}
- (NSString *) lastnamephonetic {return [self getRecordString:kABPersonLastNamePhoneticProperty];}

- (NSString *) organization {return [self getRecordString:kABPersonOrganizationProperty];}
- (NSString *) jobtitle {return [self getRecordString:kABPersonJobTitleProperty];}
- (NSString *) department {return [self getRecordString:kABPersonDepartmentProperty];}
- (NSString *) note {return [self getRecordString:kABPersonNoteProperty];}

#pragma mark Contact Name Utility
- (NSString *) contactName
{
    NSMutableString *string = [NSMutableString string];

    if (self.firstname || self.lastname)
    {
        if (self.prefix) [string appendFormat:@"%@ ", self.prefix];
        if (self.firstname) [string appendFormat:@"%@ ", self.firstname];
        if (self.nickname) [string appendFormat:@"\"%@\" ", self.nickname];
        if (self.lastname) [string appendFormat:@"%@", self.lastname];

        if (self.suffix && string.length)
            [string appendFormat:@", %@ ", self.suffix];
        else
            [string appendFormat:@" "];
    }

    if (self.organization) [string appendString:self.organization];
    return [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSString *) compositeName
{
    NSString *string = (NSString *)ABRecordCopyCompositeName(record);
    return [string autorelease];
}

#pragma mark NUMBER
- (NSNumber *) getRecordNumber: (ABPropertyID) anID
{
    return [(NSNumber *) ABRecordCopyValue(record, anID) autorelease];
}

- (NSNumber *) kind {return [self getRecordNumber:kABPersonKindProperty];}



@end`
4

0 に答える 0