2013-08-06 2 views
0

저는 Apple의 주소록과 다음 대리인 인 ABNewPersonViewControllerDelegate, ABPeoplePickerNavigationControllerDelegate 및 ABPersonViewControllerDelegate를 사용하여 간단한 연락처 응용 프로그램을 만들었습니다. 연락처 만들기, 편집 및 삭제 기능이 있습니다. 이제 작성한 모든 연락처와 전화 번호를 텍스트 파일로 가져 오는 옵션을 추가하고 싶습니다. 나는이 여기에 지금 문제가ABAddressBook에서 연락처를 텍스트 파일로 가져 오는 데 문제가 있습니다.

-(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); 

} 

: 이것은 내 코드입니다

1) NSLog에를, 트윗 모든 값을 포함하지만 같은 형식을 다음 : 1 개 값 이름 성 ---> ABMultiValueRef 0x6c26760 (들) 0 : 은 $! $ (0x6c26b60가) - PHONENUMBER은 (0x6c26b80)

내가이 형식에 필요한 이름 성 ---> PHONENUMBER

2) Contact.txt 파일이 아닙니다 ~을 얻다 채워진다. NSLog 경로에 맞습니다. 제발 조언을 여기에 뭐가 잘못.

답변

0

1) 내가 NSFile 관리자 사용이 문제에 대한 코드

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]]; 
       } 
      } 

2)의 도움으로 상기 제 문제를 해결할 수, 나 UIWebView에서의 출력 부호 비트 이상의 개질. 다른 사람이 필요로 할 경우에 대비하여 다음과 같은 주요 기능이 있습니다.

//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"]; 

}