9

iOS分享扩展支持自定义联系人

 2 years ago
source link: https://blog.51cto.com/u_11643026/5586516
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

iOS分享扩展支持自定义联系人

iOS可以通过添加Share Extension来扩展系统的分享组件,能够支持将网页,图片,音乐或任何接入了系统分享组件功能的应用分享到我们自己的App内。分享Extension的介绍可以在如下文章中查看:

 https://my.oschina.net/u/2340880/blog/485656

如果你有使用过抖音,你会发现抖音不仅提供了分享扩展,也提供了更加便捷的分享到联系人的功能,在系统的分享面板中可以看到推荐的抖音联系人信息,如下图所示:

iOS分享扩展支持自定义联系人_.net

通过联系人推荐入口,我们可以快速的将内容分享到指定的联系人,非常方便。

要向系统分享组件中添加推荐联系人功能也非常简单,只将分享扩展需要结合INSendMessageIntent进行使用即可。简单来说,分为3步:

1. 为应用工程添加一个Share Extension的扩展。

2. 声明对INSendMessageIntent意图类型的支持。

3. 使用代码注入一些INSendMessageIntent意图到扩展中。

首先,我们先来做第一步,新建一个Share Extension,选择如下图的Target插件即可:

iOS分享扩展支持自定义联系人_ios_02

暂时我们先不编写任何额外的代码。

之后,在宿主App工程的Info.plist文件中添加如下键值:

iOS分享扩展支持自定义联系人_键值_03

在Extension的Info.plist中添加如下键值:

iOS分享扩展支持自定义联系人_ios_04

需要注意,宿主App和扩展插件中的Info.plist文件都要处理,缺一不可。

最后,我们在宿主App中执行如下代码即可注入意图:

// 联系人名字
INSpeakableString *groupName = [[INSpeakableString alloc] initWithSpokenPhrase:@"珲少"];
// 联系人图片
INImage *image = [INImage imageWithImageData:UIImagePNGRepresentation([UIImage imageNamed:@"aa"])];
// 创建意图对象
INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:nil content:nil speakableGroupName:groupName conversationIdentifier:@"huishao.id" serviceName:nil sender:nil];
[intent setImage:image forParameterNamed:@"groupName"];
// 进行注入
self.interaction = [[INInteraction alloc] initWithIntent:intent response:nil];
[self.interaction donateInteractionWithCompletion:^(NSError * _Nullable error) {
        
}];

之后,运行App,在系统的分享面板中可以看到我们注入的推荐联系人信息了,如图:

iOS分享扩展支持自定义联系人_ios_05
iOS分享扩展支持自定义联系人_ios_06

参考文档:

 https://developer.apple.com/documentation/foundation/app_extension_support/supporting_suggestions_in_your_app_s_share_extension?language=objc

专注技术,热爱生活,交流技术,也做朋友。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK