Laur Stefan
Laur Stefan

Reputation: 1579

How to featch user vCard using XMPPFramework and OpeFire on iOS

As the title suggests,I am trying to fetch a vCard for my user from the server, but it seems that it doesn't work, any ideas? I will much appreciate any help

Here is my code together with the stream connect and authentication, I included this part as well because there may also be something wrong with them, as I am a noob in working with XMPP Framework, the iOS client that I use is: https://github.com/robbiehanson/XMPPFramework

the .h class code is:

#import <UIKit/UIKit.h>
#import "XMPPStream.h"
#import "XMPP.h"
#import "XMPPReconnect.h"
#import "XMPPPresence.h"
#import "XMPPRoster.h"
#import "ForgotPasswordViewController.h"
#import "XMPPReconnect.h"
#import "XMPPRosterCoreDataStorage.h"
#import "ConractsViewController.h"
#import "KeychainItemWrapper.h"
#import "SignUpViewController.h"
#import "XMPPvCardTemp.h"
#import "XMPPvCardTempModule.h"
#import "XMPPvCardCoreDataStorage.h"

@interface SignInViewController : UIViewController <UITextFieldDelegate, XMPPRosterDelegate, XMPPStreamDelegate>

@property (strong, nonatomic) XMPPRosterCoreDataStorage *xmppRosterStorage;
@property (strong, nonatomic) XMPPRoster *xmppRoster;
@property (strong, nonatomic) XMPPReconnect *reconnect;
@property (strong, nonatomic) XMPPStream *xmppStream;

@end

.m class implementation

 @implementation SignInViewController
    {
        XMPPvCardCoreDataStorage *xmppvCardStorage;
        XMPPvCardTempModule * xmppvCardTempModule;
    }
    @synthesize xmppRosterStorage, xmppRoster, reconnect, xmppStream;

    - (void)viewDidLoad {

        //add SignIn button
        int signInButtonXPossition = [[UIScreen mainScreen] bounds].size.width * 0.1f;
        int signInButtonYPossition = [[UIScreen mainScreen] bounds].size.height * 0.55f;
        int signInButtonWidth = [[UIScreen mainScreen] bounds].size.width * 0.8f;
        int signInButtonHeight = [[UIScreen mainScreen] bounds].size.height * 0.07;
        UIButton *signInButton = [[UIButton alloc] initWithFrame:CGRectMake(signInButtonXPossition, signInButtonYPossition, signInButtonWidth, signInButtonHeight)];
        [signInButton addTarget:self
                         action:@selector(signInButtonFunction)
               forControlEvents:UIControlEventTouchUpInside];
        signInButton.backgroundColor = [UIColor colorWithRed:(167/255.f) green:(224/255.f) blue:(250/255.f) alpha:1];
        signInButton.layer.cornerRadius=[[UIScreen mainScreen] bounds].size.width * 0.05f;
        signInButton.layer.borderWidth=1.0;
        signInButton.layer.masksToBounds = YES;
        signInButton.layer.borderColor=[[UIColor whiteColor] CGColor];
        [self.view addSubview:signInButton];
        [signInButton setTitle:@"Sign In" forState:UIControlStateNormal];

    }

    - (void)signInButtonFunction{

        xmppStream = [[XMPPStream alloc] init];
        xmppStream.myJID = [XMPPJID jidWithString:@"test@administrator"];
        xmppStream.hostName = @"ServerAddress";
        xmppStream.hostPort = 5222;
        [xmppStream addDelegate:self         delegateQueue:dispatch_get_main_queue()];

        xmppvCardStorage = nil;
        xmppvCardStorage = [[XMPPvCardCoreDataStorage alloc]initWithInMemoryStore];
        xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
        [xmppvCardTempModule activate:xmppStream];
        [xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()];

        reconnect = [[XMPPReconnect alloc] init];
        [reconnect activate:xmppStream];

        NSError *error = nil;
        if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
        NSLog(@"error: %@", error);
       }
        NSLog(@"error: %@", error);

        }

    - (void)xmppStreamDidConnect:(XMPPStream *)sender {
        NSError *error = nil;

        if (![xmppStream authenticateWithPassword:@"test" error:&error]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:[NSString stringWithFormat:@"Can't authenticate %@", [error localizedDescription]]
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];
        }
        XMPPPresence *mypresence = [XMPPPresence presenceWithType:@"available"];
        [xmppStream sendElement:mypresence];
    }

    - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
        NSLog(@"e%d",[xmppStream isConnected]);//prints out 1
        NSLog(@"e%d",[xmppStream isAuthenticated]);//prints out 1

        if ([xmppStream isAuthenticated]) {

             NSLog(@"authenticated");
            [xmppvCardTempModule fetchvCardTempForJID:[XMPPJID jidWithString:@"test11@administrator"] ignoreStorage:YES];

        }
    }

    - (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule
            didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp
                         forJID:(XMPPJID *)jid{

            XMPPvCardTemp *test = [xmppvCardStorage vCardTempForJID:jid xmppStream:xmppStream];
            NSLog(@"Stored card: %@",test.description);
            //Prints out: "Stored card: (null)"
    }

It returns 1 when I connect and when I authenticate, but the card is not fetched, is it correct what I do, at least partially?

Thank you for your support and vote up for all answers!

These post is related to: How to create/ update/ retrieve user vCard using XMPPFramework and OpeFire on iOS

Upvotes: 1

Views: 2339

Answers (2)

Vijay Karthik
Vijay Karthik

Reputation: 467

Invoke the method - (NSData *)photoDataForJID:(XMPPJID *)jid in XEP-0153.

NSData *photoData= [[Appdelegate appdelegateobject ]xmppvCardAvatarModule] photoDataForJID:jid];
UIImage *img=[UIImage imageWithData:data];

Upvotes: 1

souvickcse
souvickcse

Reputation: 7804

You can get the logged in user vCard using [AppDelegate delegate].xmppvCardTempModule myvCardTemp] If you are using https://github.com/robbiehanson/XMPPFramework this code.

And if you want to update the vCard you can use this code in your appdelegate

- (void)updateProfile:(UIImage *)profilePicture userData:(NSDictionary *)userData

{
NSMutableArray *elements = [NSMutableArray array];
[elements addObject:[NSXMLElement elementWithName:@"fullname" stringValue:@""]];
NSData *pictureData = UIImagePNGRepresentation(profilePicture);
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
dispatch_async(queue, ^{
    XMPPvCardTemp *myVcardTemp = [xmppvCardTempModule myvCardTemp];
    [myVcardTemp setPhoto:pictureData];
    [myVcardTemp setEmailAddresses:@[[userData valueForKey:@"email"]]];
    [myVcardTemp setName:[userData valueForKey:@"name"]];
    [xmppvCardTempModule updateMyvCardTemp:myVcardTemp];
});
}

UPDATE

- (void)configurePhotoForCell:(CurrentChatCell *)cell user:(XMPPUserCoreDataStorageObject *)user
{
// Our xmppRosterStorage will cache photos as they arrive from the xmppvCardAvatarModule.
// We only need to ask the avatar module for a photo, if the roster doesn't have it.

if (user.photo != nil)
{
    cell.imgUser.image = user.photo;
}
else
{
    NSData *photoData = [[[AppDelegate delegate] xmppvCardAvatarModule] photoDataForJID:user.jid];

    if (photoData != nil)
        cell.imgUser.image = [UIImage imageWithData:photoData];
    else
        cell.imgUser.image = [UIImage imageNamed:@"userUnknown"]; //Setting a demo image only    }
}

I am calling this method from cell_for_row_at_index you can send user data here.

Upvotes: 1

Related Questions