当前位置 : 主页 > 手机开发 > ios >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
正如标题所示,我正在尝试为我的用户创建一个vcard并将其发送到服务器,但似乎它不起作用,任何想法?我将非常感谢任何帮助 这是我的代码连同流连接和身份验证,我包括这些部分因为它
正如标题所示,我正在尝试为我的用户创建一个vcard并将其发送到服务器,但似乎它不起作用,任何想法?我将非常感谢任何帮助

这是我的代码连同流连接和身份验证,我包括这些部分因为它们可能也有问题,因为我是一个使用xmpp框架的菜鸟,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
@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,但是没有上传卡,是否正确我做了,至少部分?

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

更新1 – 上传VCARD成功:

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

这是我的更新:

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

网友评论