【问题标题】:How to featch user vCard using XMPPFramework and OpeFire on iOS如何在 iOS 上使用 XMPP 框架和 OpenFire 获取用户 vCard
【发布时间】:2015-03-09 15:26:59
【问题描述】:

正如标题所示,我正在尝试从服务器为我的用户获取 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

【问题讨论】:

    标签: ios xcode openfire vcf-vcard


    【解决方案1】:

    如果你使用https://github.com/robbiehanson/XMPPFramework这个代码,你可以使用[AppDelegate delegate].xmppvCardTempModule myvCardTemp]获取登录的用户vCard。

    如果你想更新 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 调用这个方法,你可以在这里发送用户数据。

    【讨论】:

    • 谢谢@souvickcse,但我认为这不是我正在寻找的分析器我有兴趣检索登录的用户 vCard 以及其他用户 vCard,我认为我应该更好地使用fetchvCardTempForJID 并忽略存储,因此我可以确定我正在检索新的 vCard。从我可以看到您建议的方法是使用此方法:vCardTempForJID,我不知道使用该方法是否是更好的选择,而不是我正在使用的方法。我想其中一个正在获取缓存的 vCard,另一个正在发出请求,你能帮我解决这个问题吗?
    • 请查看我更新的答案。在我的项目中,我必须显示名册的个人资料图片。您可以对其进行修改以获取其他信息。
    【解决方案2】:

    在 XEP-0153 中调用方法 - (NSData *)photoDataForJID:(XMPPJID *)jid。

    NSData *photoData= [[Appdelegate appdelegateobject ]xmppvCardAvatarModule] photoDataForJID:jid];
    UIImage *img=[UIImage imageWithData:data];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-08
      • 2011-07-15
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      相关资源
      最近更新 更多