为什么我可以在iOS模拟器中添加一个组到地址簿,但不是设备?

时间:2015-08-14 00:16:56

标签: ios abaddressbook

我的问题很简单:当我使用模拟器而不是设备时,下面的代码怎么工作?

我在8.2和8.4的部署目标上使用了两个iPad Airs,在8.2上使用了iPad Mini(不知道哪个,但相对较新),并且没有一个能够添加组。< / p>

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    ABAddressBookRef addressBook;
    bool didSave;
    CFErrorRef error = NULL;

    addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    // If you get some error when trying to add a contact, make sure it's not a permission issue

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

        if (granted) {

            [self createNewGroup:@"New Group"];
        }

    });

//    [self createNewGroup:@"New Group"];

}

-(void)createNewGroup:(NSString *)groupName
{
    CFErrorRef error = NULL;
    bool didSet, didAdd, didSave;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    if (error) {
        NSLog(@"Error: %@", error);
    }

    ABRecordRef newGroup = ABGroupCreate();

    didSet = ABRecordSetValue(newGroup, kABGroupNameProperty, (__bridge CFTypeRef)(groupName), &error);
    if (!didSet) { NSLog(@"Unresolved error while setting group: %@", error); }

    didAdd = ABAddressBookAddRecord(addressBook, newGroup, &error);
    if (!didAdd) { NSLog(@"Unresolved error while adding group: %@", error); };

    didSave = ABAddressBookSave(addressBook, &error);
    if (!didSave) { NSLog(@"Unresolved error while saving group: %@", error); };

    ABRecordID groupId = ABRecordGetRecordID(newGroup);
//    CFRelease(addressBook);
//    CFRelease(newGroup);
//    CFRelease(error);

}

1 个答案:

答案 0 :(得分:0)

你应该这样做:

#if TARGET_IPHONE_SIMULATOR
    [self createNewGroup:@"New Group"];
#endif

已经问到:What #defines are set up by Xcode when compiling for iPhone