如何在iOS上使用XMPPFramework和OpeFire创建/更新/检索用户vCard [英] How to create/ update/ retrieve user vCard using XMPPFramework and OpeFire on iOS

查看:79
本文介绍了如何在iOS上使用XMPPFramework和OpeFire创建/更新/检索用户vCard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我正在尝试为我的用户创建一个vcard并将其发送到服务器,但似乎不起作用,有什么主意吗?我将非常感谢您的帮助

As the title suggests,I am trying to create a vcard for my user and send it to the server, but it seems that it doesn't work, any ideas? i will much appreciate any help

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

Here is my code together with the stream connect and authentication, I included thses part bacause there may also be something wrong with them, as I am a noob in working with xmpp framework, the iOS client thaI use is: 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
@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 = @"www.domainName.com";
    xmppStream.hostPort = 5222;
    [xmppStream 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)xmppStreamWillConnect:(XMPPStream *)sender{
    NSLog(@"Did connect");
}

- (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");

            XMPPvCardCoreDataStorage* xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];

            XMPPvCardTempModule *xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage dispatchQueue:dispatch_get_main_queue()];

            XMPPvCardTemp *xmppvCardTemp;
            NSXMLElement *vCardElement = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
            NSXMLElement *nicknameElement = [NSXMLElement elementWithName:@"nickname" stringValue:@"A0Test1"];
            [vCardElement addChild:nicknameElement];
            xmppvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardElement];
            [xmppvCardTempModule updateMyvCardTemp:xmppvCardTemp];
    }
}

当我连接并进行身份验证但卡未上传时,它返回1,这至少部分正确吗?

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

感谢您的支持,并为所有答案投赞成票!

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

推荐答案

更新1-成功上传VCARD:

UPDATE 1 - UPLOAD VCARD SUCCEEDED:

我已经设法通过使用下一个代码来更新/创建vCard,但是当我尝试使用setLabels方法发送NSArray时,我遇到了问题,该数组未发送到服务器或服务未保存收到它的时候.

I have managed to update/ create a vCard by using the next code but I have a problem when I try to send an NSArray, using the method setLabels, the array is not send to the server or the serve doesn't save it when it receives it.

这是我的最新消息:

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{

    if ([xmppStream isAuthenticated]) {
        NSLog(@"authenticated");
        dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
        dispatch_async(queue, ^{

            XMPPvCardCoreDataStorage * xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
            XMPPvCardTempModule * xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
            [xmppvCardTempModule  activate:[self xmppStream]];

            XMPPvCardTemp *myVcardTemp = [xmppvCardTempModule myvCardTemp];

            if (!myVcardTemp) {
                NSLog(@"TEST FOR VCARD");
                NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
                XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];
                [newvCardTemp setNickname:@"nick"];
                [xmppvCardTempModule updateMyvCardTemp:newvCardTemp];
            }else{
                //Set Values as normal
                NSLog(@"TEST FOR VCARD ELSE");

                        NSLog(@"TEST FOR VCARD");
                        NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
                        XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];
                        [newvCardTemp setNickname:@"aaaaaa"];
                        NSArray *interestsArray= [[NSArray alloc] initWithObjects:@"food", nil];
                        [newvCardTemp setLabels:interestsArray];
                        [newvCardTemp setMiddleName:@"Stt"];

                        [newvCardTemp setEmailAddresses:[NSMutableArray arrayWithObjects:@"email", nil]];

                        [xmppvCardTempModule updateMyvCardTemp:newvCardTemp];
            }
        });   
    }   
}

从我的测试中得出的结论是,服务器应处理NSArrays时应该有一个问题.

from my test I conclude that there should be an issue when the serve should handle NSArrays.

我也尝试发送自定义XML,这是示例,我不知道如何发送数组,所以我将其转换为字符串:

I also tried to send a custom XML, here is the example, I don't know how to send an array so I converted it to string:

        NSLog(@"TEST FOR VCARD");
        NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
        NSXMLElement *nicknameElement = [NSXMLElement elementWithName:@"nickname" stringValue:@"A0Test1"];
        NSArray *myTestArray = [NSArray arrayWithObjects:@"IT",@"PC", nil];
        NSXMLElement *nicknameElement2 = [NSXMLElement elementWithName:@"interests" stringValue:[NSString stringWithFormat:@"%@", myTestArray]];
        [vCardXML addChild:nicknameElement];
        [vCardXML addChild:nicknameElement2];
        XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];
        [xmppvCardTempModule updateMyvCardTemp:newvCardTemp];

我的问题现在仍然存在,如何检查vCard传输完成后(如在流情况下),xmpp框架是否调用了任何委托方法?以及如何从服务器接收电子名片?

My question remains now, how to check if it reached the server, is there any delegate method called by xmpp framework when the vCard transfer is completed like in stream case? and how can I receive the vCard from the server?

这篇关于如何在iOS上使用XMPPFramework和OpeFire创建/更新/检索用户vCard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆