【问题标题】:CFArrayGetValueAtIndex won't get the social profilesCFArrayGetValueAtIndex 不会获得社交资料
【发布时间】:2013-01-29 17:58:39
【问题描述】:

如果我尝试,我无法在 CFArrayGetValueAtIndex 获得社交资料信息(推特、脸书)。其他所有信息,如姓名、号码、www 我都知道了。我用 ABPersonCreateVCardRepresentationWithPeople 创建了一个 vCard,如果我尝试输出,我会得到如下信息:

BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//iOS 10.7.5//EN
N:lastname;Firstname;;;
FN:Firstname lastname
ORG:company;
TITLE:Jobtitle
item1.EMAIL;type=INTERNET;type=pref:my@mail.com
item1.X-ABLabel:Global
TEL;type=IPHONE;type=CELL;type=VOICE;type=pref:049651651561
item2.URL;type=pref:Www.com.com
item2.X-ABLabel:_$!<HomePage>!$_
X-SOCIALPROFILE;type=twitter;x-user=Twitteracc:http://twitter.com/Twitteracc
X-SOCIALPROFILE;type=facebook;x-user=Face.book:http://www.facebook.com/Face.book
END:VCARD

我尝试获取信息的代码是:

ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);

CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, 0);

...人没有社交信息。后来我尝试附加社交信息,也是一个错误。它只有在我创建一个新的 ABPersonCreate() 时才有效......所以这是我的错误吗?或者它还不能工作?有什么想法吗?

【问题讨论】:

    标签: xcode ios5 ios6 addressbook


    【解决方案1】:

    这将是 SDK 中的一个错误...所以您唯一能做的就是分离字符串并创建一个新的 ABPerson - 使用 ABPersonCreate() - 看看我的代码 - 它对我有用。

    -(NSMutableDictionary *)getMyVCardObjects:(NSString *)vCardString{

    NSString *firstname;
    NSString *lastname;
    NSString *pos;
    NSString *company;
    NSString *email;
    NSString *www;
    NSString *tel;
    NSString *twitterUserName;
    NSString *facebookUserName;
    
    NSArray *myArr = [vCardString componentsSeparatedByString:@"\n"];
    for (int i=0; i<[myArr count]; i++) {
    
        //vorname nachname
        if ([[myArr objectAtIndex:i] rangeOfString:@"FN:"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@":"];
            if ([tempArray count] >= 2) {
                firstname = [[[tempArray objectAtIndex:1]componentsSeparatedByString:@" "] objectAtIndex:0];
                lastname = [[[tempArray objectAtIndex:1]componentsSeparatedByString:@" "] objectAtIndex:1];
            }
        }
    
        //company
        if ([[myArr objectAtIndex:i] rangeOfString:@"ORG:"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@":"];
            company = [[[tempArray objectAtIndex:1]componentsSeparatedByString:@";"] objectAtIndex:0];
        }
    
        //title = pos
        if ([[myArr objectAtIndex:i] rangeOfString:@"TITLE:"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@":"];
            pos = [tempArray objectAtIndex:1];
        }
    
        //email
        if ([[myArr objectAtIndex:i] rangeOfString:@"EMAIL;"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@";"];
            email = [[[tempArray objectAtIndex:2]componentsSeparatedByString:@":"] objectAtIndex:1];
        }
    
    
        //tel
        if ([[myArr objectAtIndex:i] rangeOfString:@"VOICE;"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@";"];
            tel = [[[tempArray objectAtIndex:4]componentsSeparatedByString:@":"] objectAtIndex:1];
        }
    
    
        //www
        if ([[myArr objectAtIndex:i] rangeOfString:@"URL;"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@";"];
            www = [[[tempArray objectAtIndex:1]componentsSeparatedByString:@":"] objectAtIndex:1];
        }
    
    
    
        //twitter
        if ([[myArr objectAtIndex:i] rangeOfString:@"twitter"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@";"];
    
            if ([[tempArray objectAtIndex:1] rangeOfString:@"type=twitter"].location != NSNotFound) {
    
                twitterUserName = [[[[[tempArray objectAtIndex:2]componentsSeparatedByString:@"="] objectAtIndex:1] componentsSeparatedByString:@":"] objectAtIndex:0];
    
            }
    
        }
    
        if ([[myArr objectAtIndex:i] rangeOfString:@"facebook"].location != NSNotFound) {
    
            NSArray *tempArray = [[myArr objectAtIndex:i] componentsSeparatedByString:@";"];
    
            if ([[tempArray objectAtIndex:1] rangeOfString:@"type=facebook"].location != NSNotFound) {
    
                facebookUserName = [[[[[tempArray objectAtIndex:2]componentsSeparatedByString:@"="] objectAtIndex:1] componentsSeparatedByString:@":"] objectAtIndex:0];
    
            }
    
        }
    }
    return [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:firstname,lastname,pos,company,email,www,tel,twitterUserName,facebookUserName, nil] forKeys:[NSArray arrayWithObjects:@"firstname",@"lastname",@"pos",@"company",@"email",@"www",@"tel",@"twitterUserName",@"facebookUserName", nil]];
    

    }

    玩得开心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多