gpt4 book ai didi

ios - 我们可以打开 watch 扩展上的联系人吗

转载 作者:行者123 更新时间:2023-12-01 17:25:14 25 4
gpt4 key购买 nike

如何在 watch 扩展中以编程方式打开 iPhone 的联系人,就像我们在 iOS 中使用地址簿一样。

提前致谢

最佳答案

通常从您使用的 WatchKit 扩展与 iPhone 通信

+ (BOOL)openParentApplication:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo, NSError *error)) reply;    // launches containing iOS application on the phone. userInfo must be non-nil
WKInterfaceController的方法类(class)。

因此,例如,您可以将 Storyboard中按钮的 IBAction 附加到此方法
- (IBAction)callPhoneAppButtonTapped
{
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"text to display on iPhone", @"key", nil];

[InterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"Reply received by Watch app: %@", replyInfo);
}];
}

注:为了从通讯簿中获取数据,用户需要授予您的应用权限。但应用程序将在后台启动,用户将专注于 Watch,因此最好在您的 iPhone 应用程序中请求此权限。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
if (!granted){
NSLog(@"Access denied");
return;
}
NSLog(@"Access granted");
});
}

为了处理 openParentApplication:reply 发送的消息在您的 AppDelegate 实现中
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
NSLog(@"Request received by iOS app");
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"your value to return to Apple Watch", @"key", nil];

// Here your app will be launch in background. Fetch AddressBook or other data you need.
// Remember to call reply block in the end.

// Example of saving data to Address Book
NSString *firstName;
NSString *lastName;

firstName = @"Maggie";
lastName = @"Peggie";

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
ABRecordRef contact = ABPersonCreate();

ABRecordSetValue(contact, kABPersonFirstNameProperty, (__bridge CFStringRef) firstName, nil);
ABRecordSetValue(contact, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, nil);

ABAddressBookAddRecord(addressBookRef, contact, nil);
ABAddressBookSave(addressBookRef, nil);

reply(dictionary);
}

关于ios - 我们可以打开 watch 扩展上的联系人吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com