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
Related
I am using XMPPFramework to implement group chat functionality in my app. The one-to-one chat works fine, but when I join a room by calling [xmppRoom joinRoomUsingNickname], the stream disconnects without giving any error.
I implemented xmppStreamDidDisconnect:withError too, but it's still giving nil error. The user also leaves the room immediately after joining it, since the stream disconnects. I am also using Reconnect module, but when it reconnects, the room is not auto-joined.
I am using pidgin to test it too, but it works fine there. What could be the reason for immediate disconnection?
PS: I am using testing it on iPhone 5 running on iOS 9.1
Update: It's now giving the following error -
Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote
peer" UserInfo={NSLocalizedDescription=Socket closed by remote peer}
Try this modules
MessageManager.h
// Created by Deepak MK on 27/11/15.
//
#import <UIKit/UIKit.h>
#import "XMPP.h"
#import <CoreData/CoreData.h>
#import "XMPPFramework.h"
#import "XMPPMessageDeliveryReceipts.h"
#import "XMPPLastActivity.h"
#import "XMPPRosterMemoryStorage.h"
#import "XMPPRoomMemoryStorage.h"
#import "XMPPvCardCoreDataStorage.h"
#import "XMPPvCardTemp.h"
/**
message manager class manage all message sequence.
*/
#interface MessageManager : NSObject
{
XMPPStream *xmppStream;
NSString *password;
BOOL isOpen;
BOOL isRegistering;
id _delgate;
XMPPRosterCoreDataStorage *xmppRosterStorage;
XMPPRoster *xmppRoster;
XMPPvCardCoreDataStorage* xmppvCardStorage;
XMPPvCardTempModule*xmppvCardTempModule;
}
+ (MessageManager *) sharedMessageHandler;
/**
The stream varible for connecting stream
*/
#property (nonatomic, readonly) XMPPStream *xmppStream;
/**
XMPPRoster varible
*/
#property (nonatomic, strong,readonly) XMPPRoster *xmppRoster;
/**
XMPPRosterCoreDataStorage varible
*/
#property (nonatomic, strong,readonly) XMPPRosterCoreDataStorage *xmppRosterStorage;
/**
XMPPRoom varible
*/
#property (nonatomic, strong, readonly) XMPPRoom *xmppRoom;
/**
Setting of delegate
#param delegate class delegate
*/
- (void) setdelegate:(id)delegate;
/**
Return of delegate
*/
- (id) delegate;
/**
connecting stream of Xmpp server
*/
- (void) setupStream;
/**
Connect user to Xmpp server
#param jabberID login user name
#param myPassword login password
*/
- (BOOL)connectWithUserId:(NSString*)jabberID withPassword:(NSString*)myPassword;
/**
Connect user to Xmpp server
#param userName login user name
#param myPassword login password
*/
- (void) authenticateUserWIthUSerName:(NSString*)userName withPassword:(NSString*)myPassword;
/**
disconnect user from Xmpp server
*/
- (void) disconnect;
/**
changes the presence to online
*/
- (void) goOnline;
/**
changes the presence to offline
*/
- (void) goOffline;
/**
Register new user to xmpp server
#param userName new user name
#param _password new password
#param EmailId new email id
*/
- (void)registerStudentWithUserName:(NSString *)userName withPassword:(NSString *)_password withEmailId:(NSString *)EmailId;
/**
send message to other user with content
#param toAdress destination address
#param content content of message
*/
- (BOOL)sendMessageTo:(NSString*)toAdress withContents:(NSString*)content;
/**
This method is used for sending subscribe invitation to user
#param userID destination address
*/
- (void) sendSubscribeMessageToUser:(NSString*)userID;
/**
This method is used for setting substate of presence
#param subState substate of user
*/
- (void) presenceWithStubState:(NSString*)subState;
/**
This method is used to create new room
#param ChatRoomJID New room name
*/
- (void) setUpRoom:(NSString *)ChatRoomJID;
/**
This method is used to destroyRoom
*/
- (void) destroyCreatedRoom;
/**
This method is used to send message to group
*/
- (BOOL)sendGroupMessageWithBody:(NSString*)_body;
- (void) requestAllMesssage;
#end
/**
Set of methods to be implemented to act as a restaurant patron
*/
#protocol MessageManagerDelegate <NSObject>
/**
Methods to be get state of stream
*/
- (void) didGetStreamState:(BOOL)state;
/**
Methods to be get state of Authentication
*/
- (void) didGetAuthenticationState:(BOOL)state;
/**
Methods to be get state of registration
*/
- (void) didGetRegistrationState:(BOOL)state WithErrorMessage:(NSString*)errorMessage;
/**
Methods to get recieved message
*/
- (void) didReceiveMessageWithBody:(NSString *) body;
/**
Methods to get presence of other user
*/
- (void) didRecievePresence:(NSString*)state withUserName:(NSString*)userName WithSubState:(NSString*)subState;
/**
Methods to get event of user joined room
*/
- (void) didCreatedOrJoinedRoomWithCreatedRoomName:(NSString*)_roomName;
- (void) didGetUserJoinedToRoomORLeaveRoomWithName:(NSString*)_userName WithPresence:(NSString*)presence;
#end
MessageManager.m
#import "MessageManager.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import <CFNetwork/CFNetwork.h>
#if DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_INFO;
#endif
#define kBaseXMPPURL #"XXXXXXXXXX"
#interface MessageManager()
#end
static MessageManager *sharedMessageHandler = nil;
#implementation MessageManager
#synthesize xmppStream;
#synthesize xmppRoster;
#synthesize xmppRosterStorage;
#synthesize xmppRoom;
#pragma mark - self class Delegate
- (void) setdelegate:(id)delegate
{
_delgate= delegate;
}
- (id) delegate
{
return _delgate;
}
#pragma mark - custom Functions
+ (MessageManager *) sharedMessageHandler
{
if (sharedMessageHandler == nil)
{
sharedMessageHandler = [[super allocWithZone:NULL] init];
}
return sharedMessageHandler;
}
+ (id)allocWithZone:(NSZone *)zone {
return [self sharedMessageHandler];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
#pragma mark - connection setup Functions
/**
This fuction is used to setup XMPP Stream
*/
- (void)setupStream
{
// Setup xmpp stream
//
// The XMPPStream is the base class for all activity.
// Everything else plugs into the xmppStream, such as modules/extensions and delegates.
xmppStream = [[XMPPStream alloc] init];
[xmppStream setHostName:kBaseXMPPURL];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
/**
This fuction is used to Connect XMPP With userId and Password
*/
- (BOOL)connectWithUserId:(NSString*)jabberID withPassword:(NSString*)myPassword
{
[self setupStream];
isRegistering=NO;
if (![xmppStream isDisconnected]) {
return YES;
}
if (jabberID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone 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) authenticateUserWIthUSerName:(NSString*)userName withPassword:(NSString*)myPassword
{
if ([xmppStream isConnected])
{
NSError*error =nil;
[xmppStream setMyJID:[XMPPJID jidWithString:userName]];
[xmppStream authenticateWithPassword:myPassword error:&error];
}
else
{
[self connectWithUserId:userName withPassword:myPassword];
}
}
#pragma mark ---Delegate of Connect
/**
This fuction is called when stream is connected
*/
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
isOpen = YES;
NSError *error = nil;
NSLog(#"Stream Connected");
if (!isRegistering)
{
if([[self delegate] respondsToSelector:#selector(didGetStreamState:)])
{
[[self delegate]didGetStreamState:YES];
}
[xmppStream authenticateWithPassword:password error:&error];
}
else
{
[xmppStream registerWithPassword:password error:&error];
}
}
/**
This fuction is called when User is Authenticated
*/
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
[self goOnline];
NSLog(#"Stream Authenticated");
if([[self delegate] respondsToSelector:#selector(didGetAuthenticationState:)])
{
[[self delegate]didGetAuthenticationState:YES];
}
// if ([xmppStream isAuthenticated]) {
// NSLog(#"authenticated");
// xmppvCardStorage = [[XMPPvCardCoreDataStorage alloc] initWithInMemoryStore];
// xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
// [xmppvCardTempModule activate:[self xmppStream]];
// [xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()];
// [xmppvCardTempModule fetchvCardTempForJID:[sender myJID] ignoreStorage:YES];
// }
}
- (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp forJID:(XMPPJID *)jid{
NSLog(#"Delegate is called");
XMPPvCardTemp *vCard = [xmppvCardStorage vCardTempForJID:jid xmppStream:xmppStream];
NSLog(#"Stored card: %#",vCard);
NSLog(#"%#", vCard.description);
NSLog(#"%#", vCard.name);
NSLog(#"%#", vCard.emailAddresses);
NSLog(#"%#", vCard.formattedName);
NSLog(#"%#", vCard.givenName);
NSLog(#"%#", vCard.middleName);
}
/**
This fuction is called when User is not Authenticated
*/
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(#"Not Authenticated");
if([[self delegate] respondsToSelector:#selector(didGetAuthenticationState:)])
{
[[self delegate]didGetAuthenticationState:NO];
}
}
#pragma mark - Stream disconnection
/**
This fuction is used to disconnet user
*/
- (void)disconnect
{
[self goOffline];
[xmppStream disconnect];
}
#pragma mark ---Delegate of disconnect
/**
This fuction is called when stream is disConnected
*/
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
NSLog(#"Stream Disconnected");
if([[self delegate] respondsToSelector:#selector(didGetStreamState:)])
{
[[self delegate]didGetStreamState:NO];
}
}
#pragma mark - setting presence
/**
This fuction is used change the presence to online
*/
- (void)goOnline
{
XMPPPresence *presence = [XMPPPresence presence];
[xmppStream sendElement:presence];
}
/**
This fuction is used change the presence to Ofline
*/
- (void)goOffline
{
XMPPPresence *presence = [XMPPPresence presenceWithType:#"unavailable"];
[xmppStream sendElement:presence];
}
/**
This fuction is used change the presence substate
*/
- (void) presenceWithStubState:(NSString*)subState
{
XMPPPresence *presence = [XMPPPresence presence];// type="available" is implicit
NSXMLElement *status = [NSXMLElement elementWithName:#"status"];
[status setStringValue:subState];
[presence addChild:status];
[xmppStream sendElement:presence];
}
/**
This fuction is called when other user state is changed
*/
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
DDLogVerbose(#"%#: %# - %#", THIS_FILE, THIS_METHOD, [presence fromStr]);
NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
NSString* presenceState= [presence status];
NSLog(#"%# is %# state %#",presenceFromUser,presenceType,presenceState);
if (![presenceFromUser isEqualToString:myUsername])
{
if ([presenceType isEqualToString:#"available"])
{
if([[self delegate] respondsToSelector:#selector(didRecievePresence:withUserName:WithSubState:)])
{
[[self delegate] didRecievePresence:presenceType withUserName:presenceFromUser WithSubState:presenceState];
}
}
else if ([presenceType isEqualToString:#"unavailable"]) {
if([[self delegate] respondsToSelector:#selector(didRecievePresence:withUserName:WithSubState:)])
{
[[self delegate] didRecievePresence:presenceType withUserName:presenceFromUser WithSubState:presenceState];
}
}
else if ([presenceType isEqualToString:#"subscribe"])
{
[xmppRoster subscribePresenceToUser:[presence from]];
[self goOnline];
}
else if ([presenceType isEqualToString:#"subscribed"])
{
[xmppRoster subscribePresenceToUser:[presence from]];
}
}
if (xmppRoom)
{
[xmppRoom fetchMembersList];
}
}
#pragma mark - subscription
- (void) sendSubscribeMessageToUser:(NSString*)userID
{
XMPPJID* jbid= [XMPPJID jidWithString:userID];
XMPPPresence *presence = [XMPPPresence presenceWithType:#"subscribe" to:jbid];
[xmppStream sendElement:presence];
}
#pragma mark - XMPP delegates
/**
This fuction is called when new IQ is received
*/
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
return NO;
}
#pragma mark - RegistrationFunction
/**
This fuction is user to retister new user
if stream is connected the it will directly call registeration function
otherwise it will connect stream and then call registeration process
*/
- (void)registerStudentWithUserName:(NSString *)userName withPassword:(NSString *)_password withEmailId:(NSString *)EmailId
{
if (xmppStream==nil)
{
[self setupStream];
}
[xmppStream setMyJID:[XMPPJID jidWithString:userName]];
NSLog(#"Attempting registration for username %#",xmppStream.myJID.bare);
password=_password;
NSError *error = nil;
BOOL success;
if(![ xmppStream isConnected])
{
success = [[self xmppStream] connectWithTimeout:XMPPStreamTimeoutNone error:&error];
isRegistering=YES;
}
else
{
success = [[self xmppStream] registerWithPassword:_password error:&error];
}
if (success)
{
NSLog(#"succeed ");
isRegistering=YES;
}
else
{
if([[self delegate] respondsToSelector:#selector(didGetRegistrationState:WithErrorMessage:)])
{
[[self delegate]didGetRegistrationState:YES WithErrorMessage:#"Stream not connected"];
}
}
}
#pragma mark ---delegates of registrtaion
/**
This fuction is called when new user is registered
*/
- (void)xmppStreamDidRegister:(XMPPStream *)sender{
if([[self delegate] respondsToSelector:#selector(didGetRegistrationState:WithErrorMessage:)])
{
[[self delegate]didGetRegistrationState:YES WithErrorMessage:#"Registration with XMPP Successful!"];
}
}
/**
This fuction is called when registeration process failed
*/
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error{
// DDXMLElement *errorXML = [error elementForName:#"error"];
// NSString *errorCode = [[errorXML attributeForName:#"code"] stringValue];
// NSString *regError = [NSString stringWithFormat:#"ERROR :- %#",error.description];
//
//
// if([errorCode isEqualToString:#"409"])
// {
// regError=#"Username Already Exists!";
// }
// else
// {
// regError= #"Server not connected";
// }
if([[self delegate] respondsToSelector:#selector(didGetRegistrationState:WithErrorMessage:)])
{
[[self delegate]didGetRegistrationState:NO WithErrorMessage:#"Username Already Exists!"];
}
}
#pragma mark - send and recieve message
/**
This fuction is used to send message to other user with contents of body
*/
//-(void)sendMessageTo:(NSString*)toAdress withContents:(NSString*)messageStr
//{
//
//
// if([messageStr length]> 0)
// {
//
// NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
// [body setStringValue:messageStr];
//
// NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
// [message addAttributeWithName:#"type" stringValue:#"chat"];
// [message addAttributeWithName:#"to" stringValue:toAdress];
// [message addChild:body];
//
// [self.xmppStream sendElement:message];
// }
//}
- (BOOL)sendMessageTo:(NSString*)toAdress withContents:(NSString*)content
{
if([content length]> 0)
{
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:content];
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:toAdress];
[message addChild:body];
[self.xmppStream sendElement:message];
}
return YES;
}
#pragma mark recieve message
/**
This fuction is called when new message arrived
*/
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
// A simple example of inbound message handling.
if ([message body])
{
NSString *body = [[message elementForName:#"body"] stringValue];
if([[self delegate] respondsToSelector:#selector(didReceiveMessageWithBody:)])
{
[[self delegate] didReceiveMessageWithBody:body];
}
}
}
#pragma mark - create new room
/**
This fuction is used to setup room with roomId
*/
-(void)setUpRoom:(NSString *)ChatRoomJID
{
if (!ChatRoomJID)
{
return;
}
// Configure xmppRoom
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPJID *roomJID = [XMPPJID jidWithString:ChatRoomJID];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage jid:roomJID dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
NSXMLElement *history = [NSXMLElement elementWithName:#"history"];
[history addAttributeWithName:#" maxchars" stringValue:#"0"];
[xmppRoom joinRoomUsingNickname:xmppStream.myJID.user
history:history
password:nil];
[self performSelector:#selector(ConfigureNewRoom:) withObject:nil afterDelay:4];
}
/**
This fuction is used configure new
*/
- (void)ConfigureNewRoom:(id)sender
{
[xmppRoom configureRoomUsingOptions:nil];
[xmppRoom fetchConfigurationForm];
[xmppRoom fetchBanList];
[xmppRoom fetchMembersList];
[xmppRoom fetchModeratorsList];
}
/**
This fuction is called when new room is created
*/
- (void)xmppRoomDidCreate:(XMPPRoom *)sender
{
DDLogInfo(#"%#: %#", THIS_FILE, THIS_METHOD);
// I am inviting friends after room is created
}
/**
This fuction is called when user joined room
*/
- (void)xmppRoomDidJoin:(XMPPRoom *)sender
{
[sender fetchMembersList];
[sender fetchConfigurationForm];
[self requestAllMesssage];
DDLogInfo(#"%#: %#", THIS_FILE, THIS_METHOD);
if([[self delegate] respondsToSelector:#selector(didCreatedOrJoinedRoomWithCreatedRoomName:)])
{
[[self delegate] didCreatedOrJoinedRoomWithCreatedRoomName:sender.myRoomJID.bare];
}
}
- (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items
{
}
- (void)xmppRoom:(XMPPRoom *)sender occupantDidJoin:(XMPPJID *)occupantJID withPresence:(XMPPPresence *)presence
{
if([[self delegate] respondsToSelector:#selector(didGetUserJoinedToRoomORLeaveRoomWithName:WithPresence:)])
{
// id details =occupantJID;
// NSString* string = (NSString*)details;
[[self delegate] didGetUserJoinedToRoomORLeaveRoomWithName:[occupantJID resource] WithPresence:[presence type]];
}
}
- (void)xmppRoom:(XMPPRoom *)sender occupantDidLeave:(XMPPJID *)occupantJID withPresence:(XMPPPresence *)presence
{
if([[self delegate] respondsToSelector:#selector(didGetUserJoinedToRoomORLeaveRoomWithName:WithPresence:)])
{
[[self delegate] didGetUserJoinedToRoomORLeaveRoomWithName:[occupantJID resource] WithPresence:[presence type]];
}
}
- (void)xmppRoom:(XMPPRoom *)sender occupantDidUpdate:(XMPPJID *)occupantJID withPresence:(XMPPPresence *)presence
{
if([[self delegate] respondsToSelector:#selector(didGetUserJoinedToRoomORLeaveRoomWithName:WithPresence:)])
{
[[self delegate] didGetUserJoinedToRoomORLeaveRoomWithName:[occupantJID resource] WithPresence:[presence type]];
}
}
- (void) requestAllMesssage
{
// <presence
// from='hag66#shakespeare.lit/pda'
// id='n13mt3l'
// to='coven#chat.shakespeare.lit/thirdwitch'>
// <x xmlns='http://jabber.org/protocol/muc'>
// <history since='1970-01-01T00:00:00Z'/>
// </x>
// </presence>
//
// NSXMLElement *iQ = [NSXMLElement elementWithName:#"presence"];
// [iQ addAttributeWithName:#"type" stringValue:#"get"];
// [iQ addAttributeWithName:#"id" stringValue:#"n13mt3l"];
//
// NSXMLElement *retrieve = [NSXMLElement elementWithName:#"retrieve"];
// [retrieve addAttributeWithName:#"xmlns" stringValue:#"urn:xmpp:archive"];
// [retrieve addAttributeWithName:#"history since" stringValue:#"1970-01-01T00:00:00Z"];
//
// NSXMLElement *set = [NSXMLElement elementWithName:#"set"];
// [set addAttributeWithName:#"xmlns" stringValue:#"http://jabber.org/protocol/muc"];
//
// [retrieve addChild:set];
// [iQ addChild:retrieve];
//
// [xmppStream sendElement:iQ];
}
- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
{
DDLogInfo(#"%#: %#", THIS_FILE, THIS_METHOD);
NSXMLElement *newConfig = [configForm copy];
NSArray *fields = [newConfig elementsForName:#"field"];
for (NSXMLElement *field in fields)
{
NSString *var = [field attributeStringValueForName:#"var"];
// Make Room Persistent
if ([var isEqualToString:#"muc#roomconfig_persistentroom"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:#"value" stringValue:#"1"]];
}
if ([var isEqualToString:#"roomconfig_enablelogging"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:#"value" stringValue:#"1"]];
}
if ([var isEqualToString:#"muc#roomconfig_maxusers"])
{
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:#"value" stringValue:#"100"]];
}
}
// [sender configureRoomUsingOptions:newConfig];
}
/**
This fuction is used to destroy created room
*/
- (void) destroyCreatedRoom
{
[xmppRoom destroyRoom];
}
- (BOOL)sendGroupMessageWithBody:(NSString*)_body
{
[xmppRoom sendMessageWithBody:_body];
return YES;
}
#end
Generally when the server is closing the connection, you get this error.
There are two reasons when the server closes the connection:
You are not sending regular pings if the client idle.
You are logging in from some different client with the same credentials,
and in the server settings have the setting:
Always kick - If there is a resource conflict, immediately kick the other
resource. in Server>server settings>resource policy.
Please check the server settings.
use this for server setting http://docs.ejabberd.im/admin/guide/installation/
And also check for App Transport Security Setting in your project. if it is not added then add the below code to info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</plist>
The actual problem was that I was changing subject of the Room, just after creating the room. On removing the code for changing the subject of the room, it started working fine. Here's the code I was using -
-(XMPPRoom *)createRoom:(XMPPJID *)roomJid withSubject:(NSString*)subject{
XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc]init];
XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
jid:roomJid
dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:self.xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
// if(subject){
// [xmppRoom changeRoomSubject:subject];
// }
return xmppRoom;
}
-(GroupCore *)createRoomAndGroup:(XMPPJID *)roomJid withSubject:(NSString*)subject{
XMPPRoom* xmppRoom = [self createRoom:roomJid withSubject:subject];
GroupCore* group = [appDelegate.coreDataController addGroup:xmppRoom withOptions:CreateOnly];
if(!xmppRoom.roomSubject){
group.name = subject;
}
//[appDelegate saveContext];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// [group joinRoom];
// });
return group;
}
Currently i am working on XMPP protocol in ios for chatting application. i want to put alert view and authentication on Login button. if authentication will successful then user can see homescreen screen else alert view will appear please check username and password i show chat secure open source project but i can't understand.
//appdelegate.m file .//
- (BOOL)connect
{
if (![xmppStream isDisconnected]) {
return YES;
// isauthenticate=YES;
}
NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];
//
// If you don't want to use the Settings view to set the JID,
// uncomment the section below to hard code a JID and password.
//
// myJID = #"user#gmail.com/xmppframework";
// myPassword = #"";
if (myJID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error connecting"
message:#"See console for error details."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
DDLogError(#"Error connecting: %#", error);
return NO;
}
return YES;
}
- (void)disconnect
{
[self goOffline];
[xmppStream disconnect];
}
#pragma mark UIApplicationDelegate
- (void)applicationDidEnterBackground:(UIApplication *)application
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
#if TARGET_IPHONE_SIMULATOR
DDLogError(#"The iPhone simulator does not process background network traffic. "
#"Inbound traffic is queued until the keepAliveTimeout:handler: fires.");
#endif
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:600 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
- (void)applicationWillTerminate:(UIApplication *)application
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
[self teardownStream];
}
#pragma mark XMPPStream Delegate
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
NSString *expectedCertName = [xmppStream.myJID domain];
if (expectedCertName)
{
settings[(NSString *) kCFStreamSSLPeerName] = expectedCertName;
}
if (customCertEvaluation)
{
settings[GCDAsyncSocketManuallyEvaluateTrust] = #(YES);
}
}
- (void)xmppStream:(XMPPStream *)sender didReceiveTrust:(SecTrustRef)trust
completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
// The delegate method should likely have code similar to this,
// but will presumably perform some extra security code stuff.
// For example, allowing a specific self-signed certificate that is known to the app.
dispatch_queue_t bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(bgQueue, ^{
SecTrustResultType result = kSecTrustResultDeny;
OSStatus status = SecTrustEvaluate(trust, &result);
if (status == noErr && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
completionHandler(YES);
}
else {
completionHandler(NO);
}
});
}
- (void)xmppStreamDidSecure:(XMPPStream *)sender
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
isXmppConnected = YES;
NSError *error = nil;
if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
DDLogError(#"Error authenticating: %#", error);
}
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
isauthenticate=YES;
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
[self goOnline];
}
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
i have tried with setting BOOL(isauthenticate) in xmppStreamDidAuthenticate method but not success. i can redirect to homepage but if i wrote wrong detail still it will redirect to homepage. i want to set it if username or password are wrong and did not authenticate by server.
//view controller.m file //
#import "ViewController.h"
#import "AppDelegate.h"
#interface ViewController ()<MBProgressHUDDelegate>
{
MBProgressHUD *HUD;
IBOutlet UITextField *mViewEmail;
IBOutlet UITextField *mViewPassword;
}
#end
NSString *const kXMPPmyJID = #"kXMPPmyJID";
NSString *const kXMPPmyPassword = #"kXMPPmyPassword";
#implementation ViewController
- (IBAction)checkLogin:(id)sender {
NSLog(#"Email: %# Password: %#",mViewEmail.text,mViewPassword.text);
[self setField:mViewEmail forKey:kXMPPmyJID];
[self setField:mViewPassword forKey:kXMPPmyPassword];
// if (appdelegate.connect==YES) {
if([ [self appDelegate] connect]) {
// if (appdelegate.isauthenticate==YES) {
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] ;
HUD.delegate = self;
HUD.color = [UIColor blackColor];
HUD.labelText = #"Please Wait";
HUD.dimBackground = YES;
// HUD.detailsLabelText = #"Close chat";
[self showHome];
//}
}
//}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"Please Check Username or Password"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
- (void)showHome{
[self performSegueWithIdentifier:#"signIn" sender:self];
}
ok. so finaly my problem is solved. and i am posting my answer. first of all my mistake is i forgot to write
appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appdelegate.viewController = self; // very imp when you call method in app delegate.h
in my view controller. so when i call viewcontroller's method in appdelegate it call's but do not execute due to above method is missing (Second line). and then i have call view controller's segue method in - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender method. and then work perfectly. so my final solution is
//app delegate.m file//
- (BOOL)connect
{
// Setup HUD(Activity Indicator) when Connect method call //
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow]; [self.window.rootViewController.view addSubview:HUD];
[HUD setDetailsLabelText:#"Please wait..."];
[HUD setDimBackground:YES];
[HUD setOpacity:0.5f];
[HUD show:YES];
if (![xmppStream isDisconnected]) {
return YES;
}
NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];
if (myJID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
{
HUD.hidden=YES;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error connecting"
message:#"See console for error details."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
DDLogError(#"Error connecting: %#", error);
return NO;
}
return YES;
}
- (void)disconnect
{
[self goOffline];
[xmppStream disconnect];
}
#pragma mark UIApplicationDelegate
- (void)applicationDidEnterBackground:(UIApplication *)application
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
#if TARGET_IPHONE_SIMULATOR
DDLogError(#"The iPhone simulator does not process background network traffic. "
#"Inbound traffic is queued until the keepAliveTimeout:handler: fires.");
#endif
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:600 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
- (void)applicationWillTerminate:(UIApplication *)application
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
[self teardownStream];
}
#pragma mark XMPPStream Delegate
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
NSString *expectedCertName = [xmppStream.myJID domain];
if (expectedCertName)
{
settings[(NSString *) kCFStreamSSLPeerName] = expectedCertName;
}
if (customCertEvaluation)
{
settings[GCDAsyncSocketManuallyEvaluateTrust] = #(YES);
}
}
- (void)xmppStream:(XMPPStream *)sender didReceiveTrust:(SecTrustRef)trust
completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
dispatch_queue_t bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(bgQueue, ^{
SecTrustResultType result = kSecTrustResultDeny;
OSStatus status = SecTrustEvaluate(trust, &result);
if (status == noErr && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
completionHandler(YES);
}
else {
completionHandler(NO);
}
});
}
- (void)xmppStreamDidSecure:(XMPPStream *)sender
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
isXmppConnected = YES;
NSError *error = nil;
if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
DDLogError(#"Error authenticating: %#", error);
}
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
HUD.hidden=YES; //Hud Will be hide when User Authenticated
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
[self.viewController showHome]; // view controllers method to go to next view controller //
[self goOnline];
}
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
// HUD will be hidden and alertview will be shown //
HUD.hidden=YES;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry"
message:#"Please Check Username or Password"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
isauthenticate=NO;
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
{
NSLog(#"error = %#", error);
}
}
and in viewcontroller.m we have show that viewcontroller is also appdelegate controller. so we have to declare it in - (void)viewDidLoad method.
// viewcontroller.m file//
- (void)viewDidLoad
{
[super viewDidLoad];
appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appdelegate.viewController = self;
}
- (AppDelegate *)appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (IBAction)checkLogin:(id)sender {
[self dismissKeyboard];
NSLog(#"Email: %# Password: %#",mViewEmail.text,mViewPassword.text);
[self setField:mViewEmail forKey:kXMPPmyJID];
[self setField:mViewPassword forKey:kXMPPmyPassword];
[[self appDelegate ]connect];//call when loginbutton pressed
}
//call in appdelegate.h // segue(push) to next view
- (void)showHome
{
[self performSegueWithIdentifier:#"signIn" sender:self];
}
Note: I have tried to explain as much as i can.
If username and password are correct then xmppStreamDidAuthenticate method call and if one of them is incorrect then didNotAuthenticate method call.
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
}
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(#"error = %#", error);
}
Here print error and display this error in UIALertview as you required.
Actually you are checking XMPP connection that is not means Authenticate User. if your User Name and password is correct then you received response in xmppStreamDidAuthenticate method. where you can write the code to push to HomeViewController once user is verified.
Below two methods, you can used to show alert or push to another view once user varified.
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
}
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(#"Error");
}
this is XMPP Delegate methods
Hope this help you.
I develop the iOS XMPP app with the tutorial in this link >>> http://code.tutsplus.com/tutorials/building-a-jabber-client-for-ios-xmpp-setup--mobile-7190
And now I run my project it is not connecting to my openfire server but there are no errors displaying. I heard some developers they had solve relevant problem by "enabling" SSL certificate in iOS client in this link >>> Unable to connect openfire server using ios client. Do I have to enable it? and how to enable it?
This problem had takes me a week so I decided to post on stackoverflow.
here is my "JabberClientAppDelegate.m"
//
// AppDelegate.m
// JabberClient
//
// Created by Lance on 7/30/14.
// Copyright (c) 2014 Lance. All rights reserved.
//
#import "JabberClientAppDelegate.h"
#import "SMBuddyListViewController.h"
#interface JabberClientAppDelegate()
- (void)setupStream;
- (void)goOnline;
- (void)goOffline;
#end
#implementation JabberClientAppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#synthesize chatDelegate;
#synthesize messageDelegate;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
SMBuddyListViewController *masterViewController = [[SMBuddyListViewController alloc] initWithNibName:#"SMBuddyListViewController" bundle:nil];
self.viewController = masterViewController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
// connection to the server successful
isOpen = YES;
NSError *error = nil;
[[self xmppStream] authenticateWithPassword:password error:&error];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
// authentication successful
[self goOnline];
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
// message received
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 {
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)setupStream {
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
- (void)applicationWillResignActive:(UIApplication *)application {
[self disconnect];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[xmppStream setHostName:#"localhost"];
[xmppStream setHostPort:5222];
}
- (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 *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:#"userID"];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:#"userPassword"];
if (![xmppStream isDisconnected]) {
return YES;
}
if (jabberID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:[NSString stringWithFormat:#"Can't connect to server %#", nil]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
return NO;
}
if(![xmppStream isConnected]){
NSLog(#"connected");
}
//[[self xmppStream] authenticateAnonymously:&error];
[self xmppStreamDidAuthenticate:xmppStream];
return YES;
}
- (void)disconnect {
[self goOffline];
[xmppStream disconnect];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"JabberClient" withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"JabberClient.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
#{NSMigratePersistentStoresAutomaticallyOption:#YES, NSInferMappingModelAutomaticallyOption:#YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#end
I am sorry if there are too much codes to review, I am a beginner of iOS development and XMPP, please kindly review my mistake and guide me how to fix I'll be truly appreciate, thank you.
You forget to set port number and host name of server.
Try this.
- (BOOL)connect {
[self setupStream];
// set openfire server host name and port number to connect
[self.xmppStream setHostName:#"serverHostName"];
[self.xmppStream setHostPort:#"serverPortNumber"];
NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:#"userID"];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:#"userPassword"];
if (![xmppStream isDisconnected]) {
return YES;
}
if (jabberID == nil || myPassword == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:[NSString stringWithFormat:#"Can't connect to server %#", nil]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
return NO;
}
return YES;
}
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.
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.