IOS7 - SLComposeViewController - 包含链接/图像?

时间:2013-11-14 14:41:55

标签: ios facebook twitter slcomposeviewcontroller

我代表客户建立了一个IOS7 Native应用程序 - 用于健身教练。

简报要求客户可以在社交上分享进度更新 - 其中包括指导教师网站的链接以帮助推广,例如 - 'Joe ran 3000 miles with the help of Debbie Personal Trainer',最好是培训师的一小部分照片。

我查看了SLComposeViewController并且可以轻松创建推文字符串但我不知道如何添加网址和图片 - 有人知道它是否可能吗?

2 个答案:

答案 0 :(得分:22)

导入框架<Twitter/Twitter.h><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;
}

答案 1 :(得分:3)

这与llario几乎完全相同,但遵循Apple文档说明并采用防御性编码并进行一些额外的错误检查。

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