Sourabh Shroff
This user hasn't shared any biographical information
Homepage: http://mobisoftinfotech.com/
Posts by Sourabh Shroff
Avatar : The Mobisoft Version
Mar 6th
Yesterday, it was Holi, the festival of colors. Since morning, the streets outside our company were filled with students of Jog School playing holi. At Mobisoft, everyone was busy coding. At around 6 in the evening, the atmosphere inside the company changed. We began seeing specks of orange, yellow, violet and green rising in the air and within minutes, everybody in the company began smearing each other with color. People from Omesa (our friendly neighbours), also joined in. And the result? Yes,there was complete chaos as we celebrated Holi, and here are the pics (Try and recognize some if you can……..)
iPhone FBConnect: Facebook Connect Tutorial
Feb 20th
Please note that this tutorial is now obsolete. Please use facebook’s official tutorial
Now a days social networking websites like facebook are becoming very popular, so integrating facebook with app has become a necessity to make you application popular. We are going to do the same through this tutorial. The Facebook Connect SDK provides code which third-party developers can embed into their applications to connect to their Facebook accounts and exchange information with iPhone apps. It’s a way of embedding “social context” to an iPhone app, according to Facebook.
Create a Viewbased Application with name ‘FacebookAPI’.
Prerequisite:
1.Download Facebook Connect for iPhone SDK (http://svn.facebook.com/svnroot/platform/clients/packages/fbconnect-iphone.zip) or you can download same from here
Just go through the project. In particular, the “Connect” sample project. Sample Project gives demo of some of the functionality.
1.1.Open src/FBConnect.xcodeproj from SDK that you downloaded, and your own project as well.
1.2.Drag n drop FBConnect group. Make sure “Copy items into destination group folder” is NOT checked. It should look as shown below
1.3.Go to Project Menu ->Edit project settings and scroll down to “User Header Search Path” add entry which will point to “src folder”
1.4.To test import all .m n .h files in case any miss. And compile.
2.Login to Facebook. After that go to Developers Page (http://www.facebook.com/developers/) as shown below.

3.Register your application with Facebook
3.1.Click on Set up New Application Button in the upper right hand corner.
3.2.Give Application name and click on create application button. Then you will see new application screen with detail including “API key”and “API Secret Key”
Note : This application will not work until you provide your Facebook application’s API keys.
Now to get started with actual coding:
Append Following code in FacebookAPIAppDelegate.h
#import<UIKit/UIKit.h>
#import "FBConnect/FBConnect.h"
#import "FBConnect/FBSession.h"
@class FacebookAPIViewController;
@interface FacebookAPIAppDelegate : NSObject {
UIWindow *window;
FacebookAPIViewController *viewController;
FBSession *_session;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet
FacebookAPIViewController *viewController;
@property (nonatomic,retain) FBSession *_session;
@end
Append Following code in FacebookAPIAppDelegate.m
#import "FacebookAPIAppDelegate.h"
#import "FacebookAPIViewController.h"
@implementation FacebookAPIAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize _session;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[_session release];
[viewController release];
[window release];
[super dealloc];
}
@end
Here in FacebookAPIAppDelegate we have just declared _session variable of type FBSession to keep track of the session and to check if session for current user exists or not.
Append Following code in FacebookAPIViewController.h
#import <UIKit/UIKit.h>
#import "FBConnect/FBConnect.h"
#import "FBConnect/FBSession.h"
@interface FacebookAPIViewController : UIViewController {
FBLoginButton *loginButton;
UIAlertView *facebookAlert;
FBSession *usersession;
NSString *username;
BOOL post;
}
@property(nonatomic,retain) FBLoginButton *loginButton;
@property(nonatomic,retain) UIAlertView *facebookAlert;
@property(nonatomic,retain) FBSession *usersession;
@property(nonatomic,retain) NSString *username;
@property(nonatomic,assign) BOOL post;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
-(void)getFacebookName;
-(void)postToWall;
@end
Append Following code in FacebookAPIViewController.m
#import "FacebookAPIViewController.h"
#import "FacebookAPIAppDelegate.h"
#define _APP_KEY @"Your API Key Goes here"
#define _SECRET_KEY @"Your Secrete Key Goes here"
@implementation FacebookAPIViewController
@synthesize loginButton;
@synthesize facebookAlert;
@synthesize usersession;
@synthesize username;
@synthesize post;
- (void)viewDidLoad {
FacebookAPIAppDelegate *appDelegate =
(FacebookAPIAppDelegate *) [[UIApplication
sharedApplication]delegate];
if (appDelegate._session == nil){
appDelegate._session = [FBSession
sessionForApplication:_APP_KEY
secret:_SECRET_KEY delegate:self];
}
if(self.loginButton == NULL)
self.loginButton = [[[FBLoginButton alloc] init] autorelease];
loginButton.frame = CGRectMake(0, 0, 100, 50);
[self.view addSubview:loginButton];
[super viewDidLoad];
}
- (void)dealloc {
[username release];
[usersession release];
[loginButton release];
[super dealloc];
}
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
self.usersession =session;
NSLog(@"User with id %lld logged in.", uid);
[self getFacebookName];
}
- (void)getFacebookName {
NSString* fql = [NSString stringWithFormat:
@"select uid,name from user where uid == %lld",
self.usersession.uid];
NSDictionary* params =
[NSDictionary dictionaryWithObject:fql
forKey:@"query"];
[[FBRequest requestWithDelegate:self]
call:@"facebook.fql.query" params:params];
self.post=YES;
}
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([request.method isEqualToString:@"facebook.fql.query"]) {
NSArray* users = result;
NSDictionary* user = [users objectAtIndex:0];
NSString* name = [user objectForKey:@"name"];
self.username = name;
if (self.post) {
[self postToWall];
self.post = NO;
}
}
}
- (void)postToWall {
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init]
autorelease];
dialog.userMessagePrompt = @"Enter your message:";
dialog.attachment = [NSString
stringWithFormat:@"{\"name\":\"Facebook Connect for
iPhone\",\"href\":\"http://developers.facebook.com/
connect.phptab=iphone\",\"caption\":\"Caption\",
\"description\":\"Description\",\"media\":[{\"type\":
\"image\",\"src\":\"http://img40.yfrog.com/img40/
5914/iphoneconnectbtn.jpg\",\"href\":
\"http://developers.facebook.com/connect.php?
tab=iphone/\"}],\"properties\":{\"another link\":
{\"text\":\"Facebook home page\",\"href\":
\"http://www.facebook.com\"}}}"];
[dialog show];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
Define API key and Secret key with the keys you received while registering your app on facebook.
#define _APP_KEY @"43e37a535cc09c2013bd76fde78dfcc7" #define _SECRET_KEY @"cc14801521a0c4d1dc31b7cacb891072"
Validate session variable in ViewDidLoad. If it doesn’t exist then create the same for using API key and Secret key. For that, one needs to conform the protocol FBSessionDelegate in respective header file. Also create a login button using FBLoginButton.
While implementing protocol FBSessionDelegate one needs to implement following mandatory method
(void)session:(FBSession*)session didLogin:(FBUID)uid
This methos is automatically called when user is logged in using FBConnect SDK.
In this method we get session for that user and it’s uid which unique identifier for that user.
Once FBSession session is avaiable, we can accesss all the APIs provided by Facebook.
For now, we will see how to post user name and status on the facebook wall.
To get Facebook username a request is send in which select query is written to get username using uid.
NSString* fql = [NSString stringWithFormat: @"select uid,name from user where uid == %lld", self.usersession.uid]; NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"]; [[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
Override following FBRequestDelegate method to check the reponse of above query.
(void)request:(FBRequest*)request didLoad:(id)result
The argument result is an array of NSDictionary Objects which contains info for that user as key-value pairs. Retrieve it as follows:
NSArray* users = result; NSDictionary* user = [users objectAtIndex:0]; NSString* name = [user objectForKey:@"name"];
Use FBStreamDialog class post message on the facbook wall. A dialog pops up with a message box to post on Wall.
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.userMessagePrompt = @"Enter your message:";
dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"Facebook Connect for iPhone\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}"];
[dialog show];
Now Save project (Command +S). Build and Run Project.
Simulator will look like as follows

Click on Fconnect Button and Facebook Login Dialog will appear.

Login with your user name and Password . Wait for untill post to Wall Dialog pops up

The Msg on Facebook will something look like this

You can download the source code from here
iPhone: View Based Application from scratch
Jan 12th
Today we will see how to create a View based application from scratch starting with a Window based application. Now one would ask when Xcode gives us a template to create a View Based Application, why create one from scratch. We should do this because a new iPhone developer should know how the template code is created so that he can understand what is going on behind the scene in the code and files created by the template. This improved understanding will allow you to customize the template as needed. Also this knowledge will help you when you would want to create a Navigation Controller Based or Tab Controller Based Application from scratch. (Yes there will be tutorials for those too, so stay tunes to our blog!) The code created by those templates is not easy to follow and modify, so I always prefer to create them from scratch.
Start Xcode and choose a Window based application with name as ViewBased.

- ViewBased Application Window
In Group & Files Panel (left side of above image) there is the Classes group which Contains 2 Delegate Files(.h or header and .m or implementation) are present. Every iPhone application will have one and only one object of type UIApplicationDelegate.
While creating Cocoa based programs you will hear the word delegate used quite often. Delegate is a software design pattern. To quote, iPhone Application Programming Guide :
Delegation is a mechanism used to avoid subclassing complex UIKit objects, such as the default UIApplication object. Instead of subclassing and overriding methods, you use the complex object unmodified and put your custom code in side the delegate object. As interesting events occur, the complex objects ends messages to your delegate object. You can use these hooks‚ to execute your custom code and implement the behavior you need.
ViewBasedAppDelegate is the name of our application delegate. Here (Viewbased+AppDelegate) ViewBased is the name of iPhone application. And AppDelgate indicates that it is derived from UIApplicationDelegate. UIApplicationDelegate is a protocol (ie interface in C# or Java) which gives you access to two important events in iPhone Application life cycle:
1. applicationDidFinishLaunching
This method gets called immediately after the application launches. As the name suggests one can implement this method to perform some initialization for the application.
2. applicationWillTerminate
This method is called just before the application is terminated or killed. Overriding this method will help to save any unsaved data before the application is terminated or killed.
Now right click on Classes group Add-New Files. You should get following dialog:

- Add New File: New View Controller
There are quite a few file types present for now just select Cocoa Touch Class From iPhone OS pane.
Cocoa Touch is one of the frameworks we use for iPhone development. Now select UIViewController subclass also tick on With XIB user interface. Click next and give name ViewOne and Click on Finished.
Now Drag n Drop ViewOne.xib from Class Folder to Resources folder as all .xib files lie there only.
Add the lines 5 and 9 from the following code to ViewBasedAppDelegate.h File
#import
@class ViewOne;
@interface ViewBasedAppDelegate : NSObject {
UIWindow *window;
ViewOne *viewOne;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic,retain)IBOutlet ViewOne *viewOne;
@end
In above code @class is used to forward declaration of ViewOne. @property and @synthesize is used as getters and setter (or can say accessors and mutators).
Now Add the lines 7, 12 and 17 from the following code to ViewBasedAppDelegate.m File
#import "ViewBasedAppDelegate.h"
#import "ViewOne.h"
@implementation ViewBasedAppDelegate
@synthesize window;
@synthesize viewOne;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:viewOne.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewOne release];
[window release];
[super dealloc];
}
@end
In above code applicationDidFinishLaunching: method will be called when your application just finishes its launching process. Using addSubview: method we add view inside the window. Then we make this window visible using makeKeyAndVisible.
In dealloc method we are deallocating memory for all objects that we have allocated/retained. Like viewOne, window etc.
Here make sure that you save (command + S) and build (command + B) the project. Otherwise sometimes interface builder won’t pick the outlets you just created.
Now double click on MainWindow.xib in Resources Group

- MainWindow.xib
There are four Objects in MainWindow.xib. They are
File’s Owner,First Responder, View Based App Delgate and Window.
The File’s Owner is one of the most important objects in a nib file. Because it provides link between application code and content of .xib file. Actually it is controller object in .xib.
View Based App Delegate is subclass of UIApplicationdelgate.

- Library
Generally every iPhone application will have only one AppDelegate file and one main window.
Now open Library from tools menu
(shift +command + L)
Add View Controller to window from library by drag n drop to MainWindow.xib. Please note that we are dragging the view controller on the window titled MainWindow.xib and not on the window title window. A new window will be created with view on it.
Now open Identity Inspector(command +4) and select view in MainWindow.xib
Change class name to Viewone.
Notice that View Controller changes to Viewone in mainwindow.xib. Select View Based App Delagate and open Connection Inspector (command +2) make connection with viewone as shown.
Go back to Xcode and double click on Viewone.xib. Open identity Inspector(command+1) change the background color.
Now save the changes. Goto xcode save the project (command+s) and build and rum your project.
At this point you have created a complete view based application from scratch starting from a window based application.
You can download the source code used in this tutorial from here : ViewBased Application
Update:
Hey thanks Alan for your thoughtful comments. I have updated the post accordingly. I really appreciate the time and effort you put in to make the tutorial better for the future readers.












