4

I'm building an IOS7 Native app on behalf of a client - its for Fitness Instructors.

The brief requests that the clients can socially share progress updates - which include a link to the instructors site to help promotion, for example - 'Joe ran 3000 miles with the help of Debbie Personal Trainer' and ideally a little pic of the trainer.

I've looked at the SLComposeViewController and can easily create the tweet string but I don't know how to add a URL and image to this - does anyone know if its possible?

Sujay
  • 2,510
  • 2
  • 27
  • 47
Dancer
  • 17,035
  • 38
  • 129
  • 206

2 Answers2

22

Import framework <Twitter/Twitter.h> and <Social/Social.h>.

-(void)sendFacebook:(id)sender {

    SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [composeController setInitialText:@"look me"];
    [composeController addImage:[UIImage imageNamed:@"image.png"]];
    [composeController addURL: [NSURL URLWithString:@"http://www.apple.com"]];

    [self presentViewController:composeController animated:YES completion:nil];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled) {
            NSLog(@"delete");
        } else  {
            NSLog(@"post");
        }

    //    [composeController dismissViewControllerAnimated:YES completion:Nil];
      };
        composeController.completionHandler =myBlock;
}

- (void)sendTwitter:(id)sender {

    SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    [composeController setInitialText:@"look me"];
    [composeController addImage:[UIImage imageNamed:@"image.png"]];
    [composeController addURL: [NSURL URLWithString:
                                @"http://www.apple.com"]];

    [self presentViewController:composeController
                       animated:YES completion:nil];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled) { 
            NSLog(@"delete"); 
        } else {
            NSLog(@"post");
        }
     //   [composeController dismissViewControllerAnimated:YES completion:Nil];
      };
        composeController.completionHandler =myBlock;
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Ilario
  • 5,979
  • 2
  • 32
  • 46
  • top notch cheers! - only slight issue is I get a 'no visible @interface for SLComposeViewControllerCompletionHandler declares the selector dismissviewcontroller:animated – Dancer Nov 14 '13 at 15:27
  • @Dancer i'm sorry, I am confused. I edited my answer, look at the first few lines, I changed the import to do – Ilario Nov 14 '13 at 15:36
  • its the line - [composeController dismissViewController:composeController Animated:YES completion:Nil]; that is erroring for me with the above - if i comment it out it still works though.. – Dancer Nov 14 '13 at 15:48
  • actually - ignore - i cut and paste the same line from the twitter method and it works fine - must have been a typo somewhere - thanks very much for your help.. – Dancer Nov 14 '13 at 15:52
  • 1
    @Dancer I'm looking at now, and it is precisely the error that you have noticed .. now edited the reply .. thank's you're welcome! – Ilario Nov 14 '13 at 15:54
  • 1
    Thanks, this saved me from having to do a lot of experimentation! – undetected Feb 17 '14 at 14:20
  • 1
    This answer should remove the "dismissViewControllerAnimated" line due to iOS7 specific instructions in the docs: "In iOS 7 and later when compiled with the iOS 7 or later SDK you must not dismiss the SLComposeViewController in your completion handler—the system will do so automatically." – Phil May 02 '14 at 19:26
3

This is almost the same answer as llario, but follows Apple docs instructions and employs defensive coding with some additional error checking.

#import <Social/Social.h>

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
    SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    if (composeViewController) {
        [composeViewController addImage:[UIImage imageNamed:@"MyImage"]];
        [composeViewController addURL:[NSURL URLWithString:@"http://www.google.com"]];
        NSString *initialTextString = @"Check out this Tweet!";
        [composeViewController setInitialText:initialTextString];
        [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
            if (result == SLComposeViewControllerResultDone) {
                NSLog(@"Posted");
            } else if (result == SLComposeViewControllerResultCancelled) {
                NSLog(@"Post Cancelled");
            } else {
                NSLog(@"Post Failed");
            }
        }];
        [self presentViewController:composeViewController animated:YES completion:nil];
    }
}
Phil
  • 1,018
  • 10
  • 10