Last updated 3.20.2010
Set your Facebook application type
Import MKAbeFook into your XCode project
- Control + click the Frameworks group in XCode and select Add-Existing Framework. Navigate to MKAbeFook.framework and click Add.
- Click Add again to add it to selected targets.
- Control + click your project in the Targets group. Select Add->New Copy Files Build Phase.
- Choose Frameworks as the destination. Close the window.
- Drag MKAbeFook.framework from the Frameworks group to your new Copy Files Build Phase folder.
Import <MKAbeFook/MKAbeFook.h> where appropriate.
Create a new MKFacebook object
MKFacebook *fbConnection = [[MKFacebook facebookWithAPIKey:@"YOUR_KEY" delegate:self] retain];
Display a login window
[fbConnection login];
Implement the userLoginSuccessful delegate method
The userLoginSuccessful method will be called after the user logs in and closes the login window.
-(void)userLoginSuccessful
{
NSLog(@"neat");
}
Call a Facebook method
Once you've verified a user has logged in you may start calling other Facebook methods.
NSMutableDictionary *parameters
= [[[NSMutableDictionary alloc
] init
] autorelease
];
MKFacebookRequest
*request
= [MKFacebookRequest requestWithDelegate
:self
];
//set up parameters for request
[parameters setValue
:[NSArray arrayWithObjects
:[fbConnection uid
],
@"123456789",
nil] forKey
:@"uids"];
[parameters setValue
:[NSArray arrayWithObjects
:@"first_name",
@"last_name",
nil] forkey
:@"fields"];
//send the request
[request sendRequest
:@"users.getInfo" withParameters
:parameters
];
Implement the delegate methods so we can do something with the response we get from Facebook.
- (void)facebookRequest
:(MKFacebookRequest
*)request responseReceived
:(id)response
{
//do something with the response
}
- (void)facebookRequest
:(MKFacebookRequest
*)request errorReceived
:(MKFacebookResponseError
*)error
{
//oh crap something went wrong, do something with the error
}
- (void)facebookRequest
:(MKFacebookRequest
*)request failed
:(NSError *)error
{
//something went very wrong..
}
Send multiple requests using a queue
The MKFacebookRequestQueue class can be used to send multiple requests that wait for the previous request to finish before sending a new one. Simply set up individual MKFacebookRequests and add them to a MKFacebookRequestQueue.
//create a MKFacebookRequest instance variable
MKFacebookRequestQueue *requestQueue = [[MKFacebookRequestQueue alloc] init];
[requestQueue setDelegate:self];
//here we set up however many MKFacebookRequest objects we want and add them to the queue
[requestQueue addRequest:request1];
[requestQueue addRequest:request2];
[requestQueue addRequest:request3];
//start processing requests in the queue
[requestQueue startRequestQueue];
//receive information about the queue using the MKFacebookRequestQueueDelegate methods
- (void)requestQueue:(MKFacebookRequestQueue *)queue activeRequest:(NSUInteger)index ofRequests:(NSUInteger)total{
NSLog(@"Request %i of %i was sent", index+1, total);
}
- (void)requestQueueDidFinish:(MKFacebookRequestQueue *)queue{
//all requests have been sent
}
Add observers for notifications
You can add observers to be notified when requests start and end.
-(void)awakeFromNib
{
[[NSNotificationCenter defaultCenter
] addObserver
:self
selector
:@selector(facebookNetworkActivityStarted
:)
name
:@"MKFacebookRequestActivityStarted"
object
:nil];
[[NSNotificationCenter defaultCenter
] addObserver
:self
selector
:@selector(facebookNetworkActivityEnded
:)
name
:@"MKFacebookRequestActivityEnded"
object
:nil];
}