勤之时 - 表示层(三)
2017-03-28 本文已影响102人
启发禅悟
应用很早就上线了,欢迎大家下载使用:http://itunes.apple.com/app/id1206687109
源码已经公开,大家可以去https://github.com/Inspirelife96/ILDiligence下载。 喜欢的话Fork或者给个Star,非常感谢。
下面是这一系列的全部帖子:
想法和原型
勤之时 - 架构与工程组织结构
勤之时 - 数据持久层的实现
勤之时 - 网络层的实现
勤之时 - 业务逻辑层
勤之时 - Info.plist的改动
勤之时 - 表示层(一)
勤之时 - 表示层(二)
勤之时 - 表示层(三)
勤之时 - 表示层(四)
勤之时 - 表示层(五)
这一节讲分享的部分。功能描述如下:
Story Controllers.png【今日故事】View Controller
- 背景图为每日故事的图片
- 左下角为一个二维码,扫描或者识别二维码跳转到APPLE Store的改应用的下载页面。
- 右下角为今日日期以及今日故事的标题。
- 二维码上部为今日故事的介绍。
- 左上角为【关闭】按钮
- 右上角为【分享】按钮,点击弹出分享菜单,如右图。
- 分享使用了第三方库:ShareSDK
MVC设计考虑:
- Controller:
- ILDStoryViewController: 就是整个今日故事的VC。
- Model:
- ILDStoryModel:可以通过[[ILDStoryDataCenter sharedInstance] prepareStoryModel]获得。
- View:
- ILDSharedView:即背景图+二维码+日期+今日故事标题+今日故事介绍,用来分享的图片。
详细编码:
ILDSharedView的编码:
- 没什么复杂的,主要还是布局的问题。背景图,二维码,日期,标题,介绍为5个需要添加的控件。
@interface ILDSharedView()
@property (nonatomic, strong) UIImageView *storyImageView;
@property (nonatomic, strong) UIImageView *codeImage;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *storyTitleLabel;
@property (nonatomic, strong) UILabel *storyDetailLabel;
@end
- 初始化函数
- (instancetype)initWithStoryImage:(UIImage *)image storyTitle:(NSString *)storyTitle storyDetail:(NSString *)storyDetail {
if (self = [super init]) {
self.storyImageView.image = image;
self.storyTitleLabel.text = storyTitle;
self.storyDetailLabel.text = storyDetail;
[self addSubview:self.storyImageView];
[self addSubview:self.codeImage];
[self addSubview:self.dateLabel];
[self addSubview:self.storyTitleLabel];
[self addSubview:self.storyDetailLabel];
}
return self;
}
- 其他就是控件的初始化以及布局了。不详细描述了。
ILDStoryViewController 编码:
- 控件及Model:除了基本的关闭,分享按钮,以及Title,主要就是把SharedView添加进来。 Model方面,获取storyModel即可。
@interface ILDStoryViewController ()
@property(nonatomic, strong) UIButton *closeButton;
@property(nonatomic, strong) UIButton *sharingButton;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) ILDSharedView *sharedView;
@property(nonatomic, strong) ILDStoryModel *storyModel;
分享按钮的事件:
- (void)clickCloseButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)clickSharingButton:(id)sender {
UIImage *sharedImage = [self.sharedView il_viewToImage];
[ILDShareSDKHelper shareMessage:self.storyModel.todaysTitle image:sharedImage onView:self.sharingButton];
}
这里定义了一个ILDShareSDKHelper类:
主要定义了:
+ (void)initShareSDK;
+ (void)shareMessage:(NSString *)message image:(UIImage *)image onView:(UIView *)view;
实现如下:
+ (void)initShareSDK {
[ShareSDK registerApp:kShareSDKApplicationId
activePlatforms:@[
@(SSDKPlatformTypeMail),
@(SSDKPlatformTypeSMS),
@(SSDKPlatformTypeWechat),
@(SSDKPlatformTypeQQ),
]
onImport:^(SSDKPlatformType platformType) {
switch (platformType)
{
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class] delegate:self];
break;
case SSDKPlatformTypeQQ:
[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
switch (platformType)
{
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId:kWXApplicationId
appSecret:kWXApplicationSecret];
break;
case SSDKPlatformTypeQQ:
[appInfo SSDKSetupQQByAppId:kQQApplicationId
appKey:kQQApplicationSecret
authType:SSDKAuthTypeBoth];
default:
break;
}
}];
}
+ (void)shareMessage:(NSString *)message image:(UIImage *)image onView:(UIView *)view {
UIViewController *currentController = [view getCurrentViewController];
//1、创建分享参数(必要)
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
NSArray* imageArray = @[image];
[shareParams SSDKSetupShareParamsByText:message
images:imageArray
url:[NSURL URLWithString:kAppURL]
title:@"勤之时"
type:SSDKContentTypeAuto];
[shareParams SSDKSetupWeChatParamsByText:message title:@"勤之时" url:[NSURL URLWithString:kAppURL] thumbImage:[UIImage imageNamed:@"Icon-share.png"] image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
[shareParams SSDKSetupWeChatParamsByText:message title:@"勤之时" url:[NSURL URLWithString:kAppURL] thumbImage:[UIImage imageNamed:@"Icon-share.png"] image:image musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeWechatSession];
[shareParams SSDKSetupQQParamsByText:message title:@"勤之时" url:[NSURL URLWithString:kAppURL] thumbImage:[UIImage imageNamed:@"Icon-share.png"] image:image type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeQZone];
[shareParams SSDKSetupQQParamsByText:message title:@"勤之时" url:[NSURL URLWithString:kAppURL] thumbImage:[UIImage imageNamed:@"Icon-share.png"] image:image type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeQQFriend];
//2、分享
[ShareSDK showShareActionSheet:view
items:nil
shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
switch (state) {
case SSDKResponseStateBegin:
{
[MBProgressHUD showHUDAddedTo:currentController.view animated:YES];
break;
}
case SSDKResponseStateSuccess:
{
//
}
case SSDKResponseStateFail:
{
if (!error) {
break;
}
if (platformType == SSDKPlatformTypeSMS && [error code] == 201) {
[currentController presentAlertTitle:@"分享失败" message:@"失败原因可能是:1、短信应用没有设置帐号;2、设备不支持短信应用;3、短信应用在iOS 7以上才能发送带附件的短信。"];
break;
} else if(platformType == SSDKPlatformTypeMail && [error code] == 201) {
[currentController presentAlertTitle:@"分享失败" message:@"失败原因可能是:1、邮件应用没有设置帐号;2、设备不支持邮件应用;"];
break;
} else {
[currentController presentAlertTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error]];
break;
}
}
case SSDKResponseStateCancel:
{
break;
}
default:
break;
}
if (state != SSDKResponseStateBegin) {
[MBProgressHUD hideHUDForView:currentController.view animated:YES];
}
}];
}