【问题标题】:Problem fetching contact container Xamarin.iOS获取联系人容器 Xamarin.iOS 时出现问题
【发布时间】:2019-12-14 03:08:30
【问题描述】:

我正在使用 Xamarin.iOS 开发的一个小应用程序出现问题

我的目标只是让每个联系人都出现在 iPhone 上。

我有这个代码,到目前为止它在我的手机和至少 3 个其他人上都有效。 昨天我在我的配置文件中添加了一个人,因为当我尝试获取容器时,我无法在手机上获得除“null”之外的任何其他值。

我的手机是 iPhone 7 iOS 13.1.2 - 我无法在任何其他手机上测试它。

它仍然可以在模拟器上运行(iPhone 8 iOS 13.1)。

我的 info.plist 中有联系授权请求。 我只是不明白获取默认容器时获取“null”是什么意思......

try
        {
            var keysTOFetch = new[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.Nickname, CNContactKey.JobTitle, CNContactKey.DepartmentName, CNContactKey.OrganizationName, CNContactKey.PostalAddresses, CNContactKey.UrlAddresses, CNContactKey.EmailAddresses, CNContactKey.PhoneNumbers };
            NSError error;
            CNContact[] contactList;
            var ContainerId = new CNContactStore().DefaultContainerIdentifier;  <---- //This where I get a null I never got before ...
            if (ContainerId != null)
            {
                using (var predicate = CNContact.GetPredicateForContactsInContainer(ContainerId))
                using (var store = new CNContactStore())
                    contactList = store.GetUnifiedContacts(predicate, keysTOFetch, out error);
                var contacts = new List<UserContact>();
                status = writeCSV(contactList, user);
            }
            else
                Console.WriteLine("Didn't get any container identifier ..");
        }

Apple 文档说它可以返回 'null' 但没有进一步解释 ..

非常感谢您的帮助

【问题讨论】:

    标签: c# ios iphone xamarin.ios


    【解决方案1】:

    您在某些手机上遇到了这个问题,而在其他手机上却没有,因为可以工作的手机只有一个容器可以尝试从中加载联系人,而不能工作的手机有多个容器(即 Gmail、Exchange、用于存储联系人的 iCloud 帐户)。因此,您仅从配置为默认帐户的帐户中加载联系人,该帐户有 0 个联系人。因此,它不会按要求加载所有联系人。

    解决方案:获取所有容器并对其进行迭代以从每个容器中提取所有联系人。您也许可以在 Github 上找到它的一些样本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多