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
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:
(code) Change ShareViewController
to simple UIViewController
(code) Add blur effect to ShareViewController
(storyboard) Add container view to ShareViewController
(storyboard) Add navigation controller
(storyboard) Embed navigation controller in ShareViewController
's container view
Customize the view controllers in the navigation controller (see this SO thread for example)
ShareViewController
to simple UIViewController
import UIKit
class ShareViewController: UIViewController {
// ^^^^^^^^^^^^^^^^
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.
}
ShareViewController
Drag a Container View
from the Object Library into the ShareViewController
on the storyboard, and adjust dimension. For example:
Drag a Navigation Controller from the Object Library to the storyboard.
ShareViewController
's container viewControl-drag from the container view of ShareViewController
to the navigation controller, select "Embed" from the menu. Should look similar to this:
My result:
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 []
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With