I have developed the XMPP Chat client for iOS and now I'm researching for how to do a new user registration from iOS itself. Can anyone help with the methods used to register a new user. As it needs to communicate with the Server and store the username and password to the server database. Please help I'm searching it from 2 days.
NSMutableArray *elements = [NSMutableArray array];
[elements addObject:[NSXMLElement elementWithName:#"username" stringValue:#"venkat"]];
[elements addObject:[NSXMLElement elementWithName:#"password" stringValue:#"dfds"]];
[elements addObject:[NSXMLElement elementWithName:#"name" stringValue:#"eref defg"]];
[elements addObject:[NSXMLElement elementWithName:#"accountType" stringValue:#"3"]];
[elements addObject:[NSXMLElement elementWithName:#"deviceToken" stringValue:#"adfg3455bhjdfsdfhhaqjdsjd635n"]];
[elements addObject:[NSXMLElement elementWithName:#"email" stringValue:#"abc#bbc.com"]];
[[[self appDelegate] xmppStream] registerWithElements:elements error:nil];
We will know whether the registration is successful or not using the below delegates.
- (void)xmppStreamDidRegister:(XMPPStream *)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Registration" message:#"Registration Successful!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (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];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Registration Failed!" message:regError delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
if([errorCode isEqualToString:#"409"]){
[alert setMessage:#"Username Already Exists!"];
}
[alert show];
}
This solution HAS WORKED for me
NSString *username = #"rohit#XMPP_SERVER_IP_HERE"; // OR [NSString stringWithFormat:#"%##%#",username,XMPP_BASE_URL]]
NSString *password = #"SOME_PASSWORD";
AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
del.xmppStream.myJID = [XMPPJID jidWithString:username];
NSLog(#"Does supports registration %ub ", );
NSLog(#"Attempting registration for username %#",del.xmppStream.myJID.bare);
if (del.xmppStream.supportsInBandRegistration) {
NSError *error = nil;
if (![del.xmppStream registerWithPassword:password error:&error])
{
NSLog(#"Oops, I forgot something: %#", error);
}else{
NSLog(#"No Error");
}
}
// You will get delegate called after registrations in either success or failure case. These delegates are in XMPPStream class
// - (void)xmppStreamDidRegister:(XMPPStream *)sender
//- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
New user can register at XMPP server from iOS by two methods as
Methode 1.) By In band Registration (In-band registration means that users that do not have an account on your server can register one using the XMPP protocol itself, so the registration stays "in band", inside the same protocol you're already using.) you have to use XEP-0077 extension.
And your server should also have to support In band Registration.
Use these steps for In band Registration
step 1: connect with xmppStream
- (BOOL)connectAndRegister
{
if (![xmppStream isDisconnected]) {
return YES;
}
NSString *myJID = #"abc#XMPP_SERVER_IP_HERE"; // OR [NSString stringWithFormat:#"%##%#",username,XMPP_BASE_URL]]
NSString *myPassword = #"SOME_PASSWORD";
//
// 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.
//
// Replace me with the proper JID and password:
// myJID = #"user#gmail.com/xmppframework";
// myPassword = #"";
if (myJID == nil || myPassword == nil) {
DDLogWarn(#"JID and password must be set before connecting!");
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
password = myPassword;
NSError *error = nil;
if (![xmppStream connect:&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;
}
NSString *password declare at #interface part of your file
step 2: When xmppStream Delegate - (void)xmppStreamDidConnect:(XMPPStream *)sender call
step 3: Start register via In-Band Registration as
- (void)xmppStreamDidConnect:(XMPPStream *)sender{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
[[NSNotificationCenter defaultCenter] postNotificationName:XMPPStreamStatusDidConnectNotification
object:nil
userInfo:nil];
_isXmppConnected = YES;
NSError *error = nil;
DDLogVerbose(#"Start register via In-Band Registration...");
if (xmppStream.supportsInBandRegistration) {
if (![xmppStream registerWithPassword:password error:&error]) {
NSLog(#"Oops, I forgot something: %#", error);
}else {
NSLog(#"No Error");
}
}
// [_xmppStream authenticateWithPassword:password error:&error];
}
step 4: Check registration success or failure by XMPPStream delegate
- (void)xmppStreamDidRegister:(XMPPStream *)sender
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
Methode 2.) By XMPP Rest Api on the openFire server installed a plugin (Rest Api plugin) that allows normal registration.
Use these steps for Rest Api Registration
step 1: Install Rest Api plugin on server
step 2: Configure server for Rest Api as server -> server settings -> Rest Api then enable it.
You can use "Secret key auth" for secure user registration, so copy that from openfire server and use when rest api is called for registration.
step 3: Call Rest Api for registration
-(void)CreateUserAPI
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"abc",#"username",#"SOME_PASSWORD",#"password",#"abc-nickname",#"name",#"abc#example.com",#"email", nil];
NSData* RequestData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:[NSString stringWithFormat:#"%#users",RESTAPISERVER]]];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:AuthenticationToken forHTTPHeaderField:#"Authorization"];
[request setHTTPBody: RequestData];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if (!error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if ([httpResponse statusCode]==201)
{
NSLog(#"Registration Successful");
}else
{
NSLog(#"Registration failed");
}
}else
{
NSLog(#"Try again for registration");
}
}] resume];
}
RESTAPISERVER is a Rest api url string.
AuthenticationToken is a "Secret key auth" (copy from openfire server)
Related
I am trying to make an XMPP LogIn and a Registration Page Separate one from another, the server is open fire based. Also I am NOT using inbound registration. I would like to underline that this is my first attempt to do something like this, my previous experience involves only working with NSSession to make a registration/ login. I want to say that I bought and read the Mastering The XMPP Framework book witted by Peter van de Put, but he explains only how to do it for an inbound registration/ login.
Also other links that I come across:
1 - similar answers between them
XMPP Aklesh Rathaur answer
XMPP Diego answer
2 - tutorial
PS: The information that I need to send to the server when I sign up is: email, name and password when created
and when I sign in: email, password
Building a Jabber Client for iOS
in this tutorial I don't understand how does the login take place since it only saves the users credentials in the NSUserDefaults.
You can download what I have implemented here (using an stackoverflow suggestion):
my code so far and database structure picture
UPDATE, this is the code that I am trying to use for registration:
-> .h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "XMPP.h"
#import "XMPPRoster.h"
#interface SignUpViewController : UIViewController <UITextFieldDelegate, UIApplicationDelegate, XMPPRosterDelegate, XMPPStreamDelegate>
{
XMPPStream *xmppStream;
}
#property (nonatomic, strong, readonly) XMPPStream *xmppStream;
#end
-> .m file
- (void)signUpButtonFunction{
NSLog(#"SignUp function");
[[self xmppStream] setHostName:#"IP_ADDRESS"];
[[self xmppStream] setHostPort:5222];
XMPPJID *jid=[XMPPJID jidWithString:emailTextField.text];
[[self xmppStream] setMyJID:jid];
[[self xmppStream] connectWithTimeout:3.0 error:nil];
NSMutableArray *elements = [NSMutableArray array];
[elements addObject:[NSXMLElement elementWithName:#"username" stringValue:#"venkat"]];
[elements addObject:[NSXMLElement elementWithName:#"password" stringValue:#"dfds"]];
[elements addObject:[NSXMLElement elementWithName:#"name" stringValue:#"eref defg"]];
[elements addObject:[NSXMLElement elementWithName:#"email" stringValue:#"abc#bbc.com"]];
[ xmppStream registerWithElements:elements error:nil];
}
//server connect delegate methods are not working at least it doesn't enter in them
- (void)xmppStreamDidRegister:(XMPPStream *)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Registration" message:#"Registration Successful!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (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];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Registration Failed!" message:regError delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
if([errorCode isEqualToString:#"409"]){
[alert setMessage:#"Username Already Exists!"];
}
[alert show];
}
So, after search on the matter I found out that on the openFire can be installed a plugin that allows normal registration, so I have implemented the next method for the registration:
NSString *urlToCall = #"http://MyIP:9090/plugins/userService/userservice?type=add&secret=BigSecretKey&username=testUser&password=testPass&name=testName&email=test#gmail.com";
NSURL *url = [NSURL URLWithString:urlToCall];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:#"GET"];
NSError *error = nil;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
if ([responseString isEqual: #"<result>ok</result>\r\n"]) {
NSLog(#"user created");
} else {
NSLog( #"user NOT created");
NSLog(#"%#",responseString);
}
I am having trouble creating a folder in box.com Here is my code. I am having trouble with foldersManager createFolderWithRequestBuilder If I breakpoint on the return I get "op = POST https://api.box.com/2.0/folders"
- (void)boxAPIAuthenticationDidSucceed:(NSNotification *)notification
{
NSLog(#"Received OAuth2 successfully authenticated notification");
BoxOAuth2Session *session = (BoxOAuth2Session *) [notification object];
NSLog(#"Access token (%#) expires at %#", session.accessToken, session.accessTokenExpiration);
NSLog(#"Refresh token (%#)", session.refreshToken);
dispatch_sync(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
BoxFolderBlock success = ^(BoxFolder *folder)
{
[self fetchFolderItemsWithFolderID:self.folderID name:self.navigationItem.title];
};
BoxAPIJSONFailureBlock failure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary)
{
NSLog(#"folder create failed with error code: %i", response.statusCode);
if (response.statusCode == 409)
{
dispatch_sync(dispatch_get_main_queue(), ^{
UIAlertView *conflictAlert = [[UIAlertView alloc] initWithTitle:#"Name conflict" message:[NSString stringWithFormat:#"A folder already exists with the name %#.\n\nNew name:", #"ezMedRecords"] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
conflictAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[conflictAlert show];
});
}
};
BoxFoldersRequestBuilder *builder = [[BoxFoldersRequestBuilder alloc] init];
builder.name = #"ezMedRecords";
builder.parentID = self.folderID;
BoxAPIJSONOperation *op;
op = [[BoxSDK sharedSDK].foldersManager createFolderWithRequestBuilder:builder success:success failure:failure];
return;
}
What is self.folderID?
It should be a valid folderID of the folder that exists on Box.
To save to "all files", start with builder.parentID = #"0";
BTW, what is error message that you get in your failure block?
Does failure block execute at all?
I'm talking about this line:
NSLog(#"folder create failed with error code: %i", response.statusCode);
I want to send the messages and want to post on facebook and twitter in background is there any possible framework is there to do this.Can any one share the idea please.Thanks in advance .
To post on Twitter in background use this library https://github.com/nst/STTwitter
You will need to register your app on Twitter to receive consumer key and consumer secret. After you have it just use the code represented here Accessing Twitter Direct Messages using SLRequest iOS. This code uses system Twitter credentials, user have to set these credentials in iPhone / iPad settings.
Regarding to post on Facebook: I found no wrapper for iOS which can provide possibility to post in background. I use official Facebook iOS SDK is here https://github.com/facebook/facebook-ios-sdk. But post functionality works only with user's participation.
For facebook make sure you have the user permission or sdk setup properly. I would recommend to go through this tutorial for details.
http://m-farhan.com/2014/03/ios-facebook-sdk-tutorial/
- (void)requestPermissionAndPost {
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObjects:#"publish_actions", #"publish_checkins",nil]
defaultAudience:FBSessionDefaultAudienceEveryone
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// Now have the permission
[self postOpenGraphAction];
} else {
// Facebook SDK * error handling *
// if the operation is not user cancelled
if (error.fberrorCategory != FBErrorCategoryUserCancelled) {
[self presentAlertForError:error];
}
}
}];
}
- (void)postOpenGraphAction
{
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error) {
// output the results of the request
[self requestCompleted:connection forFbID:#"me" result:result error:error];
};
UIImage *img = imageView.image;
NSString *message = #"Your Message";
FBRequest *request=[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:#"me/photos" parameters:[NSDictionary dictionaryWithObjectsAndKeys:UIImageJPEGRepresentation(img, 0.7),#"source",message,#"message",#"{'value':'EVERYONE'}",#"privacy", nil] HTTPMethod:#"POST"];
[newConnection addRequest:request completionHandler:handler];
[self.requestConnection cancel];
self.requestConnection = newConnection;
[newConnection start];
}
// FBSample logic
// Report any results. Invoked once for each request we make.
- (void)requestCompleted:(FBRequestConnection *)connection
forFbID:fbID
result:(id)result
error:(NSError *)error
{
NSLog(#"request completed");
// not the completion we were looking for...
if (self.requestConnection &&
connection != self.requestConnection)
{
NSLog(#" not the completion we are looking for");
return;
}
// clean this up, for posterity
self.requestConnection = nil;
if (error)
{
NSLog(#" error");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"error" message:error.localizedDescription delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
// error contains details about why the request failed
[alert show];
}
else
{
NSLog(#" ok");
NSLog(#"%#",result);
/*[[[UIAlertView alloc] initWithTitle:#"Result"
message:[NSString stringWithFormat:#"Posted Open Graph action, id: %#",
[result objectForKey:#"id"]]
delegate:nil
cancelButtonTitle:#"Thanks!"
otherButtonTitles:nil]
show];*/
[self doCheckIn];
};
}
fro Twitter its simple and easy
#import <Twitter/Twitter.h>
- (void)TWPostImage:(UIImage *)image withStatus:(NSString *)status
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
//ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
ACAccount *ac;
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
int i=0;
for (ACAccount *account in accountsArray ) {
i++;
NSLog(#"Account name: %#", account.username);
ac=account;
}
if (i==0) {
[[[UIAlertView alloc] initWithTitle:#"Wait"
message:#"Please setup Twitter Account Settigns > Twitter > Sign In "
delegate:nil
cancelButtonTitle:#"Thanks!"
otherButtonTitles:nil]
show];
return ;
}
ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];;
//SLRequestHandler requestHandler;
SLRequestHandler requestHandler =
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (responseData) {
NSInteger statusCode = urlResponse.statusCode;
if (statusCode >= 200 && statusCode < 300) {
NSDictionary *postResponseData =
[NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:NULL];
NSLog(#"[SUCCESS!] Created Tweet with ID: %#", postResponseData[#"id_str"]);
ESAppDelegate* d =[[UIApplication sharedApplication] delegate];
NSString* link = [NSString stringWithFormat:#"Your Message",twitterAccStore,hashtagFromStore];
[d linkSelected:link PointerToSelf:self];
/*[[[UIAlertView alloc] initWithTitle:#"Result"
message:[NSString stringWithFormat:#"[SUCCESS!] Created Tweet with ID: %#", postResponseData[#"id_str"]]
delegate:nil
cancelButtonTitle:#"Thanks!"
otherButtonTitles:nil]
show];*/
}
else {
NSLog(#"[ERROR] Server responded: status code %d %#", statusCode,
[NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
}
}
else {
NSLog(#"[ERROR] An error occurred while posting: %#", [error localizedDescription]);
}
});
};
//});
ACAccountStoreRequestAccessCompletionHandler accountStoreHandler =
^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com"
#"/1.1/statuses/update_with_media.json"];
NSDictionary *params = #{#"status" : status};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
NSData *imageData = UIImageJPEGRepresentation(image, 1.f);
[request addMultipartData:imageData
withName:#"media[]"
type:#"image/jpeg"
filename:#"image.jpg"];
[request setAccount:[accounts lastObject]];
[request performRequestWithHandler:requestHandler];
//});
}
else {
NSLog(#"[ERROR] An error occurred while asking for user authorization: %#",
[error localizedDescription]);
}
};
[accountStore requestAccessToAccountsWithType:twitterType
options:NULL
completion:accountStoreHandler];
}else
{
[[[UIAlertView alloc] initWithTitle:#"Wait"
message:#"Please Settigns > Twitter > In bottom Enable DealsHype to post"
delegate:nil
cancelButtonTitle:#"Thanks!"
otherButtonTitles:nil]
show];
}
}];
}
In my application i Successfully Registered the User in openfire from the use of XMPP.
but when i Registered the User for first time it is coming offline.
and I have to Connect it again.
After disConnect and connect again it is working Correct.
I don't know why this happen.
My Code is
-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
NSLog(#"Did Connected pw:%#",_myTextViewCustom.text);
isOpen = YES;
NSError *error = nil;
[xmppStream authenticateWithPassword:_myTextViewCustom.text error:&error];
}
-(void)xmppStreamDidRegister:(XMPPStream *)sender
{
NSLog(#"xmppStreamDidRegister Method");
XMPPPresence *presence = [XMPPPresence presence];
[xmppStream sendElement:presence];
[xmppStream setMyJID:[XMPPJID jidWithString:[NSString stringWithFormat:#"%##server_Id",_jidTextView.text]]];
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];
}
else
{
NSLog(#"Connection is in making");
}
[self goOnline];
}
- (void)goOnline {
XMPPPresence *presence = [XMPPPresence presence];
[xmppStream sendElement:presence];
XMPPSystemInputActivityMonitor *activiyMonitor = [[XMPPSystemInputActivityMonitor alloc]init];
activiyMonitor.inactivityTimeInterval = 1;
[activiyMonitor addDelegate:self delegateQueue:dispatch_get_main_queue()];
NSLog(#"Active : %d",[activiyMonitor isActive]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Successfull!!!"
message:[NSString stringWithFormat:#"Connected"]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}
Any types of Help will be great.
Thank you...
Once registration done. You need to Authenticate your self to openfire server.
[[self xmppStream] authenticateWithPassword:passwordString error:&error];
Once you call this method It will respond to
// get called on authenitcation successful
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
and
//Error while authenticating
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
And then
[self goOnline];
I solved this issue but not sure it's perfect or not so if you find another one then please tell me here.
i did change in
-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
NSLog(#"didNotAuthenticate Method");
if(isRegister == TRUE)
{
[xmppStream registerWithPassword:_myTextViewCustom.text error:nil];
NSError * err = nil;
if(![xmppStream registerWithPassword:_myTextViewCustom.text error:&err])
{
NSLog(#"Error registering: %#", err);
[self performSelector:#selector(connect1) withObject:nil afterDelay:2.0];
}
}
}
-(void)connect1
{
isOpen = YES;
NSError *error = nil;
[xmppStream authenticateWithPassword:_myTextViewCustom.text error:&error];
}
I am calling Authentication again and it works but i don't think it's the final solution.
Hi I am using facebook SDK 3.8 in my project. and using HelloFaceBookSample Code in my app but in my app i have no login button of facebook. I have implemented login flow of facebook and after login i have post on facebook. Now Post Status working fine but when i post image on facebook it give me error of
an attempt was made reauthorize permissions on an unopened session in ios
Code :
-(void) clickButtonFacebookUsingSDK
{
if (!appdelegate.session.isOpen)
{
appdelegate.session = [[FBSession alloc] init];
[appdelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if(appdelegate.session.isOpen)
{
NSLog(#"calling postdata when session is not open******");
[self postData];
}
}];
}
else
{
NSLog(#"calling postdata when session is open******");
[self postData];
}
}
-(void) postData
{
[self showingActivityIndicator];
UIImage *img = [UIImage imageNamed:#"abc.jpg"];
[self performPublishAction:^{
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
connection.errorBehavior = FBRequestConnectionErrorBehaviorReconnectSession
| FBRequestConnectionErrorBehaviorAlertUser
| FBRequestConnectionErrorBehaviorRetry;
FBRequest *req = [FBRequest requestForUploadPhoto:img];
[req.parameters addEntriesFromDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys:message3, #"message", nil]];
[connection addRequest:req
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// [self showAlert:#"Photo Post" result:result error:error];
[self showAlert:#"Photo Post" result:result resulterror:error];
if (FBSession.activeSession.isOpen) {
}
}];
[connection start];
}];
}
- (void) performPublishAction:(void (^)(void)) action {
// we defer request for permission to post to the moment of post, then we check for the permission
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound)
{
// if we don't already have the permission, then we request it now
[FBSession.activeSession requestNewPublishPermissions:#[#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error)
{
if (!error) {
action();
} else if (error.fberrorCategory != FBErrorCategoryUserCancelled){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Permission denied"
message:#"Unable to get permission to post"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
} else {
action();
}
}
// UIAlertView helper for post buttons
- (void)showAlert:(NSString *)message result:(id)result resulterror:(NSError *)error
{
NSString *alertMsg;
NSString *alertTitle;
if (error)
{
alertTitle = #"Error";
// Since we use FBRequestConnectionErrorBehaviorAlertUser,
// we do not need to surface our own alert view if there is an
// an fberrorUserMessage unless the session is closed.
if (FBSession.activeSession.isOpen) {
alertTitle = #"Error";
} else {
// Otherwise, use a general "connection problem" message.
alertMsg = #"Operation failed due to a connection problem, retry later.";
}
}
else
{
NSDictionary *resultDict = (NSDictionary *)result;
alertMsg = [NSString stringWithFormat:#"Successfully posted '%#'.", message];
NSString *postId = [resultDict valueForKey:#"id"];
if (!postId) {
postId = [resultDict valueForKey:#"postId"];
}
if (postId) {
alertMsg = [NSString stringWithFormat:#"%#\nPost ID: %#", alertMsg, postId];
}
alertTitle = #"Success";
}
if (alertTitle) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
message:alertMsg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[self dismissActivityIndicator];
}
}
it gives me error in performPublishAction method.
can anyone tell me what is the problem . i have a lot of search and also found many solution but no one work. Please help. Thanks in advance.
I think that you have to set to FBSession what is its active session :
FBSession *session = [[FBSession alloc] init];
[FBSession setActiveSession:session];