Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share Extension - Remove Textfield

Tags:

ios

swift

Is there a way to remove the TextView- and PreviewPart, marked in RED, from my ShareExtention?

Im using the default:

class ShareViewController: SLComposeServiceViewController

As ViewController

I want to just show the Destination Selector like below (I know how to create that) I know that you can create your own ViewController but it would be very hard to rebuild the default one.

The Second thing is: What method would you recommend to Transfer/Copy Data/Files and which to read the Main App's Document Directory?

I want to transfer/copy the selected Document to the DocumentsDirectory of my Main App. Both are in the same AppGroup. Bc if I just save the Url of the current Document in UserDefault, then I guess the Main App can't access it.

I need to read the Main App's Document Directory, because I need the file Hierachy so I can select the saving location.

Note: Just because there was some confusion in the comments for whatever reason: This is my own ShareExtention and not smo else's in UIActivityController

Screenshot

like image 350
John Smith Avatar asked Oct 06 '17 12:10

John Smith


Video Answer


2 Answers

What worked for me is to use a custom solution instead of SLComposeServiceViewController. It looks the same, and can add any element I want (see image at the end and here's the code at the commit when it was implemented).

Steps:

  1. (code) Change ShareViewController to simple UIViewController

  2. (code) Add blur effect to ShareViewController

  3. (storyboard) Add container view to ShareViewController

  4. (storyboard) Add navigation controller

  5. (storyboard) Embed navigation controller in ShareViewController's container view

  6. Customize the view controllers in the navigation controller (see this SO thread for example)


Step 1. Change ShareViewController to simple UIViewController

import UIKit

class ShareViewController: UIViewController {
//                         ^^^^^^^^^^^^^^^^

Step 2. Add blur effect to ShareViewController

    // ShareViewController continued from Step 1.

    override func viewDidLoad() {
        super.viewDidLoad()

        // https://stackoverflow.com/questions/17041669/creating-a-blurring-overlay-view/25706250

        // only apply the blur if the user hasn't disabled transparency effects
        if UIAccessibilityIsReduceTransparencyEnabled() == false {
            view.backgroundColor = .clear

            let blurEffect = UIBlurEffect(style: .dark)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            //always fill the view
            blurEffectView.frame = self.view.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            view.insertSubview(blurEffectView, at: 0)
        } else {
            view.backgroundColor = .black
        }
        // Do any additional setup after loading the view.
    }

Step 3. Add container view to ShareViewController

Drag a Container View from the Object Library into the ShareViewController on the storyboard, and adjust dimension. For example:

enter image description here

Step 4. Add navigation controller

Drag a Navigation Controller from the Object Library to the storyboard.

Step 5. Embed navigation controller in ShareViewController's container view

Control-drag from the container view of ShareViewController to the navigation controller, select "Embed" from the menu. Should look similar to this:

enter image description here

Step 6. Customize the view controllers in the navigation controller (see this SO thread for example)

My result:

enter image description here

like image 72
toraritte Avatar answered Oct 27 '22 02:10

toraritte


Not the best, but here's what I've got based on the docs:

override func viewDidLoad() {
    super.viewDidLoad()
    textView.isHidden = true
}

But then there's blank space. Perhaps it's better to put some placeholder text in and prevent user input. There are other options in the docs you can use for that, or you can mess with textView (which is a UITextView)...

Edit: Here's a complete example that shows the URL in the text field, grey and non-user-interactible.

class ShareViewController: SLComposeServiceViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        textView.isUserInteractionEnabled = false
        textView.textColor = UIColor(white: 0.5, alpha: 1)
        textView.tintColor = UIColor.clear // TODO hack to disable cursor
        getUrl { (url: URL?) in
            if let url = url {
                DispatchQueue.main.async {
                    // TODO this is also hacky
                    self.textView.text = "\(url)"
                }
            }
        }
    }

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

    func getUrl(callback: @escaping ((URL?) -> ())) {
        if let item = extensionContext?.inputItems.first as? NSExtensionItem,
            let itemProvider = item.attachments?.first as? NSItemProvider,
            itemProvider.hasItemConformingToTypeIdentifier("public.url") {
            itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { (url, error) in
                if let shareURL = url as? URL {
                    callback(shareURL)
                }
            }
        }
        callback(nil)
    }

    override func didSelectPost() {
        getUrl { (url: URL?) in
            if let url = url {
                DispatchQueue.main.async {
                    print("url: \(url)")
                }
            }
        }
    }

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

}

lol yahoo yahoo sucks

like image 22
sudo Avatar answered Oct 27 '22 02:10

sudo