forked from erica/ABContactHelper
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathABStandin.m
More file actions
executable file
·86 lines (77 loc) · 2 KB
/
ABStandin.m
File metadata and controls
executable file
·86 lines (77 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// AddressBook.m
// HelloWorld
//
// Created by Erica Sadun on 8/24/11.
// Copyright (c) 2011 Up To No Good, Inc. All rights reserved.
//
#import "ABStandin.h"
static ABAddressBookRef shared = NULL;
@implementation ABStandin
// Return the current shared address book,
// Creating if needed
+ (ABAddressBookRef) addressBook
{
@synchronized (self) {
if (!shared) {
if (ABAddressBookCreateWithOptions == NULL) {
shared = ABAddressBookCreate();
} else {
shared = ABAddressBookCreateWithOptions(NULL, NULL);
}
if (![self hasAddressBookAccess:shared]) {
//TODO: show beautiful alert
shared = NULL;
}
}
}
return shared;
}
// Load the current address book
+ (ABAddressBookRef) currentAddressBook
{
if (shared)
{
CFRelease(shared);
shared = nil;
}
return [self addressBook];
}
// Thanks Frederic Bronner
// Save the address book out
+ (BOOL) save: (NSError **) error
{
CFErrorRef cfError;
if (shared)
{
BOOL success = ABAddressBookSave(shared, &cfError);
if (!success)
{
if (error)
*error = (__bridge_transfer NSError *)cfError;
return NO;
}
return YES;
}
return NO;
}
+ (BOOL)hasAddressBookAccess:(ABAddressBookRef)addressBook {
if (ABAddressBookRequestAccessWithCompletion == NULL) {
// before iOS 6
return YES;
}
// iOS 6 and more
__block BOOL accessGranted = NO;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
if(semaphore) {
dispatch_semaphore_signal(semaphore);
}
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
semaphore = NULL;
return accessGranted;
}
@end