3

I am trying to display CNContact on the new CNContactViewController. I am getting no card selected. I tried this with unsaved contact (no go). Also tried with saved+fetched from CNContactStore also no go. Tried with "me" contact but the same result. According to the debugger fetched contact is loaded with correct values and is not nil/empty. App is sandboxed and correctly asks for Contacts permission.

Here is sample:

#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

No card selected

EDIT: 6th of October 2015

Apple's TSI confirmed that they can't get it to work on OS X 10.11.0

4

1 回答 1

1

解决方案是在加载视图后设置联系人。

TSI:感谢您在我与联系人工程师联系时的耐心等待。您的问题是一个错误,其解决方法是在呈现联系人视图控制器后设置联系人。

#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;
}
于 2015-10-09T08:33:18.413 回答