Game Development Reference
In-Depth Information
Listing 9-7. facebookButtonClicked.m (RootViewController.m)
- (IBAction)facebookButtonClicked:(id)sender {
accountStore = [[ACAccountStore alloc] init];
ACAccountType* facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
NSDictionary *dict = @{
ACFacebookAppIdKey: FB_APP_ID,
ACFacebookPermissionsKey: @[@"publish_stream"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:facebookAccountType
if (granted){
NSArray* accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
} else {
NSLog(@"Could not get permission to access FB: %@", error);
}
}];
}
In Listing 9-7, we start by creating an instance of
ACAccountStore
; note that we are storing this
object in a member field of the
RootViewController
. Next, we create an
ACAccountType
called
facebookAccountType
using the
accountTypeWithAccountIdentifier
: task of
accountStore
. The
object
facebookAccountType
will be used in subsequent calls to
accountStore
to indicate which
social service we are trying to work with. The next step is to create an
NSDictionary
that contains
details about how we wish to work with Facebook. The first item in the
NSDictionary
is the
Facebook App ID specified by the key
ACFacebookAppIdKey
. This is the key we created when we
created a Facebook App in the previous section.
The second key,
ACFacebookPermissionsKey
, specifies which Facebook actions we are asking to
perform. In this case, we just want to be able to write on the user's timeline, which is controlled by
the permission
publish_stream
. (More information about Facebook permissions can be found at
able to see any posts we create. In this case,
ACFacebookAudienceFriends
indicates that only friends
of the user will be able to see the great posts by Belt Commander.
At this point, we are ready to make a call to Facebook and ask if it is OK for us to make posts.
We do this by calling
requestAccessToAccountsWithType:options:completion:
, passing in
facebookAccountType
,
dict
, and a block which is called on completion of the call.
In the completion block, we check to see if
granted
is true; if so, we ask
accountStore
for all
accounts that are Facebook accounts. In iOS 6, a user can have only one Facebook account