Facebook iOS: Use Access Token in PHP

Yes that's right. As of the time of this writing, it is possible to use the user granted access_token back in some server side scripting language like PHP. If you currently have a 255 varchar field size for access_tokens in your database, you may want to bump that up a bit.

The time I made the sample code ran and allowed me to log in (one that came bundled with the Facebook iOS SDK from GitHub), I began realizing that the token quite have different format than the one we usually see in web-based apps (e.g. FB iFrame app).

The usual token looks something like this:

213455681425|1.BGgrgnfWrdpG_X18.3600.1213252135.2-1334679|dHcDbxGbeYbLg3SRgw12fdf4gd60

..while in mobile:

v9ylvkttPnuFWUX4KVdjDPB0SRXkuKX7z281rqjHuG0.eyJpdiI6ImEwWXBDaEtncWpDTU5ibUNuQWdROWcifQ.Y-DwxRY2ZAFZiP7EVuR-HksXqmGw9LXP6umGrfz2XnjSLm0a508u7_jXq0_Kz5a2S8AUUulzUvIRVxTS51_i6VfSByOCbFBIKoBe0-n-Pa8NC29wbuVmGJLvq4W-ezhv0DzA3diiCIqCybt9ELDXoA

The plan was, allow users to connect to our application using FB. Then once he approved it, we take a copy of the access_token and save it back to the server. Same access_token will be used once he logs in to his account using our web-based app.

Well, so far so good. Let me know if yours doesn't work.


Related posts:

Facebook iOS SDK: Let users Connect & Login with FB

Here's how to let your users connect and login to your iOS app using Facebook. The first thing you got to do is download the Facebook iOS SDK. If you have Git installed, you can also do this by pulling from GitHub.

git clone git://github.com/facebook/facebook-ios-sdk.git

Objective-C: Retain, Release, Autorelease

The rules for -retain, -release, and -autorelease are simple. If you alloc, copy, or retain something, you also need to (eventually) release or autorelease it. If you don't do any of those three things, you don't need to do any memory management at all. Not only don't need to, if you try, it messes things up.

If you alloc something, it's just like you sent -retain to the object. So you release or autorelease it. If you do a copy or a mutableCopy, it's also the same as sending a retain. So you also release or autorelease it.

If you never sent a retain (or equivalent) method to an object, then by default it is autoreleased and will go away on its own. The following example doesn't need to be released explicitly since it has already been given an -autorelease message internally. (see NSArray Class Reference -> arrayWithObjects:)
NSArray * sampleList = [NSArray arrayWithObjects:@"One", @"Two", nil];

Also, in my code I always try to release an object instead of autoreleasing it as much as possible.

The actual mechanism here is that when you call -autorelease, a pointer to the object is added to a list kept by an NSAutoreleasePool. When that pool is deallocated, every object in the list gets a -release method. Thus -autorelease is a delayed -release.

Subclassing UIView with NIB file

Here's how UIView subclassing is done (with NIB files). I felt the need of doing so for two reasons: reusability and maintainability.

Header class:
#import <UIKit/UIKit.h>

@interface MyCustomView: UIView

+ (MyCustomView *)createWithOwner:(id)owner;

@end

Implementation class:
#import "MyCustomView.h"

@implementation MyCustomView

+ (MyCustomView *)createWithOwner:(id)owner {
  NSString *nibName = NSStringFromClass([self class]);
  NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];
  MyCustomView *view = [nib objectAtIndex:0];
  return view;
}

@end

Using the the new class is as simple as this:
#import "MyCustomView.h"

...

MyCustomView * customView = [MyCustomView createWithOwner:self];
[self.view addSubView: customView];
[customView release];  

...

Related posts:

Bookmark and Share

Determine if UITableView is near bottom when scrolling

Here's my way of finding out if UITableView is near bottom when scrolling. And when it does, we fetch and load another batch of items.

Facebook `comments.remove` API Method

With FB's Comment Social plugin, having to remove comments on a certain Application is a click away as long as the current user has the Admin rights. This thing right here may be necessary to those who wanted a custom Administration sort of utility for managing FB App related comments.

Here's a sample code of how one could implement this method: