如何使用共享扩展程序从其他APP到Swift中的APP读取附件文件中的字符串,而不直接共享字符串? [英] How to read the string in an attached file, not sharing the string directly, using Share Extension from other APP to my APP in Swift?

查看:174
本文介绍了如何使用共享扩展程序从其他APP到Swift中的APP读取附件文件中的字符串,而不直接共享字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我可以用这种方式阅读文本并在代码中打印文本.

In the following code I can read text in this way and print the text in the code.

但是我不知道如何共享txt文件作为附件并在以下代码中读出.

But I do not know how to share txt file as an attachment and read it out in the following code.

import UIKit
import Social
import CoreServices

class ShareViewController: SLComposeServiceViewController {

    private var textString: String?

    override func isContentValid() -> Bool {
        // Do validation of contentText and/or NSExtensionContext attachments here

        if textString != nil {
            if !contentText.isEmpty {
                return true
            }
        }

        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()


        let extensionItem = extensionContext?.inputItems[0] as! NSExtensionItem
        let contentTypeText = kUTTypeText as String

        for attachment in extensionItem.attachments! {
            if attachment.isText {
                attachment.loadItem(forTypeIdentifier: contentTypeText, options: nil, completionHandler: { (results, error) in
                    let text = results as! String
                    self.textString = text
                    _ = self.isContentValid()
                })
            }
        }
    }

    override func didSelectPost() {
        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.

        print(String(textString!)) // <-- I cannot read txt file and print it

        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    override func configurationItems() -> [Any]! {
        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
        return []
    }

}


//MARK: NSItemProvider check
extension NSItemProvider {

    var isText: Bool {
        return hasItemConformingToTypeIdentifier(kUTTypeText as String)
    }

}

在共享扩展程序中,是否有任何方法可以共享txt文件作为附件?

Is there any method to share the txt file as an attachment in Share Extension?

感谢您的帮助.

推荐答案

首先,在共享扩展名的info.plist中,您必须设置附件的类型以及扩展名可以得到多少

First of all, in info.plist of your share extension you have to set what type of attachments and how many your extension can get

示例:

    <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <dict>
                <key>NSExtensionActivationSupportsImageWithMaxCount</key>
                <integer>10</integer>
                <key>NSExtensionActivationSupportsMovieWithMaxCount</key>
                <integer>1</integer>
                <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
                <integer>1</integer>
            </dict>
        </dict>

您可以找到所有的键扩展名首先获取附件时,您需要检查其类型(即您希望接受的附件). 然后,在didSelectPost()中,您可以获取该附件并对其进行处理(如果需要的话,可以将其存储在您的应用中).

When your extension recives attachment first you need to check it for type(is it what you are expecting to recive). And then, in didSelectPost() you can get that attachment and do something with it (maybe store it in your app if that's what you want).

这里是一篇关于照片扩展名的好文章

Here is nice article about shere extension for photos

这篇关于如何使用共享扩展程序从其他APP到Swift中的APP读取附件文件中的字符串,而不直接共享字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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