我正在代表客户构建一个 IOS7 Native 应用程序 - 它适用于健身教练。
简报要求客户可以在社交上分享进度更新——其中包括一个指向教练网站的链接以帮助提升,例如—— “乔在黛比私人教练的帮助下跑了 3000 英里”,最好是教练的小照片。
我查看了SLComposeViewController
并且可以轻松创建推文字符串,但我不知道如何向其中添加 URL 和图像 - 有人知道它是否可能吗?
我正在代表客户构建一个 IOS7 Native 应用程序 - 它适用于健身教练。
简报要求客户可以在社交上分享进度更新——其中包括一个指向教练网站的链接以帮助提升,例如—— “乔在黛比私人教练的帮助下跑了 3000 英里”,最好是教练的小照片。
我查看了SLComposeViewController
并且可以轻松创建推文字符串,但我不知道如何向其中添加 URL 和图像 - 有人知道它是否可能吗?
导入框架<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;
}
这与 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];
}
}