如何在iOS上使用XMPPFramework和OpeFire创建/更新/检索用户vCard

时间:2015-03-05 13:22:14

标签: ios objective-c xmpp openfire vcard

正如标题所暗示的那样,我正在尝试为我的用户创建一个vcard并将其发送到服务器,但似乎它不起作用,任何想法?我将非常感谢任何帮助

这是我的代码连同流连接和身份验证,我包括这些部分因为它们可能也有问题,因为我是使用xmpp框架的noob,使用的iOS客户端是:{{3 }}

.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,但是上传的卡片没有上传,我的工作是否正确,至少部分是这样?

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

3 个答案:

答案 0 :(得分:3)

更新1 - UPLOAD VCARD成功:

我已经设法通过使用下一个代码来更新/创建一个vCard,但是当我尝试发送NSArray时遇到问题,使用方法setLabels,数组没有发送到服务器或服务器没有&#39;收到它时保存它。

这是我的更新:

- (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时应该存在问题。

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

        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框架调用,就像在流情况下一样?以及如何从服务器接收vCard?

答案 1 :(得分:2)

更新2

- &GT;如何检查vCard是否成功发送?

- &GT;答:这是通过实现接下来的两个委托方法来完成的:

 - (void)xmppvCardTempModuleDidUpdateMyvCard:(XMPPvCardTempModule *)vCardTempModule;

 - (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule failedToUpdateMyvCard:(NSXMLElement *)error;

答案 2 :(得分:2)

更新3 - 需要帮助上传vCard:

因此,在成功上传我的vCard之后,我尝试实现了fetch vCard,因此我使用xmppStreamDidAuthenticate:delegate方法中的fetch代码替换了上传代码,并在我的.h文件中添加了委托XMPPvCardTempModuleDelegate。 并测试了xmppvCardTempModule:didReceivevCardTemp:forJID:委托方法是否被调用。 问题是委托方法被调用但是NSLogs:存储卡:( null)。

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

    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:[XMPPJID jidWithString:@"test11@administrator"] ignoreStorage:YES];
                                      }

}

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


    NSLog(@"Delegate is called");
    XMPPvCardTemp *test = [xmppvCardStorage vCardTempForJID:jid xmppStream:xmppStream];
    NSLog(@"Stored card: %@",test.description);

}