如标题所示,我正在尝试从服务器上为用户获取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
最佳答案
您可以使用[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调用此方法,您可以在此处发送用户数据。
关于ios - 如何在iOS上使用XMPP Framework和OpenFire获取用户vCard,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28945745/