iOS应用扩展 - 操作 - 自定义数据 [英] iOS App Extension - Action - Custom Data

查看:137
本文介绍了iOS应用扩展 - 操作 - 自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序扩展名为 Action

I'm developing an app extension as an Action.

主机应用程序将使用我的扩展程序正常方式:通过显示 UIActivityViewController 来包含一个 activityItems 数组,然后传递给我的扩展程序。

The host app will use my extension in the normal way: by presenting the UIActivityViewController to include an array of activityItems which are then passed to my extension.

iOS将决定是否根据项目是否与我在中定义的 NSExtensionActivationRule 设置相匹配来显示我的操作 info.plist 的扩展程序。

iOS will decide whether to present my Action based on whether the items match the NSExtensionActivationRule setting that I have defined in info.plist of the extension.

此功能似乎适用于内容和指向内容的指针(图片,视频,文字,文件,网址)。

This feature seems to be meant for content and pointers to content (images, videos, text, files, URLs).

相反,我需要传递结构化数据并接收结构化数据。

Instead I need to pass structured data and receive back structured data.

我可以定义我的 itemType 作为文本使用激活规则 NSExtensionActivationSupportsText ,然后只传递序列化的JSON。但是,我的行动将提供简单的纯文本。不好。

I could define my itemType as text using activation rule NSExtensionActivationSupportsText, and then just pass serialized JSON. However, then my action would be offered for simple plain text. Not good.

显然有一些神秘的查询语言可用于在我的<$中定义 NSPredicate c $ c> NSExtensionActivationRule 设置,允许某种自定义。

There is apparently some cryptic query language that can be used to define an NSPredicate in my NSExtensionActivationRule setting, which allows some kind of customization.

但我无法弄明白。所有示例都基于内容,而不是数据。如何将自定义 actionItem 定义为结构化数据,让iOS知道我的操作何时真正合适?

But I cannot figure it out. All the examples are based on content, not data. How I can define my custom actionItem as structured data and let iOS know when my action is truly appropriate?

我能完成我想要的吗?怎么样?感谢任何提示。

Can I accomplish what I want? How? Any tips are appreciated.

更新:我怀疑这个问题的关键是自定义统一类型标识符 。但是,我仍然坚持,因为UTI定义的所有示例仍然是内容(文件和媒体),而不是结构化数据。

UPDATE: I suspect the key to this question is custom Uniform Type Identifiers. However, I'm still stuck because all the examples of UTI definitions are still content (files and media), not structured data.

推荐答案

统一类型标识符确实是执行此操作的正确方法。

Uniform Type Identifiers are indeed the correct way to do this.

我还没有完成这个,但我设法让它最低限度地工作。基本上,您定义了一个自定义类型,如 com.company.app.myThing ,您可以将自定义对象作为哈希传递给您想要的任何数据结构。显然消费者需要知道并遵循这种模式。

I have not finished developing this, but I managed to get it minimally working. Basically you define a custom type like com.company.app.myThing and you can pass your custom object as a hash with whatever data structure you want. Obviously the consumer needs to know and follow this schema.

这是 info.plist 的相关部分。扩展名:

Here is the relevant portion of info.plist in the extension:

<key>NSExtensionAttributes</key>
<dict>
  <key>NSExtensionActivationRule</key>
  <string>SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO &quot;com.company.app.myThing&quot;).@count == 1).@count == 1</string>
  <key>NSExtensionPointName</key>
  <string>com.apple.ui-services</string>
  <key>NSExtensionPointVersion</key>
  <string>1.0</string>
</dict>

在扩展程序的 ActionViewController.viewDidLoad 中:

let item = self.extensionContext.inputItems[0] as NSExtensionItem
let provider = (item.attachments as Array<NSItemProvider>)[0] as NSItemProvider
provider.loadItemForTypeIdentifier("com.company.app.myThing", options: nil, completionHandler: { msg, error in
    let my_thing = (msg as? NSDictionary) as Dictionary<String, AnyObject>
    // do stuff with my_thing
})

然后在主机应用程序中:

Then in the host app:

let my_thing = [
  "foo": "bar",
  "baz": 123
]
let item = NSExtensionItem()
let attachment = NSItemProvider(item: my_thing, typeIdentifier: "com.company.app.myThing")
item.attachments = [attachment]
let activityViewController = UIActivityViewController(activityItems: [item], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: actionComplete)

免责声明:请做不要认为这是一个权威的方法,但我希望它可以指出你正确的方向。

DISCLAIMER: Please do not consider this an authoritative how-to, but I hope it can point you in the right direction.

这篇关于iOS应用扩展 - 操作 - 自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆