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];
You can roll your own composer, append your text and then use SLRequest to actually submit it to the service.
How about capturing the user entered text from the UITextField and then constructing a final string which appends the permanent message you want from it?
Now post the "finalPost" string to Facebook or Twitter.
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
IBAction
to present the composer with a couple of slight tweaks. First, you'll notice thatsetInitialText
uses 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 theUITextView
in 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 theUITextViewDelegate
methodshouldChangeTextInRange
.Important: Please note that the above will only work if placed in the completion handler.
Below is an example of how to set up
shouldChangeTextInRange
to 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!
If you want to append the text with URL, just use the addURL: method. It won't display any text in the SLComposeViewController's view. But will add the URL at the end after publishing users tweet.