Friday, October 22, 2010

How to draw a graph (pie chart) using iPhone SDK

There are two ways:
1. by using google API for charts

    In this you just have to pass value through URL and google will return you a Graph Image, Then you can use it easily.


2. Another way is to use Graphics Methods

    I have an example for the same....

get it frm HERE.

Wednesday, October 6, 2010

iPhone FBConnect: Facebook Connect Tutorial


UPDATE: Please have a look to the updated post here...

http://viksiphoneblog.blogspot.in/2013/01/integration-of-facebooksdk-into-ios-50.html






By following these steps you can easily integrate with Facebook using FBConnect...

Create a Viewbased Application with name ‘FacebookAPI’.

before that:

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
#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  
#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 @"put your app key over here"
#define _SECRET_KEY @"put your secret key over here"

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
view source
print?

-(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




You can download the source code from here. I hope this will help u in a better way.

NOTE: Some contents of this post are fetched from some other web sources.

Sunday, October 3, 2010

UIActivityIndicator in status bar

spinner status bar To display a UIActivityIndicator use the UIApplication’s property: networkActivityIndicatorVisible.

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

and to disable:

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

That’s almost all about UIActivityIndicator. More can be found in UIActivityIndicator Class Reference in documentation.