如何在iOS上使用XMPPFramework和OpenFire获取用户vCard

时间:2015-03-09 15:27:00

标签: ios xcode openfire vcard

正如标题所暗示的那样,我正在尝试从服务器上为我的用户提取vCard,但它似乎不起作用,任何想法?我将非常感谢任何帮助

这是我的代码以及流连接和身份验证,我也包含了这部分,因为它们可能也有问题,因为我是使用XMPP Framework的菜鸟,我使用的iOS客户端是: https://github.com/robbiehanson/XMPPFramework

.h类代码是:

#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类实现

 @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)"
    }

当我连接时,当我进行身份验证时返回1,但是没有获取卡,是否正确我做了,至少部分是什么?

感谢您的支持并投票支持所有答案!

这些帖子与以下内容有关: How to create/ update/ retrieve user vCard using XMPPFramework and OpeFire on iOS

2 个答案:

答案 0 :(得分:1)

您可以使用[AppDelegate delegate].xmppvCardTempModule myvCardTemp]获取登录用户vCard如果您使用https://github.com/robbiehanson/XMPPFramework此代码。

如果您想更新vCard,可以在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];
});
}

<强>更新

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

我从cell_for_row_at_index调用此方法,您可以在此处发送用户数据。

答案 1 :(得分:1)

在XEP-0153中调用方法 - (NSData *)photoDataForJID:(XMPPJID *)jid。

secondEditText.performClick()