-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddressBookHelper.m
More file actions
96 lines (66 loc) · 3.13 KB
/
AddressBookHelper.m
File metadata and controls
96 lines (66 loc) · 3.13 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
87
88
89
90
91
92
93
94
//
// AddressBookHelper.m
//
//
// Created by Emerson Jose on 7/5/15.
// Copyright (c) 2015 DevSouza Mobile. All rights reserved.
//
#import "AddressBookHelper.h"
@implementation AddressBookHelper
-(void)allContact:(void(^)(id response))callBack
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil, nil);
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
NSMutableArray *contacts = [NSMutableArray new];
for (id record in allContacts){
ABRecordRef thisContact = (__bridge ABRecordRef)record;
ABMultiValueRef phones = ABRecordCopyValue(thisContact, kABPersonPhoneProperty);
NSString *firstName = (__bridge NSString*)ABRecordCopyValue(thisContact, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString*)ABRecordCopyValue(thisContact, kABPersonLastNameProperty);
NSString *email =(__bridge NSString*)ABRecordCopyValue(thisContact, kABPersonEmailProperty);
NSString* phone;
for(CFIndex i = 0; i <= ABMultiValueGetCount(phones); i++) {
NSString *phoneLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
}
else if (phoneLabel != nil)
{
phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
break;
}
}
if([[NSString stringWithFormat:@"%@",email] rangeOfString:@"ABMultiValueRef"].length > 0)
{
email = @"";
}
[contacts addObject:@{@"name" : [NSString stringWithFormat:@"%@ %@",firstName,lastName == nil ? @"": lastName], @"email": email, @"phone" : phone == nil ? @"": phone}];
}
callBack(contacts);
}
-(void)checkPermission:(void(^)(id response))callback
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil, nil);
AddressBookPermission *permissionResponse = [AddressBookPermission new];
permissionResponse.ReturnType = ADDRESS_SUCCESS;
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
//wait for user permission delay (10sec )
dispatch_async(dispatch_get_main_queue(), ^{
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (!granted)
//fail because no have permission to access.
permissionResponse.ReturnType = ADDRESS_NO_AUTORIZED;
});
});
}
else if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {
//faile because disabled contact permission of App.
permissionResponse.ReturnType = ADDRESS_PERMISSION_DISABLED;
}
callback(permissionResponse);
}
@end
#pragma mark - AdressBookResponse
@implementation AddressBookPermission
@end