导出IPhone地址簿db的可能方法 [英] Possible ways to export IPhone addressbook db

查看:103
本文介绍了导出IPhone地址簿db的可能方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to export IPhone's AddressBook.sqlitedb into my IPhone application.

我在网上搜索但是一切似乎都在迭代ABAddressBook

I have searched around the net but everything seems to iterate over the "ABAddressBook"

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

but i want to know that is it possible to export IPhone's AddressBook.sqlitedb into my IPhone application programmatically?

请告诉我任何有价值的评论!!!

Please let me know any valuable comments!!!

感谢您的帮助.....

Thanks for your help.....

推荐答案

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

you have to fetch every single value and then insert it into your DB.

这是我将iphone地址簿输入我的应用程序的Db的代码。

Here is the code through which i got iphone address book into my application's Db.

只需拨打以下电话方法你想要获得iphone地址簿,但我建议你在delegat.m方法中调用这个方法 - DidFinishLanching:

just call the below method where ever you want to get iphone Address Book but i'll suggest you to call this methos in delegat.m method- 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]);
    }

}

}



<希望这会对你有所帮助。

Hope this will help you.

这篇关于导出IPhone地址簿db的可能方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆