gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:06:11 25 4
gpt4 key购买 nike

正如标题所暗示的,我正在尝试为我的用户创建一个 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,但是当我尝试使用方法 setLabels 发送 NSArray 时遇到了问题,该数组没有发送到服务器或者服务器没有保存当它收到它时。

这是我的更新:

- (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];
}
});
}
}

根据我的测试,我得出结论,当服务应该处理 NSArray 时应该存在问题。

我还尝试发送自定义 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 框架调用任何委托(delegate)方法,如在流情况下?以及如何从服务器接收 vCard?

关于ios - 如何在 iOS 上使用 XMPPFramework 和 OpeFire 创建/更新/检索用户 vCard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28878849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com