我正在为AR Unity应用制作iOS插件。当用户按下“添加联系人”按钮时,应将其导航至本机iPhone添加联系人屏幕,其中已填写了联系人详细信息。然后,用户可以确认添加新联系人并导航回到Unity App。 / p>
我已经使用directAddContact方法使功能起作用(请参见下文)。通过这种方法,可以正确添加新联系人,但是它会在后台发生,并且不会通知用户,而是会导航到本机添加联系人屏幕。
我相当确定我的代码存在以下问题:
CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:contact];
controller.contactStore = store;
controller.delegate = self;
[self.navigationController pushViewController:controller animated:true];
我猜测ViewController和委托无法正常工作,因为Unity正在控制View。我试图研究一些论坛和教程,以通过插件通过本机ViewController覆盖UnityViewController,但我无法将其中任何一个翻译成我的特定问题。
任何其他帮助或见解将不胜感激!这是我的Unity插件“ MyPlugin.mm”的代码。
#import <Foundation/Foundation.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import <UIKit/UIKit.h>
@interface MyPlugin: UIViewController<CNContactViewControllerDelegate>
{
NSDate * creationDate;
}
@end
@implementation MyPlugin
static MyPlugin *_sharedInstance;
+(MyPlugin*) sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Creating MyPlugin shared instance");
_sharedInstance = [[MyPlugin alloc] init];
});
return _sharedInstance;
}
-(void)directlyAddContact:(NSString *)firstName lastName:(NSString *)lastName email:(NSString *)email phone:(NSString *)phone {
// Create New Contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
// Add Name to Contact
contact.givenName = firstName;
contact.familyName = lastName;
// Add Phone Number to Contact
CNLabeledValue *workPhone = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:[CNPhoneNumber phoneNumberWithStringValue:phone]];
contact.phoneNumbers = @[workPhone];
// Add Email to Contact
// Save the newly created contact
CNContactStore *store = [[CNContactStore alloc] init];
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addContact:contact toContainerWithIdentifier:nil];
NSError *error;
NSLog(@"Added %@ %@ at %@", contact.givenName, contact.familyName, contact.phoneNumbers[0].value.stringValue);
[store executeSaveRequest:saveRequest error:&error];
}
-(void)addContact:(NSString *)firstName lastName:(NSString *)lastName email:(NSString *)email phone:(NSString *)phone {
// Create Store
CNContactStore *store = [[CNContactStore alloc] init];
// Create New Contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
// Add Name to Contact
contact.givenName = firstName;
contact.familyName = lastName;
// Add Phone Number to Contact
CNLabeledValue *workPhone = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:[CNPhoneNumber phoneNumberWithStringValue:phone]];
contact.phoneNumbers = @[workPhone];
// Add Email to Contact
// Create Controller and Delegate
CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:contact];
controller.contactStore = store;
controller.delegate = self;
[self.navigationController pushViewController:controller animated:true];
}
-(id)init
{
self = [super init];
return self;
}
@end
extern "C"
{
void IOSaddContact(const char* firstName, const char* lastName, const char* email, const char* phone)
{
NSString *firstNameString = [[NSString alloc] initWithUTF8String:firstName];
NSString *lastNameString = [[NSString alloc] initWithUTF8String:lastName];
NSString *phoneString = [[NSString alloc] initWithUTF8String:phone];
NSString *emailString = [[NSString alloc] initWithUTF8String:email];
return [[MyPlugin sharedInstance] addContact:firstNameString lastName:lastNameString email:emailString phone:phoneString];
}
void IOSdirectlyAddContact(const char* firstName, const char* lastName, const char* email, const char* phone)
{
NSString *firstNameString = [[NSString alloc] initWithUTF8String:firstName];
NSString *lastNameString = [[NSString alloc] initWithUTF8String:lastName];
NSString *phoneString = [[NSString alloc] initWithUTF8String:phone];
NSString *emailString = [[NSString alloc] initWithUTF8String:email];
return [[MyPlugin sharedInstance] directlyAddContact:firstNameString lastName:lastNameString email:emailString phone:phoneString];
}
}