سؤال

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 a UITextView. 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 the SLComposeViewController. While you're here make a iVar for the permanent string as well.

@interface ViewController : UIViewController <UITextViewDelegate> //very important!
{
    UITextView *sharingTextView;
    NSString *permanentText;
}

Then in viewDidLoad you can set up what you want the permanent text to be:

- (void)viewDidLoad
{
    [super viewDidLoad];
    permanentText = @"http://www.stackoverflow.com/";
}

The code below is a pretty basic IBAction to present the composer with a couple of slight tweaks. First, you'll notice that setInitialText 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 the UITextView 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 the UITextViewDelegate method shouldChangeTextInRange.

- (IBAction)exampleUsingFacebook:(UIButton *)sender {

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *sharingComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
            [sharingComposer dismissViewControllerAnimated:YES completion:nil];
        };
        [sharingComposer setCompletionHandler:completionHandler];
        [sharingComposer setInitialText:[NSString stringWithFormat:@"%@ %@",[[self QText]text],permanentText]];

        [self presentViewController:sharingComposer animated:YES completion:^{
            for (UIView *viewLayer1 in sharingComposer.view.subviews) {
                for (UIView *viewLayer2 in viewLayer1.subviews) {
                    if ([viewLayer2 isKindOfClass:[UIView class]]) {
                        for (UIView *viewLayer3 in viewLayer2.subviews) {
                            if ([viewLayer3 isKindOfClass:[UITextView class]]) {
                                [(UITextView *)viewLayer3 setDelegate:self];
                                sharingTextView = (UITextView *)viewLayer3;
                            }
                        }
                    }
                }
            }
        }];
    }
}

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.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView == sharingTextView) {
        NSRange substringRange = [textView.text rangeOfString:permanentText];
        if (range.location >= substringRange.location && range.location <= substringRange.location + substringRange.length) {
            return NO;
        }
    }
    return YES;
}

Hope this helps!

نصائح أخرى

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?

UITextField *textField;
NSString *enteredText = [textField text];
NSString *finalPost = [NSString stringWithFormat:@"%@ Your permanent text/URL", enteredText];

Now post the "finalPost" string to Facebook or Twitter.

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.

SLComposeViewController *slComposeThirdViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeThirdViewController setInitialText:@"initial Text"];
[slComposeThirdViewController addURL:[NSURL URLWithString:@"http://stackoverflow.com/"]];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top