【问题标题】:Xamarin.Ios crash when AddContact is called调用 AddContact 时 Xamarin.Ios 崩溃
【发布时间】:2017-07-22 01:51:35
【问题描述】:

我有一个 Xamarin.IOS 应用程序,我尝试编写一个新联系人。为此,我遵循了本指南:https://developer.xamarin.com/guides/ios/platform_features/contacts/#contacts

但是当我点击以下行时:

saveRequest.AddContact(contact, store.DefaultContainerIdentifier);

应用程序崩溃并记录以下输出:

2017-07-21 18:25:16.912 ConnectContacts.Ios[31854:7762750] critical:    0   ConnectContacts.Ios                 0x0000000104b72501 mono_handle_native_crash + 257
2017-07-21 18:25:16.912 ConnectContacts.Ios[31854:7762750] critical:    1   libsystem_platform.dylib            0x0000000110971b3a _sigtramp + 26
2017-07-21 18:25:16.912 ConnectContacts.Ios[31854:7762750] critical:    2   ???                                 0x0000000105132c2a 0x0 + 4380109866
2017-07-21 18:25:16.912 ConnectContacts.Ios[31854:7762750] critical:    3   libsystem_kernel.dylib              0x000000011087434f fcntl + 0
2017-07-21 18:25:16.913 ConnectContacts.Ios[31854:7762750] critical:    4   TCC                                 0x00000001120bb6d5 __TCCAccessRequest_block_invoke_2.80 + 0
2017-07-21 18:25:16.913 ConnectContacts.Ios[31854:7762750] critical:    5   TCC                                 0x00000001120bb61f __CRASHING_DUE_TO_PRIVACY_VIOLATION__ + 0
2017-07-21 18:25:16.913 ConnectContacts.Ios[31854:7762750] critical:    6   TCC                                 0x00000001120be56d __tccd_send_block_invoke + 305
2017-07-21 18:25:16.913 ConnectContacts.Ios[31854:7762750] critical:    7   libxpc.dylib                        0x00000001108185ba _xpc_connection_reply_callout + 45
2017-07-21 18:25:16.913 ConnectContacts.Ios[31854:7762750] critical:    8   libxpc.dylib                        0x00000001108102cb _xpc_connection_call_reply + 36
2017-07-21 18:25:16.913 ConnectContacts.Ios[31854:7762750] critical:    9   libdispatch.dylib                   0x0000000110521792 _dispatch_client_callout + 8
2017-07-21 18:25:16.914 ConnectContacts.Ios[31854:7762750] critical:    10  libdispatch.dylib                   0x0000000110507eb6 _dispatch_queue_override_invoke + 763
2017-07-21 18:25:16.914 ConnectContacts.Ios[31854:7762750] critical:    11  libdispatch.dylib                   0x0000000110509899 _dispatch_root_queue_drain + 813
2017-07-21 18:25:16.914 ConnectContacts.Ios[31854:7762750] critical:    12  libdispatch.dylib                   0x000000011050950d _dispatch_worker_thread3 + 113
2017-07-21 18:25:16.914 ConnectContacts.Ios[31854:7762750] critical:    13  libsystem_pthread.dylib             0x0000000110983616 _pthread_wqthread + 1299
2017-07-21 18:25:16.914 ConnectContacts.Ios[31854:7762750] critical:    14  libsystem_pthread.dylib             0x00000001109830f1 start_wqthread + 13
2017-07-21 18:25:16.915 ConnectContacts.Ios[31854:7762750] critical: 
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runt
ime or one of the native libraries 
used by your application.
=================================================================
The app has been terminated.
Launch failed. The app 'ConnectContacts.Ios' could not be launched on 'iPhone 7 iOS 10.3'. Error: An error occurred while executing MTouch. Please check the logs for more details.
The app has been terminated.

我已经尝试使用 try catch 来捕获错误。但这只是被忽略了。

这是我的方法:

    public void SaveContact()
    {           

        // Create a new Mutable Contact (read/write)
        var contact = new CNMutableContact();

        // Set standard properties
        contact.GivenName = "John";
        contact.FamilyName = "Appleseed";

        // Add email addresses
        var homeEmail = new CNLabeledValue<NSString>(CNLabelKey.Home, new NSString("john.appleseed@mac.com"));
        var workEmail = new CNLabeledValue<NSString>(CNLabelKey.Work, new NSString("john.appleseed@apple.com"));
        contact.EmailAddresses = new CNLabeledValue<NSString>[] { homeEmail, workEmail };

        // Add phone numbers
        var cellPhone = new CNLabeledValue<CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber("713-555-1212"));
        var workPhone = new CNLabeledValue<CNPhoneNumber>("Work", new CNPhoneNumber("408-555-1212"));
        contact.PhoneNumbers = new CNLabeledValue<CNPhoneNumber>[] { cellPhone, workPhone };

        // Add work address
        var workAddress = new CNMutablePostalAddress()
        {
            Street = "1 Infinite Loop",
            City = "Cupertino",
            State = "CA",
            PostalCode = "95014"
        };
        contact.PostalAddresses = new CNLabeledValue<CNPostalAddress>[] { new CNLabeledValue<CNPostalAddress>(CNLabelKey.Work, workAddress) };

        // Add birthday
        var birthday = new NSDateComponents()
        {
            Day = 1,
            Month = 4,
            Year = 1984
        };
        contact.Birthday = birthday;

        try
        {
            // Save new contact
            var store = new CNContactStore();
            var saveRequest = new CNSaveRequest();
            saveRequest.AddContact(contact, store.DefaultContainerIdentifier);

            // Attempt to save changes
            NSError error;
            if (store.ExecuteSaveRequest(saveRequest, out error))
            {
                Console.WriteLine("New contact saved");
            }
            else
            {
                Console.WriteLine("Save error: {0}", error);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

这个 get 在我的 MainView 的 ViewDidLoad 中被调用:

        new AddressBookService().SaveContact();

我在带有 IOS 10.3 的 iPhone 模拟器上进行测试。 我将此密钥添加到 Entitlement.plist 文件中:

<key>NSContactsUsageDescription</key>    
<string>This app request access for your contacts</string>

但从未显示请求权限对话框。我哪里错了?

【问题讨论】:

    标签: ios iphone xamarin.ios


    【解决方案1】:

    我发现了问题。首先,我必须将条目放入 info.plist。其次,我像这样更改要保存的部分:

    // Save new contact
            var store = new CNContactStore();
            store.RequestAccess(CNEntityType.Contacts, (granted, nsError) =>
            {
                if (!granted) return;
    
                var saveRequest = new CNSaveRequest();
                saveRequest.AddContact(contact, store.DefaultContainerIdentifier);
    
                // Attempt to save changes
                NSError error;
                if (store.ExecuteSaveRequest(saveRequest, out error))
                {
                    Console.WriteLine("New contact saved");
                } else
                {
                    Console.WriteLine("Save error: {0}", error);
                }
            });
    

    现在权限请求正确弹出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 2020-02-19
      相关资源
      最近更新 更多