0

My question is very simple: how come the code below works when I use the simulator, but not the device?

I've used two iPad Airs on a deployment target of 8.2 and 8.4, and an iPad Mini (don't know which one, but relatively new) on 8.2, and none of them are able to add a group.

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

}
NYC Tech Engineer
  • 1,845
  • 2
  • 22
  • 23

1 Answers1

0

You should do:

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

Already asked in: What #defines are set up by Xcode when compiling for iPhone

Community
  • 1
  • 1
Bannings
  • 10,376
  • 7
  • 44
  • 54
  • I'm sorry, but I don't think this answers my question. – NYC Tech Engineer Aug 14 '15 at 01:38
  • If I understood your question, you want to add a group on the simulator, but not add anything on the real device? – Bannings Aug 14 '15 at 01:46
  • Not exactly, I have no problem adding a group to the address book when I use the simulator, but doing the same thing on the device is a completely different story. I just want to understand why that is... – NYC Tech Engineer Aug 14 '15 at 14:24
  • I think I can understand now. You also need to add a group on the real device, right? You code is working for me. Did you get any error? – Bannings Aug 14 '15 at 14:48
  • My code worked for you when you tried it out on the device? May I ask which device, which version of the SDK, etc? – NYC Tech Engineer Aug 14 '15 at 15:14
  • I'm using iPhone 6 Plus iOS 8.4. – Bannings Aug 14 '15 at 15:14
  • Hmmm, I've only tried iPads, so I'll try it on my iPhone 6 8.4, will let you know if it works. But, isn't it weird that it doesn't work on my end? And it doesn't provide an error? – NYC Tech Engineer Aug 14 '15 at 15:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87009/discussion-between-bannings-and-nyc-tech-engineer). – Bannings Aug 14 '15 at 15:22
  • I'm experiencing this exact same problem too. @NYCTechEngineer were you ever able to find a solution to this problem? My actual device is an iPhone 6 on 9.3.2 – Thomas Jun 05 '16 at 05:06