Xamarin.Forms iOS添加联系人(通过依赖服务)

问题描述 投票:1回答:1

我有一个Xamarin.Forms应用程序,我想在其中打开添加联系人屏幕。我使用Depedency Service来做到这一点。它在Android上运行良好,但是每次在iPhone上运行时,它都会崩溃。我的示例代码如下。

public bool SaveContactAsync(Profile ContactToSave)
        {
            var store = new CNContactStore();
            var contact = new CNMutableContact();
            var cellPhone = new CNLabeledValue<CNPhoneNumber>(CNLabelPhoneNumberKey.Mobile, new CNPhoneNumber(ContactToSave.PhoneNumber));
            var phoneNumber = new[] { cellPhone };
            contact.PhoneNumbers = phoneNumber;
            contact.GivenName = ContactToSave.FirstName;
            contact.FamilyName = ContactToSave.LastName;
            var emailAddress = new CNLabeledValue<NSString>(CNLabelKey.Other, new NSString(ContactToSave.EmailAddress));
            contact.EmailAddresses = new CNLabeledValue<NSString>[] { emailAddress };
            var saveRequest = new CNSaveRequest();
            saveRequest.AddContact(contact, store.DefaultContainerIdentifier);

            NSError error;
            if (store.ExecuteSaveRequest(saveRequest, out error))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

一旦到达saveRequest.AddContact(contact, store.DefaultContainerIdentifier);,应用程序就会崩溃。我可能做错了什么?我查看了App Center诊断程序,这就是我所看到的。

libsystem_kernel.dylib
__abort_with_payload
libsystem_kernel.dylib
abort_with_payload
TCC
__CRASHING_DUE_TO_PRIVACY_VIOLATION__
TCC
__TCCAccessRequest_block_invoke.124
TCC
__tccd_send_message_block_invoke
libxpc.dylib
_xpc_connection_reply_callout
libxpc.dylib
_xpc_connection_call_reply_async
libdispatch.dylib
_dispatch_client_callout3
libdispatch.dylib
_dispatch_mach_msg_async_reply_invoke$VARIANT$mp
libdispatch.dylib
_dispatch_kevent_worker_thread
libsystem_pthread.dylib
_pthread_wqthread
libsystem_pthread.dylib
start_wqthread
xamarin xamarin.forms xamarin.ios
1个回答
3
投票

CRASHING_DUE_TO_PRIVACY_VIOLATION

似乎您没有向应用程序添加联系人权限。

将以下代码添加到iOS项目的info.plist中。

<key>NSContactsUsageDescription</key>  
<string>Your app needs access your Contact</string> 

并且在Depedency Service

   public bool CheckPermission()
    {

        CNAuthorizationStatus status = CNContactStore.GetAuthorizationStatus(CNEntityType.Contacts);

        if(status==CNAuthorizationStatus.NotDetermined)
        {
            // need request permission
            CNContactStore store = new CNContactStore();
            store.RequestAccess(CNEntityType.Contacts,null);

        }
        else if(status == CNAuthorizationStatus.Restricted|| status == CNAuthorizationStatus.Denied)
        {
            Console.WriteLine("user restricted the permission");
        }

        else if(status==CNAuthorizationStatus.Authorized)
        {
            return true;
        }
        return false;
    }
var hasPermission = CheckPermission();
if(hasPermission)
{
   SaveContactAsync(profile);
}

有关iOS权限的更多详细信息,您可以检查Complete list of iOS app permissions

© www.soinside.com 2019 - 2024. All rights reserved.