ios - 如何与我的应用程序共享选定的文本?

标签 ios objective-c mobile share

我想让我的应用程序出现在 UIActivityViewController 中以进行文本共享,例如邮件、iMessage、Notes、Gmail 等。

例如,当用户在所选文本上回击并点击附件中的任何应用的“分享”按钮时:

For example, when user tapback on selected text and hit the 'Share' button like in the attachment.

我希望我的应用出现在 UIActivityViewController 中,并且当用户选择我的应用时,启动它并能够处理所选文本。

所以我尝试过的是: 在 Apple 文档中搜索。

  • 搜索了相关的 UTI,但我了解到 UTI 仅用于文件,而不用于简单的 NSString(如果我错了请纠正我)。
  • 我已经尝试实现共享扩展,但这不是我想要的解决方案,我不需要帖子弹出窗口,而且我需要在共享后启动我的应用程序,就像在邮件、备忘录、iMessage 中一样(关于 Apple 文档,我们无法仅通过 Today 扩展程序通过 Share 扩展程序启动包含应用程序。
  • 当然,我在 StackOverFlow 中搜索了很多。

那么有什么解决办法吗? 谢谢!

最佳答案

假设您已经有一些应用。

  1. 添加目标文件 -> 新建 -> 目标。在 在左 Pane 中,从 iOS 部分选择 Application Extension, 选择操作扩展,然后点击下一步
  2. 设置产品名称。确保操作类型呈现用户界面。点击完成
  3. Project Navigator 中,展开扩展组并单击MainInterface.storyboard。选择 ImageView 并将其替换为 UITextView。创建并绑定(bind) @IBOutlet weak var。
  4. 选择扩展的 Info.plist,导航到 NSExtension -> NSExtensionAttributes -> NSExtensionActivationRule。将 NSExtensionActivationRule 的类型从 String 更改为 Dictionary。展开字典后,单击它旁边的 + 按钮。这将添加一个子键。将其名称设置为 NSExtensionActivationSupportsText,将其类型设置为 Boolean,将值设置为 YES。这可确保操作扩展仅在至少一个输入项包含文本时可见。
  5. 将此代码放入ActionViewController.swift:

_

import UIKit
import MobileCoreServices
class ActionViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Get the item[s] we're handling from the extension context.
        
        var textFound = false
        for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
            for provider in item.attachments! {
                if provider.hasItemConformingToTypeIdentifier(kUTTypePlainText as String) {
                    // This is an plain Text.
                    weak var weakTextView = self.textView
                    provider.loadItem(forTypeIdentifier: kUTTypePlainText as String, options: nil, completionHandler: { (textItem, error) in
                        OperationQueue.main.addOperation {
                            if let strongTextView = weakTextView {
                                if let gotText = textItem as? String {
                                    strongTextView.text = gotText
                                    // do what you need with the text
                                }
                            }
                        }
                    })
                    
                    textFound = true
                    break
                }
            }
            
            if (textFound) {
                // We only handle one text, so stop looking for more. You can do as you need.
                break
            }
        }
    }

    @IBAction func done() {
        // Return any edited content to the host app.
        // This template doesn't do anything, so we just echo the passed in items.
        self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
    }
}

关于ios - 如何与我的应用程序共享选定的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44994932/

相关文章:

javascript - 制作 Picker 的事件监听器

iphone - 核心数据实体为字典/plist

ios - 如何从 iOS 中的另一个类访问标签上的按钮?

objective-c - 如何调用接受 block 字典作为 Swift 参数的 Objective-C 函数?

objective-c - NSDateFormatter stringFromDate 在使用 12 小时时间格式时返回 nil

actionscript-3 - 在 AS3 移动应用程序中设置 ApplicationDPI

ios - -(void)updateConstraints 中的动画约束变化

objective-c - 核心数据: How to handle new versions?

iPhone ASIHTTP - 区分 API 调用?

javascript - 如何在移动 web UI 上实现选择性缩放?