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?

廉价感情. 提交于 2020-06-17 14:36:09

问题


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

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)
    }

}

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

Thanks for any help.


回答1:


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

Example:

    <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>

All keys you can find here

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



来源:https://stackoverflow.com/questions/62242952/how-to-read-the-string-in-an-attached-file-not-sharing-the-string-directly-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!