草庐IT

ios - 无法在 iOS 中将统一的 CNContact 添加到 CNGroup

coder 2023-09-25 原文

这是我正在做的:

- (void)doCreateGroup {
    [[self contentView] endEditing:true];

    NSString * newString = [[[[self contentView] groupNameField] text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSString * firstError = nil;
    if ([newString length] == 0) {
        firstError = @"Missing group name";
    }

    NSError * groupsError = nil;
    NSArray * groups = [self.contactStore groupsMatchingPredicate:nil error:&groupsError];

    for (CNGroup * group in groups) {
        if ([group.name isEqualToString:newString]) {
            firstError = @"Group already exists";
        }
    }

    if (firstError) {
        [self presentViewController:[WLGCommonUtilities doProcessErrorWithOkay:@"Error" errorMessage:firstError] animated:YES completion:nil];
        return;
    }

    CNMutableGroup * newGroup = [CNMutableGroup new];
    [newGroup setName:newString];

    CNSaveRequest *saveRequest = [CNSaveRequest new];
    [saveRequest addGroup:newGroup toContainerWithIdentifier:nil];

    NSError * error = nil;
    [self.contactStore executeSaveRequest:saveRequest error:&error];
    if (error) {
        [self presentViewController:[WLGCommonUtilities doProcessErrorWithOkay:@"Error" errorMessage:[error localizedDescription]] animated:YES completion:nil];
    } else {
        CNSaveRequest *saveRequest2 = [CNSaveRequest new];
        NSArray * groupsAgain = [self.contactStore groupsMatchingPredicate:nil error:&groupsError];
        CNGroup * gotGroup;
        for (CNGroup * group in groupsAgain) {
            if ([group.name isEqualToString:newString]) {
                gotGroup = group;
            }
        }
        for (CNContact * contact in self.selectedContactsArray) {
            [saveRequest2 addMember:contact toGroup:gotGroup];
        }

        NSError * error1 = nil;
        [self.contactStore executeSaveRequest:saveRequest2 error:&error1];
        if (error) {
            [self presentViewController:[WLGCommonUtilities doProcessErrorWithOkay:@"Error" errorMessage:[error1 localizedDescription]] animated:YES completion:nil];
        } else {
            [[self navigationController] dismissViewControllerAnimated:true completion:nil];
        }
    }
}

这可以创建一个 CNGroup,然后将联系人添加到所述 CNGroup。适用于除统一联系人之外的所有联系人。我已尽一切可能使这项工作成功,但事实并非如此。它可能与统一的 CNContact 标识符有关,因为该标识符仅存储在临时内存中,因此无法添加到 CNGroup,因为它实际上没有真正的 CNContact 标识符。联系人框架一团糟!任何帮助,将不胜感激。我还向 Apple 提交了技术支持请求。

编辑: 解决此问题的一种方法是使用现已弃用的地址框架。通过这样做,我可以将尽可能多的统一联系人添加到地址组。

ABRecordRef group = ABGroupCreate();
ABAddressBookAddRecord(addressBook, group, nil);

ABRecordSetValue(group, kABGroupNameProperty,@"My Groups", nil);
for (int i=0;i < nPeople;i++) {
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    ABGroupAddMember(group, ref, nil);
    ABAddressBookSave(addressBook, nil);
}

这确实将通讯录中的所有内容保存到一个组中,即所有可见的联系人。所以它确实将统一联系人存储到组中。如果您在联系人处于群组中时取消链接,则两个联系人都会保留在群组中。所以旧框架可以解决这个问题。新的 Contacts 框架无法解决这个问题,这似乎很荒谬。同样,我可能会遗漏一些新的联系人框架,所以如果 iOS 中当前的联系人框架可以做到这一点,请告诉我。

最佳答案

我想通了。这是一团糟

第一步:

NSMutableArray * finalArray = [NSMutableArray array];
NSMutableArray * unifiedContacts = [NSMutableArray array];
NSMutableArray * fullContacts = [NSMutableArray array];

CNContactFetchRequest * request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
[request setSortOrder:CNContactSortOrderGivenName];
[self.contactStore enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
    [unifiedContacts addObject:contact];
}];

CNContactFetchRequest * request2 = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
[request2 setUnifyResults:false];
[request2 setSortOrder:CNContactSortOrderGivenName];
[self.contactStore enumerateContactsWithFetchRequest:request2 error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
    [fullContacts addObject:contact];
}];

for (CNContact * contctUn in unifiedContacts) {
    NSMutableArray * nestedContacts = [NSMutableArray array];
    for (CNContact * contct in fullContacts) {
        if ([contctUn isUnifiedWithContactWithIdentifier:contct.identifier]) {
            [nestedContacts addObject:contct];
        }
    }
    if (nestedContacts.count) {
        [finalArray addObject:@{@"contact" : contctUn, @"linked" : nestedContacts}];
    } else {
        [finalArray addObject:@{@"contact" : contctUn}];
    }
}

self.mainArray = [finalArray mutableCopy];

这会从统一的联系人中拉入所有联系人,然后拉入所有未统一的联系人,将这些组拼接在一起并将它们保存为字典,如果联系人确实链接到问题。

第 2 步:创建一个组...这非常简单,无需显示代码,因为这非常简单

第 3 步:

for (id obj in self.filteredSearchArray) {
    if ([obj valueForKey:@"linked"]) {
        for (id obj2 in [obj valueForKey:@"linked"]) {
            [self.selectedContactsArray addObject:obj2];
        }
    }
}

CNSaveRequest *saveRequest2 = [CNSaveRequest new];
for (CNContact * contact in self.selectedContactsArray) {
    [saveRequest2 addMember:contact toGroup:[newGroup copy]];
}

NSError * error1 = nil;
[self.contactStore executeSaveRequest:saveRequest2 error:&error1];

self.selectedContactsArray 是包含您想要加入群组的联系人的数组。它包含您想要在组中的所有联系人此外,如果您想要在组中的联系人链接到用户,它还包含子链接的联系人。

当此保存请求执行时,该组现在包含统一联系人。

这是一团糟。 iOS 中的联系人框架一团糟,但这有效。没有任何为联系人创建群组的应用程序可以解决这个问题,因此这是值(value)百万美元的解决方案。

关于ios - 无法在 iOS 中将统一的 CNContact 添加到 CNGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52670973/

有关ios - 无法在 iOS 中将统一的 CNContact 添加到 CNGroup的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

随机推荐