我有一个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;
}
}
在我的MainView的ViewDidLoad中调用此get:
new AddressBookService().SaveContact();
我使用IOS 10.3在iPhone模拟器上进行测试。 我将此密钥添加到Entitlement.plist文件中:
<key>NSContactsUsageDescription</key>
<string>This app request access for your contacts</string>
但永远不会显示请求权限对话框。我错了什么?
答案 0 :(得分: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);
}
});
现在,正确的请求弹出正确。