Right now, our code is set to grab the text from the UITextField as setInitialText for Facebook/Twitter posts.
What we want to do is: add an additional permanent message or URL to the Facebook/Twitter posts.
How can we do this? Here’s our current code:
[slComposeThirdViewController setInitialText:[[self QText]text]];
[self presentViewController:slComposeThirdViewController animated:YES completion:nil];
It’s a little involved, so bear with me here… and this only works for SLServiceTypeTwitter
For anyone reading this that is interested in using this, I’ve put a sample project on Github: https://github.com/NSPostWhenIdle/Immutable-SLComposeViewController
The first thing you’ll want to do is make sure that your view controller conforms to
UITextViewDelegate. You’ll also want to create an iVar for aUITextView. You won’t actually be creating a text view, but you’ll want to have a pointer to assign directly to the text view inside theSLComposeViewController. While you’re here make a iVar for the permanent string as well.Then in viewDidLoad you can set up what you want the permanent text to be:
The code below is a pretty basic
IBActionto present the composer with a couple of slight tweaks. First, you’ll notice thatsetInitialTextuses a formatted string the append the permanent text to the end of the contents of the text field with a space added in between.Then comes the important part! I’ve added a loop to
presentViewController:‘s completion handler to cycle through some subviews of subviews of subviews in order to identify theUITextViewin the composer that contains the sharing text. This needs to be done so you can set that text view’s delegate in order to access theUITextViewDelegatemethodshouldChangeTextInRange.Important: Please note that the above will only work if placed in the completion handler.
Below is an example of how to set up
shouldChangeTextInRangeto compare the range that the user is attempting to edit to the range that contains your permanent text. By doing so, the user will be able to make changes to any part of the text that they want… except for the part that contains your permanent text. You’ll also notice that inside this method I’ve compared textView to shareingTextView, the pointer we assigned to the text view inside the composer. Doing so will allow you to use other text views within this controller without them following the same rules I’ve configured for the text view inside the composer.Hope this helps!