ios - 如何在 iOS 上使用 XMPP 框架和 OpenFire 获取用户 vCard

标签 ios xcode openfire vcf-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
    {
        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 框架和 OpenFire 获取用户 vCard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28945745/

相关文章:

ios - 通过聊天发送图像

ios - 无法将类型 '__NSCFNumber' () 的值转换为 'NSArray' swift

ios - Xcode 中的默认左花括号

swift - 尝试在 Xcode 中为 OS X App 设置选项卡顺序

swift - 从 UIPickerView 中获取字符串值从数组中选择字符串值? ( swift/xcode)

android - Smack API - 从 Openfire 服务器读取聊天记录

java - 如何使用 smack 库将自定义 IQ 节发送给另一个用户?

iOS 如何在后台反复执行http GET请求

ios - 从 JSON 字符串转换为 UIImage 总是返回 null?

cocoa - 在 openfire 中注册新用户?