I'm attempting to implement the latest Facebook Connect SDK and I'm having some troubles. For some reason the delegate callbacks for FBSessionDelegate protocol are not being fired. I've followed the instructions on the git Facebook page and tried to mimic the Facebook sample app but no luck. I'm going crazy here so I'm gonna post my code and maybe somebody will see something silly that I've missed.
#import <Foundation/Foundation.h>
#import "FBConnect.h"
#interface FacebookWrapper : UIViewController <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate>{
Facebook* _facebook;
NSArray* _permissions;
}
#property(readonly) Facebook *facebook;
- (void)login;
#end
#import "FacebookWrapper.h"
static NSString* kAppId = #"1234455667778";
#implementation FacebookWrapper
#synthesize facebook = _facebook;
- (id)init {
if (self = [super init]) {
_permissions = [[NSArray arrayWithObjects: #"read_stream", #"offline_access",nil] retain];
_facebook = [[Facebook alloc] initWithAppId:kAppId];
}
return self;
}
- (void)dealloc {
[_facebook release];
[_permissions release];
[super dealloc];
}
- (void)login {
[_facebook authorize:_permissions delegate:self];
}
- (void)fbDidLogin {
NSLog(#"Did Log In");
}
- (void)fbDidNotLogin:(BOOL)cancelled {
NSLog(#"Failed to log in");
}
- (void)fbDidLogout {
NSLog(#"Logged Out");
}
And to call this from another class,
FacebookWrapper *fw = [[FacebookWrapper alloc] init];
[fw login];
The behavior that I'm seeing on the phone is as expected. The Facebook app launches on init and permissions are requested. The phone then brings my app back to the foreground but the delegates for FBSessionDelegate are never fired. I've tried this in the Facebook sample app using my app ID and it worked fine. I have no idea what the difference is.
I figured out the problem. In the App Delegate you need to override handleOpenURL.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [[facebookWrapper facebook] handleOpenURL:url];
}
If you're creating a wrapper class as I am you'll need an instance of it in the app delegate so you can call the hanleOpenURL method in the Facebook class. Also notice that there is a public readonly property for my Facebook instance in my FacebookWrapper class so I can call handlOpenURL.
By doing this your app will know where to continue when it returns from getting permissions inside of the Facebook app.
I have developed wrapper class which will be very helpful to you.
First download latest SDK of Facebook.
Create one class named as "FacebookHelper"
Add following code in .h file:
#import <Foundation/Foundation.h>
#import "FBConnect.h"
#protocol FBApiCallDelegate;
typedef enum FBApiCallType
{
FBApiCallNone = 0,
FBApiCallGetUserInfo = 1,
FBApiCallGetUserFriend = 2,
FBApiCallPostMessage = 3,
FBApiCallPostPicture = 4,
FBApiCallShareLink = 5,
FBApiCallPostAll = 6,
FBApiCallPostMessageFriendWall = 7
} FBApiCallType;
#interface FacebookHelper : NSObject<FBRequestDelegate, FBDialogDelegate, FBSessionDelegate> {
Facebook *objFacebook;
NSArray *arrPermission;
id<FBApiCallDelegate> apiCallDelegate;
FBApiCallType currentApiCallType;
NSString *strMessage;
NSString *strUrlTitle;
NSString *strUrl;
NSString *strCaption;
NSString *strPictureUrl;
UIImage *image;
}
#property(readonly) Facebook *objFacebook;
#property (nonatomic, assign) id<FBApiCallDelegate> apiCallDelegate;
#property (nonatomic, assign) FBApiCallType currentApiCallType;
#property (nonatomic, retain) NSString *strMessage;
#property (nonatomic, retain) NSString *strUrlTitle;
#property (nonatomic, retain) NSString *strUrl;
#property (nonatomic, retain) NSString *strCaption;
#property (nonatomic, retain) NSString *strPictureUrl;
#property (nonatomic, retain) UIImage *image;
+ (FacebookHelper *) sharedInstance;
- (void)releaseObjects;
//Store Authentication
- (void)storeAuthData:(NSString *)accessToken expiresAt:(NSDate *)expiresAt;
- (void)removeAuthData;
//Public Methods
-(BOOL)isLoggedIn;
-(void)loginToFacebook;
-(void)logoutFromFacebook;
//Facebook Methods
-(void)getUserInfo; //Get User Info
-(void)getUserFriends; //Get User's Friend List
-(void)postMessageToWall; //Post Message to FB Wall
-(void)postPictureToWall; //Post Picture to FB Wall
-(void)shareLinkOnWall; //Share Link on FB Wall
-(void)postAllToWall; //Post All - Message, Link, Caption, PhotoUrl
-(void)postMessageToFriendWall; //Post Message to Friend Wall
//String Methods
- (BOOL)isEmptyString:(NSString *)strValue;
- (NSString *) trimWhiteSpace:(NSString *)strValue;
// Default AlertView
-(void)showAlertView:(NSString *)pstrTitle withMessage:(NSString *)pstrMessage delegate:(id)pDelegate;
#end
#protocol FBApiCallDelegate <NSObject>
#optional
//Get User Info Delegate
-(void)finishUserInfoResponse:(id)result;
-(void)failedUserInfoResponse:(NSError *)error;
//Get User's Friend List
-(void)finishUserFriendResponse:(id)result;
-(void)failedUserFriendResponse:(NSError *)error;
//Post Message to FB Wall Delegate
-(void)finishPostMessageResponse:(id)result;
-(void)failedPostMessageResponse:(NSError *)error;
//Post Picture to FB Wall
-(void)finishPostPictureResponse:(id)result;
-(void)failedPostPictureResponse:(NSError *)error;
//Share Link on FB Wall
-(void)finishShareLinkResponse:(id)result;
-(void)failedShareLinkResponse:(NSError *)error;
//Post All - Message, Link, Caption, PhotoUrl
-(void)finishPostAllResponse:(id)result;
-(void)failedPostAllResponse:(NSError *)error;
//Post Message to Friend Wall Delegate
-(void)finishPostMsgFriendWallResponse:(id)result;
-(void)failedPostMsgFriendWallResponse:(NSError *)error;
#end
Add following code in .m file:
#import "FacebookHelper.h"
#implementation FacebookHelper
#synthesize objFacebook;
#synthesize apiCallDelegate, currentApiCallType;
#synthesize strMessage, strUrlTitle, strUrl, strCaption, strPictureUrl, image;
#pragma mark -
#pragma mark Singleton Variables
static FacebookHelper *singletonHelper = nil;
#pragma mark -
#pragma mark Singleton Methods
- (id)init {
if (!g_kFacebookAppId) {
NSLog(#"%#", msgFBAppIDMissing);
exit(1);
return nil;
}
if ((self = [super init])) {
arrPermission = [[NSArray arrayWithObjects: #"read_stream", #"publish_stream", #"offline_access", #"email", #"read_friendlists", #"friends_birthday",nil] retain];
}
return self;
}
+ (FacebookHelper *)sharedInstance {
#synchronized(self) {
if (singletonHelper == nil) {
[[self alloc] init]; // assignment not done here
}
}
return singletonHelper;
}
+ (id)allocWithZone:(NSZone *)zone {
#synchronized(self) {
if (singletonHelper == nil) {
singletonHelper = [super allocWithZone:zone];
// assignment and return on first allocation
return singletonHelper;
}
}
// on subsequent allocation attempts return nil
return nil;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
//- (void)release {
- (void)dealloc {
[self releaseObjects];
[super dealloc];
}
- (id)autorelease {
return self;
}
- (void)releaseObjects {
[self.strMessage release];
[self.strUrlTitle release];
[self.strUrl release];
[self.strCaption release];
[self.strPictureUrl release];
[self.image release];
[objFacebook release];
objFacebook = nil;
}
#pragma mark -
#pragma mark FBDelegate(FBSessionDelegate) Methods
/**
* Called when the user has logged in successfully.
*/
- (void)fbDidLogin {
NSLog(#"FB login OK");
[self storeAuthData:objFacebook.accessToken expiresAt:objFacebook.expirationDate];
switch(currentApiCallType)
{
case FBApiCallGetUserInfo:
[self getUserInfo];
break;
case FBApiCallGetUserFriend:
[self getUserFriends];
break;
case FBApiCallPostMessage:
[self postMessageToWall];
break;
case FBApiCallPostPicture:
[self postPictureToWall];
break;
case FBApiCallShareLink:
[self shareLinkOnWall];
break;
case FBApiCallPostAll:
[self postAllToWall];
break;
case FBApiCallPostMessageFriendWall:
[self postMessageToFriendWall];
break;
}
}
/**
* Called when the user canceled the authorization dialog.
*/
-(void)fbDidNotLogin:(BOOL)cancelled {
NSLog(#"FB did not login");
[self removeAuthData];
}
/**
* Called when the request logout has succeeded.
*/
- (void)fbDidLogout {
NSLog(#"FB logout OK");
[self removeAuthData];
}
-(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
NSLog(#"token extended");
[self storeAuthData:accessToken expiresAt:expiresAt];
}
/**
* Called when the session has expired.
*/
- (void)fbSessionInvalidated {
[self showAlertView:msgFBSessionInvalidateTitle withMessage:msgFBSessionInvalidateMessage delegate:nil];
[self fbDidLogout];
}
#pragma mark -
#pragma mark FBRequestDelegate Methods
/**
* Called when the Facebook API request has returned a response. This callback
* gives you access to the raw response. It's called before
* (void)request:(FBRequest *)request didLoad:(id)result,
* which is passed the parsed response object.
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"received response");
}
/**
* Called when a request returns and its response has been parsed into
* an object. The resulting object may be a dictionary, an array, a string,
* or a number, depending on the format of the API response. If you need access
* to the raw response, use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
*/
- (void)request:(FBRequest *)request didLoad:(id)result {
NSLog(#"FB request OK");
switch(currentApiCallType)
{
case FBApiCallGetUserInfo:
if([self.apiCallDelegate respondsToSelector:#selector(finishUserInfoResponse:)])
[self.apiCallDelegate finishUserInfoResponse:result];
break;
case FBApiCallGetUserFriend:
if ([self.apiCallDelegate respondsToSelector:#selector(finishUserFriendResponse:)])
[self.apiCallDelegate finishUserFriendResponse:result];
break;
case FBApiCallPostMessage:
if ([self.apiCallDelegate respondsToSelector:#selector(finishPostMessageResponse:)])
[self.apiCallDelegate finishPostMessageResponse:result];
break;
case FBApiCallPostPicture:
if ([self.apiCallDelegate respondsToSelector:#selector(finishPostPictureResponse:)])
[self.apiCallDelegate finishPostPictureResponse:result];
break;
case FBApiCallShareLink:
if ([self.apiCallDelegate respondsToSelector:#selector(finishShareLinkResponse:)])
[self.apiCallDelegate finishShareLinkResponse:result];
break;
case FBApiCallPostAll:
if ([self.apiCallDelegate respondsToSelector:#selector(finishPostAllResponse:)])
[self.apiCallDelegate finishPostAllResponse:result];
break;
case FBApiCallPostMessageFriendWall:
if ([self.apiCallDelegate respondsToSelector:#selector(finishPostMsgFriendWallResponse:)])
[self.apiCallDelegate finishPostMsgFriendWallResponse:result];
break;
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
currentApiCallType = FBApiCallNone;
}
/**
* Called when an error prevents the Facebook API request from completing
* successfully.
*/
/*- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(#"FB error: %#", [error localizedDescription]);
}*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(#"Err message: %#", [[error userInfo] objectForKey:#"error_msg"]);
/*NSLog(#"Err code: %#", [error code]);
NSLog(#"Err desc: %#", [error description]);
NSLog(#"FB error: %#", [error localizedDescription]);*/
switch(currentApiCallType)
{
case FBApiCallGetUserInfo:
if ([self.apiCallDelegate respondsToSelector:#selector(failedUserInfoResponse:)])
[self.apiCallDelegate failedUserInfoResponse:error];
break;
case FBApiCallGetUserFriend:
if ([self.apiCallDelegate respondsToSelector:#selector(failedUserFriendResponse:)])
[self.apiCallDelegate failedUserFriendResponse:error];
break;
case FBApiCallPostMessage:
if ([self.apiCallDelegate respondsToSelector:#selector(failedPostMessageResponse:)])
[self.apiCallDelegate failedPostMessageResponse:error];
break;
case FBApiCallPostPicture:
if ([self.apiCallDelegate respondsToSelector:#selector(failedPostPictureResponse:)])
[self.apiCallDelegate failedPostPictureResponse:error];
break;
case FBApiCallShareLink:
if ([self.apiCallDelegate respondsToSelector:#selector(failedShareLinkResponse:)])
[self.apiCallDelegate failedShareLinkResponse:error];
break;
case FBApiCallPostAll:
if ([self.apiCallDelegate respondsToSelector:#selector(failedPostAllResponse:)])
[self.apiCallDelegate failedPostAllResponse:error];
case FBApiCallPostMessageFriendWall:
if ([self.apiCallDelegate respondsToSelector:#selector(failedPostMsgFriendWallResponse:)])
[self.apiCallDelegate failedPostMsgFriendWallResponse:error];
break;
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
currentApiCallType = FBApiCallNone;
}
/**
* Called when a UIServer Dialog successfully return.
*/
- (void)dialogDidComplete:(FBDialog *)dialog {
NSLog(#"Published successfully on FB");
}
#pragma mark -
#pragma mark Store/Remove Authentication
- (void)storeAuthData:(NSString *)accessToken expiresAt:(NSDate *)expiresAt {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:g_kFacebookAccessToken];
[defaults setObject:expiresAt forKey:g_kFacebookExpirationDate];
[defaults synchronize];
}
- (void)removeAuthData{
// Remove saved authorization information if it exists and it is
// ok to clear it (logout, session invalid, app unauthorized)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:g_kFacebookAccessToken];
[defaults removeObjectForKey:g_kFacebookExpirationDate];
[defaults synchronize];
/*
[[NSUserDefaults standardUserDefaults] setObject:#"" forKey:g_kFacebookAccessToken];
[[NSUserDefaults standardUserDefaults] setObject:#"" forKey:g_kFacebookExpirationDate];
[[NSUserDefaults standardUserDefaults] synchronize];*/
/*
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:g_kFacebookAccessToken];
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:g_kFacebookExpirationDate];
[[NSUserDefaults standardUserDefaults] synchronize];*/
}
#pragma mark -
#pragma mark Public Methods
-(BOOL)isLoggedIn
{
if(objFacebook == nil)
objFacebook = [[[Facebook alloc] initWithAppId:g_kFacebookAppId andDelegate:self] retain];
NSString *strAccessToken = [[NSUserDefaults standardUserDefaults] stringForKey:g_kFacebookAccessToken];
NSLog(#"Access Token = %#", strAccessToken);
//if(![strAccessToken isEmptyString])
if(![self isEmptyString:strAccessToken])
{
objFacebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:g_kFacebookAccessToken];
objFacebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:g_kFacebookExpirationDate];
}
if([objFacebook isSessionValid])
return YES;
else
return NO;
return NO;
}
-(void)loginToFacebook
{
if(![self isLoggedIn])
[objFacebook authorize:arrPermission];
}
-(void)logoutFromFacebook {
[objFacebook logout:self];
[self removeAuthData];
[self releaseObjects];
}
#pragma mark -
#pragma mark Facebook Methods
-(void)getUserInfo
{
currentApiCallType = FBApiCallGetUserInfo;
if([self isLoggedIn])
{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"name,picture",#"fields",nil];
[objFacebook requestWithGraphPath:#"me" andParams:params andDelegate:self];
}
else
[self loginToFacebook];
}
-(void)getUserFriends
{
currentApiCallType = FBApiCallGetUserFriend;
if([self isLoggedIn])
{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"picture,id,name,birthday,link,gender,last_name,first_name",#"fields",nil];
[objFacebook requestWithGraphPath:#"me/friends" andParams:params andDelegate:self];
}
else
[self loginToFacebook];
}
-(void)postMessageToWall
{
currentApiCallType = FBApiCallPostMessage;
if([self isLoggedIn])
{
NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
[params setObject:#"status" forKey:#"type"];
[params setObject:self.strMessage forKey:#"message"];
[objFacebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
}
else
[self loginToFacebook];
}
-(void)postPictureToWall
{
currentApiCallType = FBApiCallPostPicture;
if([self isLoggedIn])
{
NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
[params setObject:self.image forKey:#"source"];
[params setObject:self.strMessage forKey:#"message"];
[objFacebook requestWithGraphPath:#"me/photos" andParams:params andHttpMethod:#"POST" andDelegate:self];
}
else
[self loginToFacebook];
}
-(void)shareLinkOnWall
{
currentApiCallType = FBApiCallShareLink;
if([self isLoggedIn])
{
NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
[params setObject:#"link" forKey:#"type"];
[params setObject:self.strUrl forKey:#"link"];
[params setObject:self.strMessage forKey:#"description"];
[objFacebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
}
else
[self loginToFacebook];
}
-(void)postAllToWall
{
currentApiCallType = FBApiCallPostAll;
if([self isLoggedIn])
{
NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
[params setObject:self.strMessage forKey:#"description"];
[params setObject:self.strUrlTitle forKey:#"name"];
[params setObject:self.strUrl forKey:#"link"];
[params setObject:self.strCaption forKey:#"caption"];
[params setObject:self.strPictureUrl forKey:#"picture"];
[objFacebook requestWithGraphPath:#"me/feed" andParams:params andHttpMethod:#"POST" andDelegate:self];
}
else
[self loginToFacebook];
}
-(void)postMessageToFriendWall
{
currentApiCallType = FBApiCallPostMessageFriendWall;
if([self isLoggedIn])
{
NSString *strGraphPath = [NSString stringWithFormat:#"%#/feed", #"100002305497328"];
//NSString *strGraphPath = [NSString stringWithFormat:#"%#/feed", #"100002560928461"];
NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
[params setObject:self.strMessage forKey:#"message"];
[objFacebook requestWithGraphPath:strGraphPath andParams:params andHttpMethod:#"POST" andDelegate:self];
}
else
[self loginToFacebook];
}
#pragma mark -
#pragma mark String Methods
- (BOOL)isEmptyString:(NSString *)strValue
{
NSString *copy;
if (strValue == nil)
return (YES);
if ([strValue isEqualToString:#""])
return (YES);
if ([strValue isEqualToString:#"(null)"])
return (YES);
copy = [[strValue copy] autorelease];
//if ([[copy trimWhiteSpace] isEqualToString: #""])
if ([[self trimWhiteSpace:copy] isEqualToString: #""])
return (YES);
return (NO);
} /*stringIsEmpty*/
- (NSString *) trimWhiteSpace:(NSString *)strValue
{
NSMutableString *s = [[strValue mutableCopy] autorelease];
CFStringTrimWhitespace ((CFMutableStringRef) s);
return (NSString *) [[s copy] autorelease];
} /*trimWhiteSpace*/
#pragma mark -
#pragma mark Default AlertView
-(void)showAlertView:(NSString *)pstrTitle withMessage:(NSString *)pstrMessage delegate:(id)pDelegate
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:pstrTitle message:pstrMessage delegate:pDelegate cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
#end
That's it. Now our Facebook wrapper class is ready.
Create 3 below class for better organization.
FacebookGlobal.h
Add below data:
#define g_kFacebookAppId #"<Your AppID>"
#define g_kFacebookAppSecret #"<Your Secret>"
#define g_kPostMessage #"<Message>"
#define g_kPostUrlTitle #"<Title>"
#define g_kPostUrl #"<Url>"
#define g_kPostCaption #"<Caption>"
#define g_kPostPictureUrl #"<Image Url>"
FacebookKey.h
Add below data:
#define g_kFacebookAccessToken #"FBAccessTokenKey"
#define g_kFacebookExpirationDate #"FBExpirationDateKey"
FacebookMessage.h
Add below data:
//AppID Missing
#define msgFBAppIDMissing #"Missing AppID"
//Session Invalidated
#define msgFBSessionInvalidateTitle #"Auth Exception"
#define msgFBSessionInvalidateMessage #"Your session has expired."
Import below files in your .pch files:
#import "FacebookGlobal.h"
#import "FacebookKey.h"
#import "FacebookMessage.h"
#import "FacebookHelper.h"
Now, you are ready for use.
In your view controller, add below delegate:
<FBApiCallDelegate>
In your view controller .m file, add below methods to call as well delegate:
- (void)viewWillAppear:(BOOL)animated {
/*
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
[fbHelper logoutFromFacebook];
*/
//Get User Info
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
[fbHelper getUserInfo];
/*
//Get User's Friend List
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
[fbHelper getUserFriends];
*/
/*
//Post Message to FB Wall
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
fbHelper.strMessage = g_kPostMessage;
[fbHelper postMessageToWall];
*/
/*
//Post Picture to FB Wall
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
fbHelper.strMessage = g_kPostMessage;
fbHelper.image = [UIImage imageNamed:#"postPicture.jpg"];
[fbHelper postPictureToWall];
*/
/*
//Share Link on FB Wall
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
fbHelper.strUrl = g_kPostUrl;
fbHelper.strMessage = g_kPostMessage;
[fbHelper shareLinkOnWall];
*/
/*
//Post All to FB Wall
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
fbHelper.strMessage = g_kPostMessage;
fbHelper.strUrlTitle = g_kPostUrlTitle;
fbHelper.strUrl = g_kPostUrl;
fbHelper.strCaption = g_kPostCaption;
fbHelper.strPictureUrl = g_kPostPictureUrl;
[fbHelper postAllToWall];
*/
/*
//Post Message to Friend Wall
FacebookHelper *fbHelper = [FacebookHelper sharedInstance];
fbHelper.apiCallDelegate = self;
fbHelper.strMessage = g_kPostMessage;
[fbHelper postMessageToFriendWall];
*/
}
#pragma mark -
#pragma mark Get User Info Delegate
-(void)finishUserInfoResponse:(id)result{
NSLog(#"UserInfo response successed!");
NSLog(#"%#", result);
}
-(void)failedUserInfoResponse:(NSError *)error{
NSLog(#"UserInfo response failed!");
}
#pragma mark -
#pragma mark Get User's Friend List
-(void)finishUserFriendResponse:(id)result{
NSLog(#"User FriendList response successed!");
NSLog(#"%#", result);
}
-(void)failedUserFriendResponse:(NSError *)error{
NSLog(#"User FriendList response failed!");
}
#pragma mark -
#pragma mark Post Message to FB Wall Delegate
-(void)finishPostMessageResponse:(id)result{
NSLog(#"Post message successed!");
NSLog(#"%#", result);
}
-(void)failedPostMessageResponse:(NSError *)error{
NSLog(#"Post message failed!");
}
#pragma mark -
#pragma mark Post Picture to FB Wall
-(void)finishPostPictureResponse:(id)result{
NSLog(#"Post picture successed!");
NSLog(#"%#", result);
}
-(void)failedPostPictureResponse:(NSError *)error{
NSLog(#"Post picture failed!");
}
#pragma mark -
#pragma mark Share Link on FB Wall
-(void)finishShareLinkResponse:(id)result{
NSLog(#"Share link successed!");
NSLog(#"%#", result);
}
-(void)failedShareLinkResponse:(NSError *)error{
NSLog(#"Share link failed!");
}
#pragma mark -
#pragma mark Post All - Message, Link, Caption, PhotoUrl
-(void)finishPostAllResponse:(id)result{
NSLog(#"Post all successed!");
NSLog(#"%#", result);
}
-(void)failedPostAllResponse:(NSError *)error{
NSLog(#"Post all failed!");
}
#pragma mark -
#pragma mark Post Message to Friend Wall Delegate
-(void)finishPostMsgFriendWallResponse:(id)result{
NSLog(#"Post message to friend wall successed!");
NSLog(#"%#", result);
}
-(void)failedPostMsgFriendWallResponse:(NSError *)error{
NSLog(#"Post message to friend wall failed!");
}
Happy Coding :)
Enjoy.
Related
I am trying to make a jabber chat application using the xmppframework.
I have implemented the xmppStream methods in the applicationAppDelegate, but none of these method has been invoked.
Here is the code of the applicationAppDelegate:
- (void)setupStream {
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//[self connect];
}
- (void)goOnline {
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
}
- (void)goOffline {
XMPPPresence *presence = [XMPPPresence presenceWithType:#"unavailable"];
[[self xmppStream] sendElement:presence];
}
- (BOOL)connect {
[self setupStream];
NSString *emailUserDefault = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
NSString *jabberID = [emailUserDefault stringByAppendingString:#"#server.local"];
NSLog(#"%#",jabberID);
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:#"password"];
NSLog(#"%#",myPassword);
if (![xmppStream isDisconnected]) {
NSLog(#"You are connected");
return YES;
}
if (jabberID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
//xmppStream.myJID = [XMPPJID jidWithString:jabberID];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:20 error:&error])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:[NSString stringWithFormat:#"Can't connect to server %#", [error localizedDescription]]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
return NO;
}
return YES;
}
- (void)disconnect {
[self goOffline];
[xmppStream disconnect];
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
isOpen = YES;
NSError *error = nil;
[[self xmppStream] authenticateWithPassword:password error:&error];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
[self goOnline];
}
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
if (![presenceFromUser isEqualToString:myUsername]) {
if ([presenceType isEqualToString:#"available"]) {
[__chatDelegate newBuddyOnline:[NSString stringWithFormat:#"%##%#", presenceFromUser, #"server.local"]];
} else if ([presenceType isEqualToString:#"unavailable"]) {
[__chatDelegate buddyWentOffline:[NSString stringWithFormat:#"%##%#", presenceFromUser, #"server.local"]];
}
}
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
NSString *msg = [[message elementForName:#"body"] stringValue];
NSString *from = [[message attributeForName:#"from"] stringValue];
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:#"msg"];
[m setObject:from forKey:#"sender"];
[__messageDelegate newMessageReceived:m];
}
Here my code for the chatViewController classe:
- (myApplicationAppDelegate *)appDelegate {
return (myApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (XMPPStream *)xmppStream {
return [[self appDelegate] xmppStream];
}
- (void)viewDidLoad
{
[super viewDidLoad];
onlineBuddies = [[NSMutableArray alloc ] init];
myApplicationAppDelegate *del = [self appDelegate];
[self xmppStream];
NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:#"email"];
del._chatDelegate = self;
if (login) {
if ([[self appDelegate] connect]) {
NSLog(#"show buddy list");
}
} else {
NSLog(#"Login Error");
}
}
I cannot figure out why the xmpp delegate methods are not being invoked. If someone can give me a hand, please don't hesitate.
Thanks in advance.
I think you misunderstood the purpose of AppDelegate. First of all for every iOS app that you are creating in Xcode there is a class created that contains the name AppDelegate but this class should only be used to get information of your application state, such as if the app goes to background, if it's launched with success or if it's coming up from background. Also the app delegate is used to specify the root (or entry point) view controller of your application.
So I think you should first check the basic rules (or a basic tutorial) on how to create a very simple application (a "Hello World Application"), after that you can go forward and create a basic structure of your application and decide what view controller or what model classes will handle your connection handling and response/request parsing.
I strongly suggest that you have a look over view controllers and I'm pretty sure that after you do the above suggested "tasks" you will answer yourself the posted question.
P.S Last point, have a look on "iOS naming & other conventions"enter link description herelife cycle methods
How could I upload/download a file from/to dropbox? I integrated dropbox applications in my app, but I couldn't upload/download a file.
This is my upload code:
- (NSString *)getDocumentPath
{
NSMutableData * pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"vani.doc"];
[pdfData writeToFile:path atomically:YES];
return path;
}
- (IBAction)upload:(id)sender {
NSString *path = [self getDocumentPath];
NSString * local = [path lastPathComponent];
NSString *destDir = #"/Plist Folder/vani.doc";
[restClient uploadFile:local toPath:destDir withParentRev:nil fromPath:path];
This is for downloading:
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
NSString *fileName = [NSString stringWithFormat:#"/vani.doc"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path2 = [documentsDirectory stringByAppendingString:
[NSString stringWithFormat:#"%#", fileName]];
//NSLog(#"%#", path2);
[restClient loadFile:fileName intoPath:path2];
Let me help you in this case, I have created Wrapper class for Dropbox find code below used in one of my project. It does not support ARC. Create DropBoxManager header and implementation file
This may be too much for you if you are beginner but try to read whole answer and follow step by step. Let me know in case of any problem, I will help.
Code for DropBoxManager.h
#import <Foundation/Foundation.h>
#import <DropboxSDK/DropboxSDK.h>
#define kDropbox_AppKey #"" // Provide your key here
#define kDropbox_AppSecret #"" // Provide your secret key here
#define kDropbox_RootFolder kDBRootDropbox //Decide level access like root or app
#protocol DropBoxDelegate;
typedef enum
{
DropBoxTypeStatusNone = 0,
DropBoxGetAccountInfo = 1,
DropBoxGetFolderList = 2,
DropBoxCreateFolder = 3,
DropBoxUploadFile = 4
} DropBoxPostType;
#interface DropboxManager : NSObject <DBRestClientDelegate,DBSessionDelegate,UIAlertViewDelegate>
{
UIViewController<DropBoxDelegate> *apiCallDelegate;
DBSession *objDBSession;
NSString *relinkUserId;
DBRestClient *objRestClient;
DropBoxPostType currentPostType;
NSString *strFileName;
NSString *strFilePath;
NSString *strDestDirectory;
NSString *strFolderCreate;
NSString *strFolderToList;
}
#property (nonatomic,retain) DBSession *objDBSession;
#property (nonatomic,retain) NSString *relinkUserId;
#property (nonatomic,assign) UIViewController<DropBoxDelegate> *apiCallDelegate;
#property (nonatomic,retain) DBRestClient *objRestClient;
#property (nonatomic,assign) DropBoxPostType currentPostType;
#property (nonatomic,retain) NSString *strFileName;
#property (nonatomic,retain) NSString *strFilePath;
#property (nonatomic,retain) NSString *strDestDirectory;
#property (nonatomic,retain) NSString *strFolderCreate;
#property (nonatomic,retain) NSString *strFolderToList;
//Singleton
+(id)dropBoxManager;
//Initialize dropbox
-(void)initDropbox;
//Authentication Verification
-(BOOL)handledURL:(NSURL*)url;
-(void)dropboxDidLogin;
-(void)dropboxDidNotLogin;
//Upload file
-(void)uploadFile;
//Download File
-(void)downlaodFileFromSourcePath:(NSString*)pstrSourcePath destinationPath:(NSString*)toPath;
//Create Folder
-(void)createFolder;
//Get Account Information
-(void)loginToDropbox;
-(void)logoutFromDropbox;
-(BOOL)isLoggedIn;
//List Folders
-(void)listFolders;
#end
#protocol DropBoxDelegate <NSObject>
#optional
- (void)finishedLogin:(NSMutableDictionary*)userInfo;
- (void)failedToLogin:(NSString*)withMessage;
- (void)finishedCreateFolder;
- (void)failedToCreateFolder:(NSString*)withMessage;
- (void)finishedUploadFile;
- (void)failedToUploadFile:(NSString*)withMessage;
- (void)finishedDownloadFile;
- (void)failedToDownloadFile:(NSString*)withMessage;
- (void)getFolderContentFinished:(DBMetadata*)metadata;
- (void)getFolderContentFailed:(NSString*)withMessage;
#end
Code for DropBoxManager.m
#import "DropboxManager.h"
#implementation DropboxManager
#synthesize objDBSession,relinkUserId,apiCallDelegate;
#synthesize objRestClient;
#synthesize currentPostType;
#synthesize strFileName;
#synthesize strFilePath;
#synthesize strDestDirectory;
#synthesize strFolderCreate;
#synthesize strFolderToList;
static DropboxManager *singletonManager = nil;
+(id)dropBoxManager
{
if(!singletonManager)
singletonManager = [[DropboxManager alloc] init];
return singletonManager;
}
-(void)initDropbox
{
DBSession* session = [[DBSession alloc] initWithAppKey:kDropbox_AppKey appSecret:kDropbox_AppSecret root:kDropbox_RootFolder];
session.delegate = self;
[DBSession setSharedSession:session];
[session release];
if([[DBSession sharedSession] isLinked] && objRestClient == nil)
{
self.objRestClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
self.objRestClient.delegate = self;
}
}
-(void)checkForLink
{
if(![[DBSession sharedSession] isLinked])
[[DBSession sharedSession] linkFromController:apiCallDelegate];
}
-(BOOL)handledURL:(NSURL*)url
{
BOOL isLinked=NO;
if ([[DBSession sharedSession] handleOpenURL:url])
{
if([[DBSession sharedSession] isLinked])
{
isLinked=YES;
[self dropboxDidLogin];
}
else
{
isLinked = NO;
[self dropboxDidNotLogin];
}
}
return isLinked;
}
#pragma mark -
#pragma mark Handle login
-(void)dropboxDidLogin
{
NSLog(#"Logged in");
if([[DBSession sharedSession] isLinked] && self.objRestClient == nil)
{
self.objRestClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
self.objRestClient.delegate = self;
}
switch(currentPostType)
{
case DropBoxTypeStatusNone:
break;
case DropBoxGetAccountInfo:
[self loginToDropbox];
break;
case DropBoxGetFolderList:
[self listFolders];
break;
case DropBoxCreateFolder:
[self createFolder];
break;
case DropBoxUploadFile:
[self uploadFile];
break;
}
//[(MainViewController*)apiCallDelegate setLoginStatus];
}
-(void)dropboxDidNotLogin
{
NSLog(#"Not Logged in");
switch(currentPostType)
{
case DropBoxTypeStatusNone:
break;
case DropBoxUploadFile:
if([self.apiCallDelegate respondsToSelector:#selector(failedToUploadFile:)])
[self.apiCallDelegate failedToUploadFile:#"Problem connecting dropbox. Please try again later."];
break;
case DropBoxGetFolderList:
break;
case DropBoxCreateFolder:
break;
case DropBoxGetAccountInfo:
break;
}
}
#pragma mark -
#pragma mark DBSessionDelegate methods
- (void)sessionDidReceiveAuthorizationFailure:(DBSession*)session userId:(NSString *)userId
{
relinkUserId = [userId retain];
[[[[UIAlertView alloc] initWithTitle:#"Dropbox Session Ended" message:#"Do you want to relink?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Relink", nil] autorelease] show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index
{
if (index != alertView.cancelButtonIndex)
[[DBSession sharedSession] linkUserId:relinkUserId fromController:apiCallDelegate];
[relinkUserId release];
relinkUserId = nil;
}
#pragma mark -
#pragma mark Fileupload
-(void)uploadFile
{
if([[DBSession sharedSession] isLinked])
[self.objRestClient uploadFile:strFileName toPath:strDestDirectory withParentRev:nil fromPath:strFilePath];
else
[self checkForLink];
}
-(void)downlaodFileFromSourcePath:(NSString*)pstrSourcePath destinationPath:(NSString*)toPath
{
if([[DBSession sharedSession] isLinked])
[self.objRestClient loadFile:pstrSourcePath intoPath:toPath];
else
[self checkForLink];
}
- (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath from:(NSString*)srcPath metadata:(DBMetadata*)metadata
{
if([self.apiCallDelegate respondsToSelector:#selector(finishedUploadeFile)])
[self.apiCallDelegate finishedUploadFile];
NSLog(#"File uploaded successfully to path: %#", metadata.path);
}
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath contentType:(NSString*)contentType
{
if([self.apiCallDelegate respondsToSelector:#selector(finishedDownloadFile)])
[self.apiCallDelegate finishedDownloadFile];
}
-(void)restClient:(DBRestClient *)client loadFileFailedWithError:(NSError *)error
{
if([self.apiCallDelegate respondsToSelector:#selector(failedToDownloadFile:)])
[self.apiCallDelegate failedToDownloadFile:[error description]];
}
- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error
{
if([self.apiCallDelegate respondsToSelector:#selector(failedToUploadFile:)])
[self.apiCallDelegate failedToUploadFile:[error description]];
NSLog(#"File upload failed with error - %#", error);
}
#pragma mark -
#pragma mark Create Folder
-(void)createFolder
{
if([[DBSession sharedSession] isLinked])
[self.objRestClient createFolder:strFolderCreate];
else
[self checkForLink];
}
- (void)restClient:(DBRestClient*)client createdFolder:(DBMetadata*)folder
{
if([self.apiCallDelegate respondsToSelector:#selector(finishedCreateFolder)])
[self.apiCallDelegate finishedCreateFolder];
NSLog(#"Folder created successfully to path: %#", folder.path);
}
- (void)restClient:(DBRestClient*)client createFolderFailedWithError:(NSError*)error
{
if([self.apiCallDelegate respondsToSelector:#selector(failedToCreateFolder:)])
[self.apiCallDelegate failedToCreateFolder:[error description]];
NSLog(#"Folder create failed with error - %#", error);
}
#pragma mark -
#pragma mark Load account information
-(void)loginToDropbox
{
if([[DBSession sharedSession] isLinked])
[self.objRestClient loadAccountInfo];
else
[self checkForLink];
}
- (void)restClient:(DBRestClient*)client loadedAccountInfo:(DBAccountInfo*)info
{
if([self.apiCallDelegate respondsToSelector:#selector(finishedLogin:)])
{
NSMutableDictionary *userInfo = [[[NSMutableDictionary alloc] init] autorelease];
[userInfo setObject:info.displayName forKey:#"UserName"];
[userInfo setObject:info.userId forKey:#"UserID"];
[userInfo setObject:info.referralLink forKey:#"RefferelLink"];
[self.apiCallDelegate finishedLogin:userInfo];
}
NSLog(#"Got Information: %#", info.displayName);
}
- (void)restClient:(DBRestClient*)client loadAccountInfoFailedWithError:(NSError*)error
{
if([self.apiCallDelegate respondsToSelector:#selector(failedToLogin:)])
[self.apiCallDelegate failedToLogin:[error description]];
NSLog(#"Failed to get account information with error - %#", error);
}
#pragma mark -
#pragma mark Logout
-(void)logoutFromDropbox
{
[[DBSession sharedSession] unlinkAll];
[self.objRestClient release];
}
#pragma mark -
#pragma mark Check for login
-(BOOL)isLoggedIn
{
return [[DBSession sharedSession] isLinked] ? YES : NO;
}
#pragma mark -
#pragma mark Load Folder list
-(void)listFolders
{
NSLog(#"Here-->%#",self.strFolderToList);
if([[DBSession sharedSession] isLinked])
[self.objRestClient loadMetadata:self.strFolderToList];
else
[self checkForLink];
}
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata
{
if (metadata.isDirectory)
{
NSLog(#"Folder '%#' contains:", metadata.contents);
for (DBMetadata *file in metadata.contents)
{
NSLog(#"\t%#", file);
}
if([apiCallDelegate respondsToSelector:#selector(getFolderContentFinished:)])
[apiCallDelegate getFolderContentFinished:metadata];
}
NSLog(#"Folder list success: %#", metadata.path);
}
- (void)restClient:(DBRestClient*)client metadataUnchangedAtPath:(NSString*)path
{
}
- (void)restClient:(DBRestClient*)client loadMetadataFailedWithError:(NSError*)error
{
NSLog(#"Load meta data failed with error - %#", error);
if([apiCallDelegate respondsToSelector:#selector(getFolderContentFailed:)])
[apiCallDelegate getFolderContentFailed:[error localizedDescription]];
}
E.g. usage header file
//Your view controller Header file.
#import <UIKit/UIKit.h>
#import "DropboxManager.h"
#interface YourViewController : UIViewController <DropBoxDelegate>
{
DropboxManager *objManager;
}
#property (nonatomic,assign) DropboxManager *objManager;
-(IBAction)btnUploadFileTapped:(id)sender;
#end
E.g. usage implementation file
#import "YourViewController.h"
#implementation YourViewController
#synthesize objManager;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
objManager = [DropboxManager dropBoxManager];
objManager.apiCallDelegate =self;
[objManager initDropbox];
}
-(IBAction)btnUploadFileTapped:(id)sender
{
objManager.currentPostType = DropBoxUploadFile;
objManager.strFileName = #"YourFileName";
objManager.strFilePath = #"YourFilePath";
objManager.strDestDirectory = #"/";
[objManager uploadFile];
}
#pragma mark -
#pragma mark File upload delegate
- (void)finishedUploadFile
{
NSLog(#"Uploaded successfully.");
}
- (void)failedToUploadFile:(NSString*)withMessage
{
NSLog(#"Failed to upload error is %#",withMessage);
}
#end
Just add
import UIKit/UIKit.h
wherever you are getting error other than ARC.
I'm following the tutorial at:
http://mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/ to set up an iOS app with an ejabberd server. So far I have pretty much copied the code over to a new project.
My problem is that the XMPP delegate functions AppDelegate.m are not being called when run on the phone. Everything works fine in Simulator and the two functions below are called.
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
NSLog(#"in WSAppDelegate - xmppStreamDidConnect");
isOpen = YES;
NSError *error = nil;
[[self xmppStream] authenticateWithPassword:password error:&error];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
NSLog(#"in WSAppDelegate - xmppStreamDidAuthenticate");
[self goOnline];
}
I am able to connect on both the phone and simulator as this call runs without error:
[xmppStream connect:&error]
Here is my AppDelegate.h code:
#import <UIKit/UIKit.h>
#import "XMPPRoster.h"
#import "XMPP.h"
#import "SMChatDelegate.h"
#import "SMMessageDelegate.h"
#class SMBuddyListViewController;
#interface WSAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SMBuddyListViewController *viewController;
XMPPStream *xmppStream;
XMPPRoster *xmppRoster;
NSString *password;
BOOL isOpen;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet SMBuddyListViewController *viewController;
#property (nonatomic, readonly) XMPPStream *xmppStream;
#property (nonatomic, readonly) XMPPRoster *xmppRoster;
#property (nonatomic, assign) id _chatDelegate;
#property (nonatomic, assign) id _messageDelegate;
- (BOOL)connect;
- (void)disconnect;
#end
And AppDelegate.m:
#import "WSBuddyListViewController.h"
#interface WSAppDelegate()
- (void)setupStream;
- (void)goOnline;
- (void)goOffline;
#end
#implementation WSAppDelegate
#synthesize xmppStream;
#synthesize xmppRoster;
#synthesize window;
#synthesize viewController;
#synthesize _chatDelegate;
#synthesize _messageDelegate;
- (void)applicationWillResignActive:(UIApplication *)application {
[self disconnect];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self setupStream];
BOOL connected = NO;
connected = [self connect];
NSLog(#"*** connected = %i", connected);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)setupStream {
NSLog(#"in WSAppDelegate - setupStream");
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppStream setHostName:#"localhost"];
}
- (void)goOnline {
NSLog(#"in WSAppDelegate - goOnline");
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
}
- (void)goOffline {
XMPPPresence *presence = [XMPPPresence presenceWithType:#"unavailable"];
[[self xmppStream] sendElement:presence];
}
- (BOOL)connect {
NSLog(#"in WSAppDelegate - connect");
[self setupStream];
NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:#"userID"];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:#"userPassword"];
if (![xmppStream isDisconnected]) {
NSLog(#"in WSAppDelegate - connect - if (![xmppStream isDisconnected]) ");
return YES;
}
if (jabberID == nil || myPassword == nil) {
NSLog(#"in WSAppDelegate - connect - if (jabberID == nil || myPassword == nil)");
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connect:&error])
{
NSLog(#"in WSAppDelegate - connect - if (![xmppStream connect:&error]))");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:[NSString stringWithFormat:#"Can't connect to server %#", [error localizedDescription]]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
return NO;
}
return YES;
}
- (void)disconnect {
[self goOffline];
[xmppStream disconnect];
[_chatDelegate didDisconnect];
}
#pragma mark -
#pragma mark XMPP delegates
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
NSLog(#"in WSAppDelegate - xmppStreamDidConnect");
isOpen = YES;
NSError *error = nil;
[[self xmppStream] authenticateWithPassword:password error:&error];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
NSLog(#"in WSAppDelegate - xmppStreamDidAuthenticate");
[self goOnline];
}
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
return NO;
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
NSLog(#"in WSAppDelegate - xmppStream:(XMPPStream *)sender didReceiveMessage");
NSString *msg = [[message elementForName:#"body"] stringValue];
NSString *from = [[message attributeForName:#"from"] stringValue];
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:#"msg"];
[m setObject:from forKey:#"sender"];
[_messageDelegate newMessageReceived:m];
}
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
NSLog(#"in WSAppDelegate - xmppStream:(XMPPStream *)sender didReceivePresence:");
NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
if (![presenceFromUser isEqualToString:myUsername]) {
if ([presenceType isEqualToString:#"available"]) {
[_chatDelegate newBuddyOnline:[NSString stringWithFormat:#"%##%#", presenceFromUser, #"localhost"]];
} else if ([presenceType isEqualToString:#"unavailable"]) {
[_chatDelegate buddyWentOffline:[NSString stringWithFormat:#"%##%#", presenceFromUser, #"localhost"]];
}
}
}
- (void)dealloc {
[xmppStream removeDelegate:self];
[xmppRoster removeDelegate:self];
[xmppStream disconnect];
}
#end
If you look at your setupStream method, you are using the name "localhost." This is leading me to believe the server is on your development machine, and the device is trying to connect to itself (localhost). You will have to replace that with your server name.
This likely works in the simulator because the the client and server are one and the same.
UPDATE:
I just read through the tutorial, and it does not do a good job at all at describing how it would work on real device.
as Mike D said, you're connecting to the server which is on your machine([xmppStream setHostName:#"localhost"];)
In order to connect to the "localhost" you have to change hosts file on your device(/etc/hosts), but that is forbidden by Apple, since your app cant change stuff outside the sandbox.(unless the device is jailbroken).
I've done this stuff on an android phone, when faced similar problem in the past.
Check this discussion(Does hosts file exist on the iPhone? How to change it?)
I am using the Facebook iOS SDK for iPhone. I initialize the Facebook instance
facebook = [[Facebook alloc] initWithAppId:kAppId];
And then I do login:
[facebook authorize:permissions delegate:self];
After I logged in to Facebook I am doing the following to get the user profile information:
[facebook requestWithGraphPath:#"me" andDelegate:self];
NSMutableData *response = [fbRequest responseText];
unsigned char *firstBuffer = [response mutableBytes];
NSLog(#"Got Facebook Profile: : \"%s\"\n", (char *)firstBuffer);
But I get the following on my console:
Got Facebook Profile: "(null)"
What am I doing wrong, also I believe that Facebook response is a json string and I am looking to get a hold of that json string.
I thought may be I should make it a wiki and tell the people how I am doing it. because lot of people are facing similar problem.
The first thing that I did was.
In Facebook.m class I added the following statement in the following method
(void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
safariAuth:(BOOL)trySafariAuth
trySafariAuth = NO;
This prevents a safari page to get open for the facebook login, but it pops up a screen in app itself. Then i created a helper class for Facebook, the header file code is here.
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#interface FaceBookHelper : UIViewController
<FBRequestDelegate,
FBDialogDelegate,
FBSessionDelegate>{
Facebook *facebook;
NSArray *permissions;
}
#property(readonly) Facebook *facebook;
- (void)login;
-(void)getUserInfo:(id)sender;
- (void)getUserFriendList:(id)sender;
-(void)postToFriendsWall;
The .m file.
static NSString* kAppId = #"xxx";
#define ACCESS_TOKEN_KEY #"fb_access_token"
#define EXPIRATION_DATE_KEY #"fb_expiration_date"
#implementation FaceBookHelper
#synthesize facebook;
//////////////////////////////////////////////////////////////////////////////////////////////////
// UIViewController
/**
* initialization
*/
- (id)init {
if (self = [super init]) {
facebook = [[Facebook alloc] initWithAppId:kAppId];
facebook.sessionDelegate = self;
permissions = [[NSArray arrayWithObjects:
#"email", #"read_stream", #"user_birthday",
#"user_about_me", #"publish_stream", #"offline_access", nil] retain];
[self login];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (void)dealloc {
[facebook release];
[permissions release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
/**
* Login.
*/
- (void)login {
// only authorize if the access token isn't valid
// if it *is* valid, no need to authenticate. just move on
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
}
/**
* This is the place only where you will get the hold on the accessToken
*
**/
- (void)fbDidLogin {
NSLog(#"Did Log In");
NSLog(#"Access Token is %#", facebook.accessToken );
NSLog(#"Expiration Date is %#", facebook.expirationDate );
// Store the value in the NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:facebook.accessToken forKey:ACCESS_TOKEN_KEY];
[defaults setObject:facebook.expirationDate forKey:EXPIRATION_DATE_KEY];
[defaults synchronize];
// This is the best place to login because here we know that user has already logged in
[self getUserInfo:self];
//[self getUserFriendList:self];
//[self postToFriendsWall];
}
- (void)fbDidNotLogin:(BOOL)cancelled {
NSLog(#"Failed to log in");
}
- (void)getUserInfo:(id)sender {
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
- (void)getUserFriendList:(id)sender {
[facebook requestWithGraphPath:#"me/friends" andDelegate:self];
}
////////////////////////////////////////////////////////////////////////////////
// FBRequestDelegate
/**
* Called when the Facebook API request has returned a response. This callback
* gives you access to the raw response. It's called before
* (void)request:(FBRequest *)request didLoad:(id)result,
* which is passed the parsed response object.
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"Inside didReceiveResponse: received response");
//NSLog(#"Status Code #", [response statusCode]);
NSLog(#"URL #", [response URL]);
}
/**
* Called when a request returns and its response has been parsed into
* an object. The resulting object may be a dictionary, an array, a string,
* or a number, depending on the format of the API response. If you need access
* to the raw response, use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
*/
- (void)request:(FBRequest *)request didLoad:(id)result {
NSLog(#"Inside didLoad");
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
// When we ask for user infor this will happen.
if ([result isKindOfClass:[NSDictionary class]]){
//NSDictionary *hash = result;
NSLog(#"Birthday: %#", [result objectForKey:#"birthday"]);
NSLog(#"Name: %#", [result objectForKey:#"name"]);
}
if ([result isKindOfClass:[NSData class]])
{
NSLog(#"Profile Picture");
//[profilePicture release];
//profilePicture = [[UIImage alloc] initWithData: result];
}
NSLog(#"request returns %#",result);
//if ([result objectForKey:#"owner"]) {}
};
/**
* Called when an error prevents the Facebook API request from completing
* successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
//[self.label setText:[error localizedDescription]];
};
////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate
/**
* Called when a UIServer Dialog successfully return.
*/
- (void)dialogDidComplete:(FBDialog *)dialog {
//[self.label setText:#"publish successfully"];
}
#end
Thanks for this hint Yogesh!
In facebook.m you can also set the safariAuth param in the authorize method.
- (void)authorize:(NSArray *)permissions
delegate:(id<FBSessionDelegate>)delegate {
...
[self authorizeWithFBAppAuth:YES safariAuth:NO];
}
I am trying to integrate Facebook connect to my iPhone app but I have an error (code 10000):
did fail: The operation couldn’t be completed. (facebookErrDomain error 10000.)
When I try to update my wall. The code seems pretty simple (I had to struggle to find some doc though).
- (void)viewDidLoad {
[super viewDidLoad];
// Permissions
NSArray *permissions = [[NSArray arrayWithObjects:#"publish_stream",#"read_stream",#"offline_access",nil] retain];
// Connection
Facebook *facebook = [[Facebook alloc] init];
[facebook authorize:#"MY_APP_ID" permissions:permissions delegate:self];
// Update my wall
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"MY_API_KEY", #"api_key", #"test", #"message", nil];
[facebook requestWithGraphPath:#"me/home" andParams:params andHttpMethod:#"POST" andDelegate:self];
}
// FBRequestDelegate
- (void)request:(FBRequest*)request didLoad:(id)result {
NSArray* users = result;
NSDictionary* user = [users objectAtIndex:0];
NSString* name = [user objectForKey:#"name"];
NSLog(#"Query returned %#", name);
}
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error {
NSLog(#"did fail: %#", [error localizedDescription]);
}
- (void)request:(FBRequest*)request didReceiveResponse:(NSURLResponse*)response {
NSLog(#"did r response");
}
Cannot really figure out what is wrong.
Thanks a lot,
Luc
I think I figured it out, I used to have the same issue. Your issue should be solved if you authenticate first with the right permissions and make sure you actually get to accept the needed permissions (publish_stream). Make sure you actually get to press the "allow" button. In your current code you won't see it, because you immediately try to post a message after authenticating, yet authentication isn't yet completed. As a result you most likely don't have a valid accessToken, preventing you to post messages to the wall.
// you perform authorization, but you don't wait for the user to accept the publish permission
[facebook authorize:#"MY_APP_ID" permissions:permissions delegate:self];
// you attempt to publish, but you don't have the permission to publish yet ...
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"MY_API_KEY", #"api_key", #"test", #"message", nil];
[facebook requestWithGraphPath:#"me/home" andParams:params andHttpMethod:#"POST" andDelegate:self];
How to fix this issue?
This is the way I fixed it (please note DLog() is a macro for NSLog()):
/* The operationQueue array is used to keep track of operations that need to be
completed in order (e.g. if we aren't logged in when we want to post, first
log in, then post - this way we'll make sure we always have a valid
accessToken. */
- (id)initWithDelegate:(id <ServiceDelegate>)serviceDelegate
{
self = [super init];
if (self)
{
[self setDelegate:serviceDelegate];
userId = nil;
operationQueue = [[NSMutableArray alloc] init];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
facebook = appDelegate.facebook;
}
return self;
}
- (id)init
{
return [self initWithDelegate:nil];
}
- (void)dealloc
{
[operationQueue release];
[super dealloc];
}
#pragma mark - Instance methods
- (void)login
{
if (![facebook isSessionValid]) {
SEL authorizationSelector = #selector(performAuthorization);
NSValue *selector = [NSValue valueWithPointer:authorizationSelector];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:selector forKey:#"selector"];
[operationQueue addObject:dictionary];
[self runOperations];
}
}
- (void)logout
{
SEL logoutSelector = #selector(performLogout);
NSValue *selector = [NSValue valueWithPointer:logoutSelector];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:selector forKey:#"selector"];
[operationQueue addObject:dictionary];
[self runOperations];
}
- (void)postMessage:(NSString *)message
{
NSArray *objects = [NSArray arrayWithObjects:message, nil];
NSArray *keys = [NSArray arrayWithObjects:#"message", nil];
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys];
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:parameters forKey:#"parameters"];
SEL postMessageSelector = #selector(performPostMessageWithDictionary:);
NSValue *selector = [NSValue valueWithPointer:postMessageSelector];
NSDictionary *operationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:selector, #"selector", dictionary, #"parameters", nil];
[operationQueue addObject:operationDictionary];
if (![facebook isSessionValid]) {
[self performSelectorOnMainThread:#selector(performAuthorization) withObject:nil waitUntilDone:NO];
} else {
[self runOperations];
}
}
#pragma mark - Private methods
- (void)runOperations
{
DLog(#"running operations ...");
for (NSDictionary *operationDictionary in operationQueue) {
NSValue *value = [operationDictionary objectForKey:#"selector"];
SEL selector = [value pointerValue];
NSDictionary *parameters = [operationDictionary objectForKey:#"parameters"];
[self performSelectorOnMainThread:selector withObject:parameters waitUntilDone:YES];
}
[operationQueue removeAllObjects];
}
- (void)performLogout {
[facebook logout:self];
}
- (void)performAuthorization {
NSArray *permissions = [NSArray arrayWithObject:#"publish_stream"];
[facebook authorize:permissions delegate:self];
}
- (void)performPostMessageWithDictionary:(NSDictionary *)dictionary {
NSMutableDictionary *parameters = [dictionary objectForKey:#"parameters"];
NSString *encodedToken = [facebook.accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *graphPath = [NSString stringWithFormat:#"me/feed?access_token=%#", encodedToken];
FBRequest *request = [facebook requestWithGraphPath:graphPath
andParams:parameters
andHttpMethod:#"POST"
andDelegate:self];
if (!request) {
DLog(#"error occured when trying to create FBRequest object with graph path : %#", graphPath);
}
}
/* make sure your interface conforms to the FBRequestDelegate protocol for
extra debug information, but this is not required */
#pragma mark - Facebook request delegate
- (void)requestLoading:(FBRequest *)request
{
DLog(#"requestLoading:");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
{
DLog(#"request:didReceiveResponse:");
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
DLog(#"error occured when trying to perform request to Facebook : %#", error);
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
DLog(#"request:didLoad: %#", result);
}
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
DLog(#"request:didLoadRawResponse");
}
/* make sure your interface conforms to the FBSessionDelegate protocol! */
#pragma mark - Facebook session delegate
/* if there are still operations in the queue that need to be completed,
continue executing the operations, otherwise inform out delegate that login
is completed ... */
- (void)fbDidLogin
{
if ([operationQueue count] > 0) {
[self runOperations];
}
if ([self.delegate respondsToSelector:#selector(serviceDidLogin:)])
{
[self.delegate serviceDidLogin:self];
}
}
- (void)fbDidNotLogin:(BOOL)cancelled
{
if ([self.delegate respondsToSelector:#selector(serviceLoginFailed:)])
{
[self.delegate serviceLoginFailed:self];
}
}
- (void)fbDidLogout
{
if ([self.delegate respondsToSelector:#selector(serviceDidLogout:)])
{
[self.delegate serviceDidLogout:self];
}
}
Make sure you have declared your app's API key.