13

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

John Smith
  • 573
  • 6
  • 22
  • In my experience, there is no way to remove the the area highlighted in red. You will need to ask the user's permission to post to Twitter on their behalf, then you can do the posting behind the scenes. This would no involve a ShareExtension, but Twitter's SDK. As far as storing the Documents, you can write those to a file and share through your AppGroup. There are a lot of tutorials on that subject, try doing a Google search. – MSU_Bulldog Oct 06 '17 at 13:55
  • 1
    @MSU_Bulldog If it would be so easy I wouldn't ask. You can only rarely find stuff about ShareExtention-Sending Email or stuff like that. But not Copying Files! If you found something I didn't just Post it – John Smith Oct 06 '17 at 13:59
  • For sharing files through App Groups, this question handles the file sharing through App Groups, hope that works for you: https://stackoverflow.com/a/32653801/3543861 – MSU_Bulldog Oct 06 '17 at 14:09
  • 1
    @MSU_Bulldog Why do you always mention Twitter? I never said that I want to Post anything or use it for SocialSharing. I just want to Import the File to the Main App's Document Dir – John Smith Oct 06 '17 at 14:11
  • John, don't get frustrated at the ONLY person helping you bc you can't figure things out. Thats unprofessional. Read through the post in the link that I posted, that will help you solve your problem. – MSU_Bulldog Oct 06 '17 at 14:15
  • @MSU_Bulldog Im not getting frustrated at you, I only asked why you mentioned Twitter and SocialSharing. Your link doesn't solve the Problem, bc I need to access the Main App's DocumentDir so I can get a File Hierarchy for example – John Smith Oct 06 '17 at 14:18
  • @JohnSmith Were you ever able to figure this out? – toraritte Oct 22 '18 at 16:32
  • 1
    @toraritte You can create an own viewcontroller easily. The only way to modify the default would be to hook the apple orginal function that implements this view but apple won't allow it in the app store. So bc I didn't want to create an own view, I stuck with the orginal view and used the textfield for other purposes. (rename file - optional) – John Smith Oct 24 '18 at 19:41

3 Answers3

1

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

sudo
  • 5,604
  • 5
  • 40
  • 78
1

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

toraritte
  • 6,300
  • 3
  • 46
  • 67
-1

You can't. When you use the share extension you will afterwards select an app to share to, thus plugging yourself to the share extension of said app.

Your app does not and cannot have any access of what said share extension does, those are totally separated from the main app runtime.

Also this allow users to customise what they share.

For the second part of your post you need to elaborate, who wants to transfer a document, you as the developper or your end-user?

thibaut noah
  • 1,474
  • 2
  • 11
  • 39
  • I don't thing you know what a share extension is. I think you confuse it with the UIActivityController which is when you share smth from your/my app – John Smith Oct 11 '17 at 13:16
  • @JohnSmith And you think that UIActivityController communicates with what exactly? – thibaut noah Oct 11 '17 at 13:23
  • Idk-, I only asked if smo knows a way to remove the Textfields of my OWN share extension and which is the best way to communicate – John Smith Oct 11 '17 at 13:27
  • My bad, then i'm afraid you have to drop SLComposeServiceViewController altogether. To send the document like i said we need more information, but from what i understand you probably need to pass the document itself – thibaut noah Oct 11 '17 at 13:49
  • Which information and what do you mean with pass document itself? – John Smith Oct 11 '17 at 13:53
  • Do you want for an app A to be able to send a document to your app B with your share extension (the user will be able to do it) or is it another use case? What type of document is it? You probably need to copy the document, best guess without knowing what this is about. – thibaut noah Oct 11 '17 at 14:03
  • I don't know why there is this confusion, bc I thought I explained it well but never mind: This app is almost finished an it's a kind of FileBrowser. So I want the user to be able to import any kind of Document from any other app to my App, that's why I'm using the ShareExtention – John Smith Oct 11 '17 at 14:06
  • Well, then you definitly need to copy it since in most cases you won't be able to have access to the original document. – thibaut noah Oct 11 '17 at 14:07
  • For now I already copied the Apps Doc. Dir to the Shared Folder but the question is if there is a better way, But the Main question was how I can remove the TextField – John Smith Oct 11 '17 at 14:11