如何使用共享扩展共享包含标记的注释 Swift
How to share a Note including Markup with Share Extensions Swift
我已经弄明白了如何分享笔记的一般内容(文字和图片)。但我目前处理问题的方式是不保留注释的标记(标题、列表等)。我刚收到纸条的纯文本。例如,当您与 Mail 共享便笺时,您可以看到标记已传输。有没有办法为您自己的应用程序做到这一点?
我目前只收到纯文本的解决方案:
class ShareViewController: UIViewController{
override func viewDidLoad() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
// Verify the provider is valid
if let contents = content.attachments as? [NSItemProvider] {
// look for images
for attachment in contents {
print(attachment.registeredTypeIdentifiers)
if attachment.hasItemConformingToTypeIdentifier("public.plain-text"){
attachment.loadItem(forTypeIdentifier: "public.plain-text", options: nil) { data, error in
let string = data as! String
print(string)
}
}
}
}
}
}
}
编辑:
我现在的 NSExtensionActivationRules
:
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationDictionaryVersion</key>
<integer>2</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>100</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
</dict>
使用你的NSExtensionItem
(内容)的attributedContentText
属性:
override func viewDidLoad() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
// move your content validation to `func isContentValid() -> Bool`
let attributedString = content.attributedContentText // yay NSAttributedString!
}
}
我已经弄明白了如何分享笔记的一般内容(文字和图片)。但我目前处理问题的方式是不保留注释的标记(标题、列表等)。我刚收到纸条的纯文本。例如,当您与 Mail 共享便笺时,您可以看到标记已传输。有没有办法为您自己的应用程序做到这一点?
我目前只收到纯文本的解决方案:
class ShareViewController: UIViewController{
override func viewDidLoad() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
// Verify the provider is valid
if let contents = content.attachments as? [NSItemProvider] {
// look for images
for attachment in contents {
print(attachment.registeredTypeIdentifiers)
if attachment.hasItemConformingToTypeIdentifier("public.plain-text"){
attachment.loadItem(forTypeIdentifier: "public.plain-text", options: nil) { data, error in
let string = data as! String
print(string)
}
}
}
}
}
}
}
编辑:
我现在的 NSExtensionActivationRules
:
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationDictionaryVersion</key>
<integer>2</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>100</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
</dict>
使用你的NSExtensionItem
(内容)的attributedContentText
属性:
override func viewDidLoad() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
// move your content validation to `func isContentValid() -> Bool`
let attributedString = content.attributedContentText // yay NSAttributedString!
}
}