1

私は、ユーザーが多くの連絡先を持っている場所でクラッシュする iPhone アプリケーションに取り組んでいます。500 件以上の連絡先。以下は関数呼び出しです。よろしくお願いします。

- (IBAction)addToAddressBook{

NSString *importString = [[NSString alloc] init];
importString = importTextView.text;
if (importString==nil) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Could not Find Text" 
                                                    message:@"Try Pasting the Text Again" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
statusLabel.text = @"Importing Lines";
NSArray *importLines = [importString componentsSeparatedByString:@"\n"];
[importString release];
ABAddressBookRef addressBook = ABAddressBookCreate();
CFRetain(addressBook);

ABRecordRef person;
CFErrorRef error = NULL;


ABMutableMultiValueRef multiPhone;
ABMutableMultiValueRef multiURL = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
ABMutableMultiValueRef multiDate = ABMultiValueCreateMutable(kABMultiStringPropertyType);

NSMutableDictionary *addressDictionary;
statusLabel.text = @"Counting Records";
int totalRecords = 0;
for( int j = 0; j < 5; j++){
    NSString *line = [importLines objectAtIndex:j];
    if ([line rangeOfString: @"Contacts:"].location!=NSNotFound) {
        NSString *totalContactsLabel = [[line componentsSeparatedByString:@" Contacts: "] objectAtIndex:1];
        totalRecords = [totalContactsLabel intValue];
    }
}






int recordNumber = 0;
statusLabel.text = [NSString stringWithFormat:@"%i/%i", recordNumber, totalRecords];
for (NSString *line in importLines)
{
 if (![line isEqualToString:@""])
{

        NSMutableString *mutableLine = [NSMutableString stringWithString: line];


        if ([line hasPrefix:@"---"]) {
            //NSLog(@"starting new person");
            person = ABPersonCreate();
            multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); //clear multiphone for each new person
            multiURL = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
            multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
            multiDate = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 


        } else if ([line hasPrefix:@"First Name: "]) {
            [mutableLine deleteCharactersInRange: [mutableLine rangeOfString: @"First Name: "]];
            line = (NSString *)mutableLine;
            ABRecordSetValue(person, kABPersonFirstNameProperty, line, &error);

        } else if ([line hasPrefix:@"Middle Name: "]) {
            [mutableLine deleteCharactersInRange: [mutableLine rangeOfString: @"Middle Name: "]];
            line = (NSString *)mutableLine;
            ABRecordSetValue(person, kABPersonMiddleNameProperty, line, &error);

        } else if ([line hasPrefix:@"Last Name: "]) {
            [mutableLine deleteCharactersInRange: [mutableLine rangeOfString: @"Last Name: "]];
            line = (NSString *)mutableLine;
            ABRecordSetValue(person, kABPersonLastNameProperty, line, &error);

        } else if ([line rangeOfString: @"Phone:"].location!=NSNotFound) {
            NSString *phoneLabel = [[line componentsSeparatedByString:@" Phone: "] objectAtIndex:0];
            NSString *phoneContent = [[line componentsSeparatedByString:@" Phone: "] objectAtIndex:1];
            if ([phoneLabel isEqualToString:@"Mobile"]) {
                ABMultiValueAddValueAndLabel(multiPhone, phoneContent, kABPersonPhoneMobileLabel, NULL);
                ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
            } else if ([phoneLabel isEqualToString:@"Home"]) {
                ABMultiValueAddValueAndLabel(multiPhone, phoneContent, kABHomeLabel, NULL);
                ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
            } else if ([phoneLabel isEqualToString:@"Work"]) {
                ABMultiValueAddValueAndLabel(multiPhone, phoneContent, kABWorkLabel, NULL);
                ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
            } else if ([phoneLabel isEqualToString:@"iPhone"]) {
                ABMultiValueAddValueAndLabel(multiPhone, phoneContent, kABPersonPhoneIPhoneLabel, NULL);
                ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
            } else {
                ABMultiValueAddValueAndLabel(multiPhone, phoneContent, (CFStringRef)phoneLabel, NULL);
                ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
            }
        }else if ([line rangeOfString: @"Email:"].location!=NSNotFound) {
            NSString *emailLabel = [[line componentsSeparatedByString:@" Email: "] objectAtIndex:0];
            NSString *emailContent = [[line componentsSeparatedByString:@" Email: "] objectAtIndex:1];
            if ([emailLabel isEqualToString:@"Main"]) {
                ABMultiValueAddValueAndLabel(multiEmail, emailContent, (CFStringRef)@"Main", NULL);
                ABRecordSetValue(person, kABPersonEmailProperty, multiEmail, &error);
            }else if ([emailLabel isEqualToString:@"Work"]) {
                ABMultiValueAddValueAndLabel(multiEmail, emailContent, kABWorkLabel, NULL);
                ABRecordSetValue(person, kABPersonEmailProperty, multiEmail, &error);
            } else {
                ABMultiValueAddValueAndLabel(multiEmail, emailContent, (CFStringRef)emailLabel, NULL);
                ABRecordSetValue(person, kABPersonEmailProperty, multiEmail, &error);
            }
        }

        if ([line hasPrefix:@"- -"]) {
            //ABRecordSetValue(person, kABPersonEmailProperty, multiEmail, &error);

            NSString *updateText;
            NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

            if (![self personAlreadyExists:firstName withName:lastName]){
                ABAddressBookAddRecord(addressBook, person, &error);
                ABAddressBookSave(addressBook, &error);
                recordNumber++;

                if (firstName==nil) {
                    updateText = [NSString stringWithFormat:@"%@ added.", lastName];
                } else if (lastName==nil) {
                    updateText = [NSString stringWithFormat:@"%@ added.", firstName];
                } else {
                    updateText = [NSString stringWithFormat:@"%@ %@ added.", firstName, lastName];
                }

                //NSLog(updateText);
            } else {
                if (firstName==nil) {
                    updateText = [NSString stringWithFormat:@"%@ skipped.", lastName];
                } else if (lastName==nil) {
                    updateText = [NSString stringWithFormat:@"%@ skipped.", firstName];
                } else {
                    updateText = [NSString stringWithFormat:@"%@ %@ skipped.", firstName, lastName];
                }
                //NSLog(updateText);
            }

            updateLabel.text = [updateLabel.text stringByAppendingFormat:@"%@\n", updateText];

            statusLabel.text = @"";

            NSLog(@"%i/%i Imported", recordNumber, totalRecords);


        }

        if (error != NULL) {                    
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not create contact" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
}
//[importLines release];


[activity stopAnimating];
activity.hidden = TRUE;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add To Contacts" message:[NSString stringWithFormat:@"Contacts successfully restored!", ABRecordCopyValue(person, kABPersonFirstNameProperty)] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
 } 
4

0 に答える 0