勤之时 - 表示层(三)

这一节讲分享的部分。功能描述如下:

【今日故事】View Controller

  • 背景图为每日故事的图片
  • 左下角为一个二维码,扫描或者识别二维码跳转到APPLE Store的改应用的下载页面。
  • 右下角为今日日期以及今日故事的标题。
  • 二维码上部为今日故事的介绍。
  • 左上角为【关闭】按钮
  • 右上角为【分享】按钮,点击弹出分享菜单,如右图。
  • 分享使用了第三方库:ShareSDK

MVC设计考虑:

  • Controller:
    • ILDStoryViewController: 就是整个今日故事的VC。
  • Model:
    • ILDStoryModel:可以通过[[ILDStoryDataCenter sharedInstance] prepareStoryModel]获得。
  • View:
    • ILDSharedView:即背景图+二维码+日期+今日故事标题+今日故事介绍,用来分享的图片。

详细编码:
ILDSharedView的编码:

  • 没什么复杂的,主要还是布局的问题。背景图,二维码,日期,标题,介绍为5个需要添加的控件。
1
2
3
4
5
6
7
8
9
@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
  • 初始化函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (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即可。
1
2
3
4
5
6
7
8
@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;

分享按钮的事件:

1
2
3
4
5
6
7
8
- (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类:
主要定义了:

1
2
+ (void)initShareSDK;
+ (void)shareMessage:(NSString *)message image:(UIImage *)image onView:(UIView *)view;

实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
+ (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];
}
}];
}