cordova iOS 调起通讯录崩溃问题

来源:互联网 发布:优化一个网站多少钱 编辑:程序博客网 时间:2024/06/05 01:05

是这样的崩溃是因为iOS9以后更换了使用通讯录的框架 以前的AddressBook 更换为Contacts
所以需要判断系统版本来创建addressVC还是contactVC然后分别写下他们的代理方法
之后就是一个比较容易入坑的地方
之前的代理方法里面都是一些 像我这样的培训的iOS 难懂的地方了 ABRecordRef 这是个什么东西 怎么创建 里面存了什么

- (NSMutableDictionary *)convertToDictionary:(ABRecordRef)person {    NSString *fullName, *email;    fullName = (__bridge NSString*)ABRecordCopyCompositeName(person);    if (!fullName) {        fullName = @"";    }    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);    if(ABMultiValueGetCount(multi) > 0)        email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multi, 0);    else        email = @"";    ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);    NSMutableDictionary* phones = [NSMutableDictionary dictionaryWithCapacity:2];    for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {        NSString *label = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(multiPhones, i);        label = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(multiPhones, i);        NSLog(@"Phone Label: %@", label);        [phones setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(multiPhones, i) forKey: label];    }    ABMultiValueRef multiAddresses = ABRecordCopyValue(person, kABPersonAddressProperty);    NSMutableArray *addresses = [NSMutableArray array];    for (CFIndex i = 0; i < ABMultiValueGetCount(multiAddresses); i++) {        NSDictionary *dictionary = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(multiAddresses, i));        NSArray *keys = @[(__bridge NSString *)kABPersonAddressStreetKey, (__bridge NSString *)kABPersonAddressCityKey,                          (__bridge NSString *)kABPersonAddressStateKey, (__bridge NSString *)kABPersonAddressCountryKey];        NSMutableArray *values = [NSMutableArray array];        for (NSString *key in keys) {            NSString *value = dictionary[key];            if (value && ![value isEqualToString:@""]) {                [values addObject:value];            }        }        [addresses addObject:[values componentsJoinedByString:@", "]];    }    NSString *imageURL = [self imageURLForRecord:person fullName:fullName];    NSLog(@"%@ %@", fullName, email);    NSMutableDictionary* contact = [NSMutableDictionary dictionaryWithCapacity:2];    if (email) {    }    [contact setObject:email forKey: @"email"];    [contact setObject:fullName forKey: @"displayName"];    [contact setObject:phones forKey:@"phones"];    contact[@"photoUrl"] = imageURL;    contact[@"address"] = addresses;    ABRecordID recordID = ABRecordGetRecordID(person); // ABRecordID is a synonym (typedef) for int32_t    [contact setObject:@(recordID) forKey:@"id"];    return contact;}

好像看不太懂的样子 而cordova-contact 这个三方还要了 ABRecordRef作为参数 但是这个ABRecordRef根本没法创建 无从下手 所以只好再写个方法 其实ABRecordRef里面的参数换了种方式存储了起来:CNContact

这个就一目了然了 任意搞吧

- (NSMutableDictionary *)convertToDictionaryFor:(CNContact *)contact{    NSString *fullName, *email;    fullName=contact.givenName;    email=@"";    NSLog(@"%@",contact.phoneNumbers[0].value);    NSMutableDictionary* phones = [NSMutableDictionary dictionaryWithCapacity:2];    for (int i =0; i<contact.phoneNumbers.count; i++) {        NSString *phoneNumber=[contact.phoneNumbers[i].value stringValue];        [phones setObject:phoneNumber forKey:[NSString stringWithFormat:@"%d",i]];    }    NSMutableArray *addresses = [NSMutableArray array];    NSString *tmpDirectory = NSTemporaryDirectory();    NSString *fileName = [NSString stringWithFormat:@"%@_image.png", [fullName isEqualToString:@""] ? [NSDate date] : fullName];    NSString *imagePath = [tmpDirectory stringByAppendingPathComponent:fileName];    [contact.imageData writeToFile:imagePath atomically:YES];    NSString *imageURL =[NSURL fileURLWithPath:imagePath].absoluteString;    NSLog(@"%@ %@", fullName, email);    NSMutableDictionary* kContact = [NSMutableDictionary dictionaryWithCapacity:2];    if (email) {    }    [kContact setObject:email forKey: @"email"];    [kContact setObject:fullName forKey: @"displayName"];    [kContact setObject:phones forKey:@"phones"];    kContact[@"photoUrl"] = imageURL;    kContact[@"address"] = addresses;    [kContact setObject:@(1) forKey:@"id"];    return kContact;}

捋顺一下 判断是否是iOS8以上 如果是走自己写的方法 自己写的代理方法 使用- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
方法 就可以接收到我们点击某个联系人所执行的方法了 这样就解决了 iOS10 cordova调用联系人崩溃的问题

0 0
原创粉丝点击