objective-c - 如何在 OSX 上使用 CNContactViewController 显示 CNContact

标签 objective-c macos cocoa contacts osx-elcapitan

我正在尝试在新的 CNContactViewController 上显示 CNContact。我没有选择任何卡。我用未保存的联系人试过这个(不行)。还尝试使用 saved+fetched from CNContactStore 也没有成功。尝试与“我”联系,但结果相同。根据调试器,获取的联系人加载了正确的值并且不是零/空。应用程序被沙盒化并正确请求联系人权限。

这是示例:

#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

编辑:2015 年 10 月 6 日

Apple 的 TSI 确认他们无法让它在 OS X 10.11.0 上运行

最佳答案

解决方案是加载 View 后设置联系人。

TSI: Thank you for your patience while I was reaching out to the Contacts engineers. Your issue is a bug whose workaround is to set the contact after presenting the contact view controller.

#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;
}

关于objective-c - 如何在 OSX 上使用 CNContactViewController 显示 CNContact,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32507411/

相关文章:

ios - 如果远程有新版本可用,则更新本地 bundle 文件

objective-c - Mac 操作系统 : Injecting item to Finder's Context Menu

php - 无法在 Apache (Homebrew OS X) 中加载已安装的 PHP 版本

swift - 将 CGDisplayStream 与队列一起使用

cocoa - 在 NSTableView 中仅为填充的行绘制网格线?

cocoa - NSURLConnection 只下载前 567 个字节?

iphone - Imageview 必须专注于点击的图像

objective-c - NSView setNeedDisplay :rect causes redrawing bigger area than needed

ios - 在 ios 中创建用户可配置的 UI

Objective-C 响应选择器