@ [toc]
2.9 通过社交平台分享图片
如果LightWork成功保存图片到图库,我们要向用户展示一个包含分享菜单的弹窗.以下代码将检查程序对几种社交平台是否提供支持,并显示弹框,该弹框会每个支持的社交平台创建一个操作按钮.这些操作按钮虽然分享到不同的平台,但是他们的创建方式很相似,所以,我们用一个帮助方法shareImageActionWithTitle:serviceType:image:
来创建这些行为按钮.我们还提供一个Do not share的操作按钮,该按钮不进行分享,直接停止程序的繁忙模式:
- (void)showSaveImageSuccessAlertWithImage:(UIImage *)image {
// Create a "Saved image" alert.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Saved image" message:@"The image has been added to your Photos library. Would you like to share it with your friends?" preferredStyle:UIAlertControllerStyleAlert];
// If the user has a Facebook account on this device, add a
// "Post on Facebook" button to the alert.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
UIAlertAction* facebookAction = [self shareImageActionWithTitle:@"Post on Facebook" serviceType:SLServiceTypeFacebook image:image];
[alert addAction:facebookAction];
}
// If the user has a Twitter account on this device, add a
// "Tweet" button to the alert.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
UIAlertAction* twitterAction = [self shareImageActionWithTitle:@"Tweet" serviceType:SLServiceTypeTwitter image:image];
[alert addAction:twitterAction];
}
// If the user has a Sina Weibo account on this device, add a
// "Post on Sina Weibo" button to the alert.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
UIAlertAction* sinaWeiboAction = [self shareImageActionWithTitle:@"Post on Sina Weibo" serviceType:SLServiceTypeSinaWeibo image:image];
[alert addAction:sinaWeiboAction];
}
// If the user has a Tencent Weibo account on this device, add a
// "Post on Tencent Weibo" button to the alert.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTencentWeibo]) {
UIAlertAction* tencentWeiboAction = [self shareImageActionWithTitle:@"Post on Tencent Weibo" serviceType:SLServiceTypeTencentWeibo image:image];
[alert addAction:tencentWeiboAction];
}
// Add a "Do not share" button to the alert.
UIAlertAction* doNotShareAction = [UIAlertAction actionWithTitle:@"Do not share" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self stopBusyMode];
}];
[alert addAction:doNotShareAction];
// Show the alert.
[self presentViewController:alert animated:YES completion:nil];
}
当用户点击了分享按钮,会呈现一个iOS标准组合对话框,该组合框会连接到指定的社交平台.我们还会吧图像添加到组合框中.当用户取消该组合框时,我们要确保LightWork停止繁忙模式.下面是构建操作按钮的相关帮助方法:
- (UIAlertAction *)shareImageActionWithTitle:(NSString *)title serviceType:(NSString *)serviceType image:(UIImage *)image {
UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:serviceType];
[composeViewController addImage:image];
[self presentViewController:composeViewController animated:YES completion:^{
[self stopBusyMode];
}];
}];
return action;
}