我的联系人没有在ios 7中添加地址簿 [英] my contact not add in addressbook in ios 7

查看:60
本文介绍了我的联系人没有在ios 7中添加地址簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的初学者;此代码在iOS 6中有效但在iOS 7中无效...
我的代码如下,我想在我的地址簿中添加联系人。我正在开发一个应用程序,我已经通过了许多链接,我有以下代码,但现在我被卡住了...

I am a beginner in iOS development; this code is working in iOS 6 but does not work in iOS 7 ... My code is below, I want to add contacts into my address book. I am developing an app where I have gone through many links, and I have following code, but now I am stuck...

我有导入:

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

ViewController.m file

ABAddressBookRef ab = ABAddressBookCreate();
    // To add a new ab entry with telephone number
    ABRecordRef newPerson = ABPersonCreate();

    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef) nameFirststr, nil);

    ABRecordSetValue(newPerson, kABPersonLastNameProperty, (__bridge CFStringRef)@"Jones", nil);

    //phone
    ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFStringRef)myphone,kABPersonPhoneMobileLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty,  phoneNumberMultiValue, nil);

    // Adreess
    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (Address)
    {
        if (Address)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", Address, Address];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = Address;

        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressCityKey] = @"city";
        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressStateKey] = @"city";
        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressZIPKey] = @"city";
        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressCountryKey] = @"city";
    }


    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);

    ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress, nil);

    // email

    if (emailstr)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) emailstr, kABWorkLabel, NULL);

        ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    }
    if (Organization)
    {
        ABRecordSetValue(newPerson, kABPersonOrganizationProperty, (__bridge CFStringRef)Organization, nil);

    }
    if (title)
    {
        ABRecordSetValue(newPerson, kABPersonJobTitleProperty, (__bridge CFStringRef)title, nil);

    }
    if (notes)
    {
        ABRecordSetValue(newPerson, kABPersonNoteProperty, (__bridge CFStringRef)notes, nil);
    }


    if (webUrl)
    {

        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webUrl, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
    }

    ABAddressBookAddRecord(ab, newPerson, nil);
    ABAddressBookSave(ab,NULL);
    if (ABAddressBookSave(ab, nil)) {
        NSLog(@"\nPerson Saved successfuly");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Your contact sucessfully Add" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
    } else {
        NSLog(@"\n Error Saving person to AddressBook");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error...!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
    }


推荐答案

检查是否允许使用地址簿

check if addressbook is allowed

    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        // First time access has been granted, add the contact
        [self addContact];
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
    [self addContact];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access Denied" message:@"Please give access to AddressBook via settings." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
}

也替换第一行

    CFErrorRef * error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

这篇关于我的联系人没有在ios 7中添加地址簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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