我试图在新的CNCONTACVIEW控制器上显示CNACTACK。我没有选择卡。我用未保存的联系人尝试了这个(不走)。也试过用saved+fetched从cncontactstore也不去。尝试与“我”接触,但结果相同。根据调试器,获取的联系人加载了正确的值,并且不是nil/empty。应用程序已沙盒化,并正确请求联系人权限。
以下是样本:
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import "AppDelegate.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property (strong) CNContact *contact;
@property (strong) CNContactViewController *controller;
@end
@implementation AppDelegate
- (instancetype)init {
self = [super init];
if (self) {
_controller = [[CNContactViewController alloc] init];
}
return self;
}
- (void)awakeFromNib
{
CNContactStore *store = [[CNContactStore alloc] init];
CNMutableContact *mutContact = [[CNMutableContact alloc] init];
NSString *identifier = [mutContact identifier];
mutContact.givenName = @"GivenName";
mutContact.familyName = @"FamilyName";
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addContact:mutContact toContainerWithIdentifier:[store defaultContainerIdentifier]];
[store executeSaveRequest:saveRequest error:nil];
id keysToFetch = @[[CNContactViewController descriptorForRequiredKeys]];
_contact = [store unifiedContactWithIdentifier:identifier keysToFetch:keysToFetch error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
self.controller.contact = self.contact;
self.window.contentViewController = self.controller;
self.window.contentView = self.controller.view;
});
}
@end
编辑:2015年10月6日
苹果的tsi证实他们无法让它在os x 10.11.0上运行。
最佳答案
解决方案是在加载视图后设置联系人。
TSI:谢谢你的耐心,而我正在接触
联系工程师。您的问题是一个bug,其解决方法是设置
在呈现接触视图控制器之后的接触。
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import "ViewController.h"
@interface ViewController()
@property (strong) CNContactStore *store;
@property (strong) CNContactViewController *controller;
@property(nonatomic, copy) NSString *contactIdentifier;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.store = [[CNContactStore alloc] init];
[self saveContact];
}
-(void)saveContact
{
CNMutableContact *mutContact = [[CNMutableContact alloc] init];
mutContact.givenName = @"GivenName";
mutContact.familyName = @"FamilyName";
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addContact:mutContact toContainerWithIdentifier:[self.store defaultContainerIdentifier]];
[self.store executeSaveRequest:saveRequest error:nil];
self.contactIdentifier = [mutContact identifier];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
- (IBAction)displayContact:(id)sender {
id keysToFetch = @[[CNContactViewController descriptorForRequiredKeys]];
CNContact *contact = [self.store unifiedContactWithIdentifier:self.contactIdentifier keysToFetch:keysToFetch error:nil];
self.controller = [[CNContactViewController alloc] init];
[self.controller.view setFrameSize:NSMakeSize(500, 500)];
[self presentViewController:self.controller asPopoverRelativeToRect:self.view.bounds ofView: self.view preferredEdge: NSMaxXEdge behavior:NSPopoverBehaviorTransient];
self.controller.contact = contact;
}