Dancer
Dancer

Reputation: 17671

IOS7 - SLComposeViewController - Include links / Images?

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?

Upvotes: 4

Views: 12490

Answers (2)

Ilario
Ilario

Reputation: 6079

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;
}

Upvotes: 22

Phil
Phil

Reputation: 1018

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];
    }
}

Upvotes: 3

Related Questions