Forked from jcayzac/ContactDiscovery.partial.mm
Last active
January 20, 2016 16:54
-
-
Save Bodacious/b7909140598e16ec8111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <AddressBook/AddressBook.h> | |
#import <Accounts/Accounts.h> | |
#import <Social/Social.h> | |
@interface ContactsDiscovery() | |
@property (nonatomic, retain) ACAccountStore* accounts; | |
@property (nonatomic, retain) ACAccountType* faceBookAccountType; | |
@property (nonatomic, retain) ACAccount* faceBookAccount; | |
@property (nonatomic, retain) id addressBook; | |
@end | |
-(void)dealloc | |
{ | |
self.accounts = nil; | |
self.faceBookAccountType = nil; | |
self.faceBookAccount = nil; | |
[super dealloc]; | |
} | |
// Request access to Facebook account | |
-(void)requestFacebookAccessWithBlock:(void (^)(bool))block | |
{ | |
self.accounts = [[[ACAccountStore alloc] init] autorelease]; | |
self.faceBookAccountType = [accounts accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; | |
NSDictionary* options = @{ | |
@"ACFacebookAppIdKey" : @"XXXXXXXXXXXXX", | |
@"ACFacebookPermissionsKey" : @[@"email"], // must contain some basic profile permission | |
@"ACFacebookAudienceKey" : ACFacebookAudienceFriends | |
}; | |
[accounts requestAccessToAccountsWithType:faceBook options:options completion:^(BOOL granted, NSError* error){ | |
if (!granted) { | |
NSLog(@"Access to Facebook account not granted. Reason: %@", error); | |
block(false); | |
return; | |
} | |
self.faceBookAccount = [[self.accounts accountsWithAccountType:self.faceBookAccountType] lastObject]; | |
block(self.faceBookAccount); | |
}]; | |
} | |
// Perform a Facebook Graph API request | |
-(void)performFacebookRequestWithMethod:(NSString*)method | |
url:(NSURL*)url | |
parameters:(NSDictionary*)parameters | |
block:(void (^)(NSData*, NSHTTPURLResponse*, NSError*))block | |
{ | |
if (!self.faceBookAccount) { | |
block(nil, nil, [NSError errorWithDomain:@"Facebook" | |
code:123 | |
userInfo:@{ | |
NSLocalizedDescriptionKey: @"No Facebook account" | |
}]); | |
return; | |
} | |
NSInteger slMethod; | |
if ([method isEqualToString:@"GET"]) slMethod = SLRequestMethodGET; else | |
if ([method isEqualToString:@"POST"]) slMethod = SLRequestMethodPOST; else | |
if ([method isEqualToString:@"DELETE"]) slMethod = SLRequestMethodDELETE; | |
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook | |
requestMethod:slMethod | |
URL:url | |
parameters:parameters]; | |
[request setAccount:self.faceBookAccount]; | |
[request performRequestWithHandler:block]; | |
} | |
// Request access to address book | |
-(void)requestAddressBookAccessWithBlock:(void (^)(bool))block | |
{ | |
if (&ABAddressBookCreateWithOptions) { | |
// iOS >=6 | |
CFErrorRef errorRef; | |
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &errorRef); | |
if (addressBookRef) { | |
self.addressBook = [(id) addressBookRef autorelease]; | |
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { | |
if (!granted) { | |
NSLog(@"Access to address book not granted. Reason: %@", (NSError*)error); | |
} | |
block(granted); | |
}); | |
} else { | |
NSLog(@"Could not create address book. Reason: %@", (NSError*)error); | |
block(false); | |
} | |
} else { | |
// iOS <6 | |
ABAddressBookRef addressBookRef = ABAddressBookCreate(); | |
if (addressBookRef) { | |
self.addressBook = [(id) addressBookRef autorelease]; | |
block(true); | |
} else { | |
NSLog(@"Could not create address book."); | |
block(false); | |
} | |
} | |
} | |
@end | |
// Reading the address book: | |
NSArray* contacts = [(NSArray*)ABAddressBookCopyArrayOfAllPeople((ABAddressBookRef) contactDiscovery.addressBook) autorelease]; | |
for (NSUInteger index = 0; index < [contacts count]; ++index) { | |
ABRecordRef recordRef = (ABRecordRef)[contacts objectAtIndex:index]; | |
// If access to the Facebook account was granted, this will contain the facebook username | |
ABMultiValueRef im = ABRecordCopyValue(recordRef, kABPersonInstantMessageProperty); | |
if (im) { | |
[(id)im autorelease]; | |
long count = ABMultiValueGetCount(im); | |
while (--count >= 0) { | |
NSDictionary* value = ABMultiValueCopyValueAtIndex(im, count); | |
if (![value isKindOfClass:[NSDictionary class]]) continue; | |
NSLog(@"Service: %@", value[kABPersonInstantMessageServiceKey]); // Facebook | |
NSLog(@"Username: %@", value[kABPersonInstantMessageUsernameKey]); // foo.bar.412 | |
} | |
} | |
ABMultiValueRef emailAddresses = ABRecordCopyValue(recordRef, kABPersonEmailProperty); | |
if (emailAddresses) { | |
[(id)emailAddresses autorelease]; | |
long count = ABMultiValueGetCount(im); | |
while (--count >= 0) { | |
NSString* email = [(NSString*)ABMultiValueCopyValueAtIndex(emailAddresses, count) autorelease]; | |
NSLog(@"E-Mail: %@", email); | |
} | |
} | |
ABMultiValueRef phoneNumbers = ABRecordCopyValue(recordRef, kABPersonPhoneProperty); | |
if (phoneNumbers) { | |
[(id)phoneNumbers autorelease]; | |
long count = ABMultiValueGetCount(im); | |
while (--count >= 0) { | |
NSString* phoneNumber = [(NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, count) autorelease]; | |
NSLog(@"Phone Number: %@", phoneNumber); | |
} | |
} | |
} | |
// Querying Facebook friends' ids through the Graph API (untested): | |
NSString* method = @"GET"; | |
NSURL* url = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"]; | |
NSDictionary* parameters = @{@"limit": @5000}; | |
void (^block)(NSData*, NSHTTPURLResponse*, NSError*) = ^(NSData* data, NSHTTPURLResponse* response, NSError* error) { | |
if (error || !data) { | |
// NOT GOOD | |
return; | |
} | |
NSError* parsingError = nil; | |
NSDictionary* responseDictionary = [NSJSONSerialization JSONObjectWithData:data | |
options:kNilOptions | |
error:&error]; | |
if (!responseDictionary || parsingError) { | |
// NOT GOOD | |
return; | |
} | |
// Friends' names and ids are in responseDictionary | |
}; | |
[contactDiscovery performFacebookRequestWithMethod:method | |
url:url | |
parameters:parameters | |
block:block]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment