How to retrieve vCard in iOS from XMPP/OpenFire

2019-05-21 10:00发布

问题:

As the name suggests I am trying to retrieve a vCard for a user using one of the next XMPPFrameworks methods:

- (XMPPvCardTemp *)fetchvCardTempForJID:(XMPPJID *)jid;
- (XMPPvCardTemp *)fetchvCardTempForJID:(XMPPJID *)jid useCache:(BOOL)useCache;

My current implementation is:

dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
dispatch_async(queue, ^{

    XMPPvCardCoreDataStorage* xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
    XMPPvCardTempModule* m = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
    [m fetchvCardTempForJID:[sender myJID] ignoreStorage:YES];
    NSLog(@"%@",xmppvCardStorage.description);
                       });

The code above is not working and also I wanted to know how can I log the data, if the current approach is the right one.

回答1:

First create a instance of the appdelegate:

-(AppDelegate *) appDelegate{
return (AppDelegate *)[UIApplication sharedApplication].delegate;}

Then use below code to retrieve the vCard:

XMPPvCardTemp *vCard =[[self appDelegate].xmppvCardTempModule vCardTempForJID:[self appDelegate].xmppStream.myJID.bareJID shouldFetch:YES];

Here I have used the current user JID. You can used other users JID, but make sure that it should be a XMPPJID object.

Log it using the object of the XMPPvCardTemp

NSLog(@"%@", vCard.nickname);
NSLog(@"%@", vCard.name);
NSLog(@"%@", vCard.emailAddresses);
NSLog(@"%@", vCard.formattedName);
NSLog(@"%@", vCard.givenName);
NSLog(@"%@", vCard.middleName);


回答2:

After requesting the vCard in the delegate method I have implemented:

- (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule
        didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp
                     forJID:(XMPPJID *)jid{

    XMPPvCardTemp *storedCard = [vCardTempModule vCardTempForJID:[XMPPJID jidWithString:@"newUser1@administrator"] shouldFetch:YES];
    NSLog(@"Stored card: %@",storedCard.prettyXMLString);

}

Witch prints out a nice string representation of the xml.