forked from bolav/fuse-contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactsImpl.iOS.uno
More file actions
192 lines (164 loc) · 7.4 KB
/
ContactsImpl.iOS.uno
File metadata and controls
192 lines (164 loc) · 7.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Uno;
using Fuse;
using Bolav.ForeignHelpers;
using Uno.Compiler.ExportTargetInterop;
using Uno.Threading;
[Require("Source.Import","AddressBook/AddressBook.h")]
[Require("Source.Include", "@{ForeignDict:Include}")]
[Require("Xcode.Framework", "AddressBook")]
public extern(iOS) class ContactsImpl
{
public static Future<string> AuthorizeImpl()
{
var p = new Promise<string>();
var status = GetAuthorizationStatus();
if (status == "AuthorizationNotDetermined") {
var closure = new AuthorizationClosure(p);
closure.RequestAuthorization();
return p;
}
p.Resolve(status);
return p;
}
[Foreign(Language.ObjC)]
public static string GetAuthorizationStatus()
@{
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied) {
return @"AuthorizationDenied";
}
else if (status == kABAuthorizationStatusRestricted) {
return @"AuthorizationRestricted";
}
else if (status == kABAuthorizationStatusNotDetermined) {
return @"AuthorizationNotDetermined";
}
else if (status == kABAuthorizationStatusAuthorized) {
return @"AuthorizationAuthorized";
}
else {
return @"Unknown";
}
@}
// http://stackoverflow.com/questions/3747844/get-a-list-of-all-contacts-on-ios
[Foreign(Language.ObjC)]
public static void GetAllImpl(ForeignList ret)
@{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
// CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
CFIndex nPeople = CFArrayGetCount(allPeople);
for ( int i = 0; i < nPeople; i++ )
{
id<UnoObject> row = @{ForeignList:Of(ret).NewDictRow():Call()};
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
// https://developer.apple.com/library/ios/documentation/AddressBook/Reference/ABRecordRef_iPhoneOS/
@{ForeignDict:Of(row).SetKeyVal(string,string):Call(@"firstName", CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)))};
@{ForeignDict:Of(row).SetKeyVal(string,string):Call(@"lastName", CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)))};
@{ForeignDict:Of(row).SetKeyVal(string,string):Call(@"organization", CFBridgingRelease(ABRecordCopyValue(person, kABPersonOrganizationProperty)))};
id<UnoObject> emailList = @{ForeignDict:Of(row).AddListForKey(string):Call(@"email")};
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(emails); i++) {
id<UnoObject> emailRow = @{ForeignList:Of(emailList).NewDictRow():Call()};
NSString *label = (__bridge NSString *) ABMultiValueCopyLabelAtIndex(emails, i);
NSString *email = (__bridge NSString *) ABMultiValueCopyValueAtIndex(emails, i);
@{ForeignDict:Of(emailRow).SetKeyVal(string,string):Call(@"email", email)};
@{ForeignDict:Of(emailRow).SetKeyVal(string,string):Call(@"label", label)};
}
CFRelease(emails);
id<UnoObject> phoneList = @{ForeignDict:Of(row).AddListForKey(string):Call(@"phone")};
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
id<UnoObject> phoneRow = @{ForeignList:Of(phoneList).NewDictRow():Call()};
NSString *label = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
@{ForeignDict:Of(phoneRow).SetKeyVal(string,string):Call(@"phone", phone)};
@{ForeignDict:Of(phoneRow).SetKeyVal(string,string):Call(@"label", label)};
}
CFRelease(phones);
}
CFRelease(allPeople);
@}
[Foreign(Language.ObjC)]
public static void GetPageImpl(ForeignList ret, int numRows, int curPage)
@{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = CFArrayGetCount(allPeople);
int i = curPage * numRows;
int j = i + numRows;
while ( i < j )
{
i++;
if (i >= nPeople) break;
id<UnoObject> row = @{ForeignList:Of(ret).NewDictRow():Call()};
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *fullName = @"";
if (!firstName && !lastName) {
fullName = @"Unknown Contact";
} else if (!lastName) {
fullName = [NSString stringWithFormat:@"%@", firstName];
} else if (!firstName) {
fullName = [NSString stringWithFormat:@"%@", lastName];
} else {
fullName = [NSString stringWithFormat:@"%@%@%@", firstName, @" ", lastName];
}
@{ForeignDict:Of(row).SetKeyVal(string,string):Call(@"name", fullName)};
id<UnoObject> emailList = @{ForeignDict:Of(row).AddListForKey(string):Call(@"email")};
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(emails); i++) {
id<UnoObject> emailRow = @{ForeignList:Of(emailList).NewDictRow():Call()};
NSString *email = (__bridge NSString *) ABMultiValueCopyValueAtIndex(emails, i);
@{ForeignDict:Of(emailRow).SetKeyVal(string,string):Call(@"email", email)};
}
CFRelease(emails);
id<UnoObject> phoneList = @{ForeignDict:Of(row).AddListForKey(string):Call(@"phone")};
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
id<UnoObject> phoneRow = @{ForeignList:Of(phoneList).NewDictRow():Call()};
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
@{ForeignDict:Of(phoneRow).SetKeyVal(string,string):Call(@"phone", phone)};
}
CFRelease(phones);
}
CFRelease(allPeople);
@}
}
[Require("Source.Import","AddressBook/AddressBook.h")]
public extern(iOS) class AuthorizationClosure {
Promise<string> promise;
public AuthorizationClosure(Promise<string> p) {
promise = p;
}
public void Resolve(string s) {
promise.Resolve(s);
}
[Foreign(Language.ObjC)]
public void RequestAuthorization()
@{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
@{AuthorizationClosure:Of(_this).Resolve(string):Call(@"Error getting addressBook")};
return;
}
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
@{AuthorizationClosure:Of(_this).Resolve(string):Call(@"Error getting access")};
}
if (granted) {
@{AuthorizationClosure:Of(_this).Resolve(string):Call(@"AuthorizationAuthorized")};
} else {
@{AuthorizationClosure:Of(_this).Resolve(string):Call(@"AuthorizationDenied")};
}
CFRelease(addressBook);
});
@}
}