Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将ABAddressBook中的联系人导入文本文件时出现问题_Ios_Objective C_Abaddressbook - Fatal编程技术网

Ios 将ABAddressBook中的联系人导入文本文件时出现问题

Ios 将ABAddressBook中的联系人导入文本文件时出现问题,ios,objective-c,abaddressbook,Ios,Objective C,Abaddressbook,我使用苹果的通讯录创建了一个简单的联系人应用程序,它有以下代理ABNewPersonViewControllerDelegate、ABPeoplePickerNavigationControllerDelegate和ABPersonViewControllerDelegate,它具有以下功能:创建、编辑和删除联系人。现在我想添加一个选项,通过该选项,我创建的所有联系人以及电话号码都将导入到文本文件中。这是我的代码: -(void)fetchContacts { addressBooks

我使用苹果的通讯录创建了一个简单的联系人应用程序,它有以下代理ABNewPersonViewControllerDelegate、ABPeoplePickerNavigationControllerDelegate和ABPersonViewControllerDelegate,它具有以下功能:创建、编辑和删除联系人。现在我想添加一个选项,通过该选项,我创建的所有联系人以及电话号码都将导入到文本文件中。这是我的代码:

-(void)fetchContacts
{
    addressBooks =ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBooks);
    for (CFIndex i = 0; i < CFArrayGetCount(people); i++)
    {
        person12 = CFArrayGetValueAtIndex(people, i);
        NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonLastNameProperty))!=NULL)
        {
            tweet=[tweet stringByAppendingString:@" "];
            tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonLastNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];

        }
        if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonFirstNameProperty))!=NULL)
        {
            tweet=[tweet stringByAppendingString:@" ---> "];
            tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonPhoneProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        }
        NSLog(@"%@",tweet); 

        NSString* documentPlistPath = [[NSBundle mainBundle] pathForResource:@"Contacts" ofType:@"txt"];
            [tweet writeToFile:documentPlistPath atomically:NO  encoding:NSUTF8StringEncoding error:nil];

        NSLog(@"%@",documentPlistPath);
    }
    CFBridgingRelease(people);

}
-(无效)获取联系人
{
addressBooks=ABAddressBookCreate();
CFArrayRef people=abAddressBookCopyArrayFallPeople(地址簿);
对于(CFIndex i=0;i”;
tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@“%@”,(u bridge_transfer NSString*)ABRecordCopyValue(person12,KabbersonPhoneProperty)]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
NSLog(@“%@”,tweet);
NSString*documentPlistPath=[[NSBundle mainBundle]路径资源:@“txt”类型的“联系人”;
[tweet writeToFile:documentPlistPath原子:无编码:NSUTF8StringEncoding错误:nil];
NSLog(@“%@”,documentPlistPath);
}
释放(人);
}
我现在有两个问题:

1) 在NSLog上,tweet包含所有值,但格式如下:FirstName LastName-->ABMultiValueRef 0x6c26760和1个值 0: $!!$ (0x6c26b60)-电话号码(0x6c26b80)

我需要这种格式的FirstName LastName-->PhoneNumber

2) 未填充Contact.txt文件。NSLog上的路径是正确的。请告诉我这里出了什么问题。

1)有了这段代码的帮助,我能够解决第一个问题

ABMultiValueRef multi = ABRecordCopyValue(person12, kABPersonPhoneProperty);
            if(ABMultiValueGetCount(multi)!=0)
            {
            //For fetching multiple Phone Numbers
            for (int i=0; i < ABMultiValueGetCount(multi); i++) 
                {
            temp = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(multi, i);
                phoneNumbers=[phoneNumbers stringByAppendingString:[NSString stringWithFormat:@"%@/",temp]];
                }
             }
//For reading the file

- (IBAction)readFileAction:(id)sender {


    //Test whether this file exists before we read it
    if([self.fileMgr fileExistsAtPath:self.file])
    {

        NSString *content = [[NSString alloc] initWithContentsOfFile:self.file usedEncoding:nil error:nil];
        [webview loadHTMLString:content baseURL:nil];

    }
    else 
    {
        NSString *content = @"Contact list is not generated yet, click on create to generate the contact list";
        [webview loadHTMLString:content baseURL:nil];
    }
}

- (IBAction)deleteFileAction:(id)sender {

    //Test whether this file exists now that we have tried to remove it
    if([self.fileMgr fileExistsAtPath:self.file])
    {
        // attempt to delete file from documents directory
        [fileMgr removeItemAtPath:self.file error:nil];
        if([self.fileMgr fileExistsAtPath:self.file])
        {

            NSString *content = @"File still exists after trying to remove it";
            [webview loadHTMLString:content baseURL:nil];
        }
        else 
        {
            [webview loadHTMLString:@"" baseURL:nil];
            NSString *content = @"We've successfully removed the contact file";
            [webview loadHTMLString:content baseURL:nil];
        }
    }
    else {

        NSString *content = @"File does not exist no need to remove it.";
        [webview loadHTMLString:content baseURL:nil];
    }
}

- (IBAction)createFileAction:(id)sender {

     // To create file
        [self.fileMgr createFileAtPath:file contents:nil attributes:nil];

        [allContacts writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];


        //Test if file exists now that we have tried creating it
        if([self.fileMgr fileExistsAtPath:self.file])
        {

            NSString *content = @"Contact list generated click on view to show the list";
            [webview loadHTMLString:content baseURL:nil];
        }
        else 
        {

            NSString *content = @"Contact list is not created yet, click on create to generate the contact list";
            [webview loadHTMLString:content baseURL:nil];
        }

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // create the fileManager
    self.fileMgr = [NSFileManager defaultManager];

    // get array of paths
    self.paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    // get documents directory path
    self.documentsDirectory = [paths objectAtIndex:0];

    // create the file name with qualified path ot the file
    self.file = [documentsDirectory stringByAppendingPathComponent:@"TestPage.html"];

}