Desktop Getting Started
Last updated 4.15.2009
Set your Facebook application type
- Go to your Facebook application page at http://www.facebook.com/developers/apps.php?app_id= YOUR APP ID
- Click "edit settings"
- Go to the Advanced section and set the Application Type to Desktop
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 MKFacebookObject
withSecret:@"YOUR_SECRET_KEY"
delegate:self] retain];
Display a login window
Implement the userLoginSuccessful delegate method
The userLoginSuccessful method will be called after the user logs in and closes the login window.
{
NSLog(@"neat");
}
Call a Facebook method
Once you've verified a user has logged in you may start calling other Facebook methods.
MKFacebookRequest *request = [MKFacebookRequest requestUsingFacebookConnection:facebookConnection delegate:self];
//set up parameters for request
[parameters setValue:@"facebook.photos.getAlbums" forKey:@"method"];
[parameters setValue:[facebookConnection uid] forKey:@"uid"];
//add parameters to request
[request setParameters:parameters];
//send the request
[request sendRequest];
[parameters release];
Implement our delegate methods so we can do something with the response we get from Facebook.
{
NSArray *albums = [[xml rootElement] arrayFromXMLElement];
//do something with the array of albums
}
-(void)facebookErrorResponseReceived:(NSXMLDocument *)xml
{
//oh crap something went wrong
}
Upload a photo
Uploading a photo is almost identical to sending any other request. The only extra step is adding a NSImage object to the NSDictionary of parameters that is passed to the MKFacebookRequest object.
MKFacebookRequest *photoRequest = [MKFacebookRequest requestUsingFacebookConnection:facebookConnection delegate:self];
//we want to handle responses from photo uploads differently so we'll override the default delegate method
[photoRequest setSelector:@selector(uploadedPhoto:)];
[parameters setValue:@"facebook.photos.upload" forKey:@"method"];
//add a NSImage object to the parameters dictionary, key name can be anything
[parameters setObject:<some NSImage object here> forKey:@"image"];
[parameters setValue:@"123456789" forKey:@"aid"];
[parameters setValue:@"Neat." forKey:@"caption"];
[photosRequest setParameters:parameters];
[photoRequest sendRequest];
[parameters release];
Implement our own method for handling photo upload responses.
{
//do something with the response
}
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.
MKFacebookRequestQueue *requestQueue = [[MKFacebookRequestQueue alloc] init];
[requestQueue setDelegate:self];
/*currentlyUploadingImage: will receive a NSDictionary with a "current" key and a "total" key
to indicate which image is currently being uploaded*/
[requestQueue setCurrentlySendingSelector:@selector(currentlyUploadingImage:)];
/*lastImageUploaded will receive the NSXMLDocument containing the response from
Facebook from the most recent image upload*/
[requestQueue setLastRequestResponseSelector:@selector(lastImageUploaded:)];
//allImagesUploaded will be called after the queue has attempted all requests
[requestQueue setAllRequestsFinishedSelector:@selector(allImagesUploaded)];
//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];
//handle responses in appropriate selectors...
Add observers for notifications.
You can add observers to be notified when requests start and end.
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(facebookNetworkActivityStarted:)
name:@"MKFacebookRequestActivityStarted"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(facebookNetworkActivityEnded:)
name:@"MKFacebookRequestActivityEnded"
object:nil];
}