CFArrayGetValueAtIndex将无法获取社交个人资料

时间:2013-01-29 17:58:40

标签: xcode ios5 ios6 addressbook

如果我尝试的话,我无法在CFArrayGetValueAtIndex获取社交资料信息(twitter,facebook)。其他所有信息,如姓名,号码,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()时才有效...所以这是我的错误?或者它不会工作?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这将是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]];

}

玩得开心。