gpt4 book ai didi

iphone - 导出 iPhone 地址簿数据库的可能方法

转载 作者:搜寻专家 更新时间:2023-10-30 20:11:08 24 4
gpt4 key购买 nike

我想将 iPhone 的 AddressBook.sqlitedb 导出到我的 iPhone 应用程序中。

我在网上搜索过,但似乎所有内容都遍历“ABAddressBook”

但我想知道是否可以通过编程方式将 iPhone 的 AddressBook.sqlitedb 导出到我的 iPhone 应用程序中?

有什么宝贵的意见请告诉我!!!

感谢您的帮助......

最佳答案

您必须获取每个值,然后将其插入到您的数据库中。

这是我将 iphone 通讯录放入应用程序数据库的代码。

只要在你想获取iphone地址簿的地方调用下面的方法,但我建议你在delegat.m方法中调用这个方法——DidFinishLanching:

-(void)fetchRecordsFromAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

//NSArray *addresses = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

//[arrayContacts removeAllObjects];

[self emptyDataContext];

for (int i = 0; i < nPeople; i++)
{


ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);


////////////////// get first name ///////////////////

CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);

CFStringRef nickName = ABRecordCopyValue(ref, kABPersonNicknameProperty);

CFStringRef middleName = ABRecordCopyValue(ref, kABPersonMiddleNameProperty);


////////////////// get image ///////////////////

// ABMultiValueRef ContactImage = (ABMultiValueRef) ABRecordCopyValue(ref,kABPersonImageFormatThumbnail);


NSData *data=nil;

// NSLog(@"Image Testing is : %@",ref);

if(ABPersonHasImageData(ref))
{
data = [(NSData *) ABPersonCopyImageData(ref) autorelease];
if(data)
{
// NSLog(@"Im Testing is : %@",data);

//image = [[UIImage alloc] initWithData:data];
}
}

// NSLog(@"Im agte is : %@",ContactImage);
// NSLog(@" Name is : %@",firstName);



////////////////// get email ///////////////////

ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonEmailProperty);

NSString *emailID=@"";

if(ABMultiValueGetCount(emails)>=1)
{
emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails,0);
}


////////////////// get phone number ///////////////////

ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonPhoneProperty);

NSString *phone=@"";

NSString *homeNumber = @"";

NSString *worknumber = @"";

if(ABMultiValueGetCount(phones)>=1)
{
//int ph = [ABMultiValueCopyValueAtIndex(phones, 0) intValue];
phone = (NSString *)ABMultiValueCopyValueAtIndex(phones,0);
}
// NSLog(@"%@",(NSString*)phone);


if(ABMultiValueGetCount(phones)>=2)
{
homeNumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,1);
}

if(ABMultiValueGetCount(phones)>=3)
{
worknumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,2);
}


NSMutableArray *arrayContacts = [[NSMutableArray alloc] init ];


///////////////////////////// insert into array ////////////////////////////

arrayContacts = [CoreDataAPIMethods getObjectsFromContext:@"AllContactData" :@"Index" :NO :self.managedObjectContext];

//////////////////////////// insert Index ///////////////////////////////
int NewEntryID;

if ([arrayContacts count] > 0)
{
AllContactData * Contacdata = [arrayContacts objectAtIndex:0];

NewEntryID = [Contacdata.Index intValue] +1;

}
else
{
NewEntryID = 1;
}

NSString *capitalisedSentence =
[(NSString *)firstName stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[(NSString *)firstName substringToIndex:1] capitalizedString]];

AllContactData *Contactitem=(AllContactData *)[NSEntityDescription insertNewObjectForEntityForName:@"AllContactData" inManagedObjectContext:self.managedObjectContext];

// NSLog(@"%@",capitalisedSentence);

Contactitem.Name = capitalisedSentence;

Contactitem.LastName = (NSString*)lastName;

Contactitem.NickName = (NSString*)nickName;

Contactitem.MiddleName = (NSString*)middleName;

Contactitem.Email=(NSString*)emailID;

phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@"+" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];

NSLog(@"The Replacing Stinr is : %@", phone);

Contactitem.PhoneNumber=(NSString*)phone;

Contactitem.HomeNumber=(NSString*)homeNumber;

Contactitem.WorkNumber=(NSString*)worknumber;

Contactitem.Index = [NSNumber numberWithInt:NewEntryID];

Contactitem.Image = data;

// NSLog(@"Image in databse is : %@",(NSData *)ContactImage);

if(firstName!=nil)
{
CFRelease(firstName);
}
CFRelease(ref);

}
CFRelease(allPeople);


///////////////////////////// save entries ////////////////////////////

NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error...
}


}



-(void)emptyDataContext
{

self.managedObjectContext = [(Dial_Up_AppAppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];

NSLog(@"managedObjectContext=%@",self.managedObjectContext);
// Get all counties, It's the top level object and the reference cascade deletion downward
NSMutableArray* mutableFetchResults = [CoreDataAPIMethods getObjectsFromContext:@"AllContactData" :@"Name" :YES :self.managedObjectContext];
// Delete all Counties
for (int i = 0; i < [mutableFetchResults count]; i++) {
[managedObjectContext deleteObject:[mutableFetchResults objectAtIndex:i]];
}
//[mutableFetchResults release];
// Update the data model effectivly removing the objects we removed above.
NSError *error;
if([mutableFetchResults count] > 0)
{

if (![managedObjectContext save:&error]) {
// Handle the error.
//NSLog([error domain]);
}

}

}

希望对您有所帮助。

关于iphone - 导出 iPhone 地址簿数据库的可能方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8411442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com