勤之时 - 表示层(三)

2017-03-28  本文已影响102人  启发禅悟

应用很早就上线了,欢迎大家下载使用:http://itunes.apple.com/app/id1206687109

源码已经公开,大家可以去https://github.com/Inspirelife96/ILDiligence下载。 喜欢的话Fork或者给个Star,非常感谢。

下面是这一系列的全部帖子:
想法和原型
勤之时 - 架构与工程组织结构
勤之时 - 数据持久层的实现
勤之时 - 网络层的实现
勤之时 - 业务逻辑层
勤之时 - Info.plist的改动
勤之时 - 表示层(一)
勤之时 - 表示层(二)
勤之时 - 表示层(三)
勤之时 - 表示层(四)
勤之时 - 表示层(五)

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

Story Controllers.png

【今日故事】View Controller

MVC设计考虑:

详细编码:
ILDSharedView的编码:

@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 编码:

@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];
                   }
               }];
}
上一篇下一篇

猜你喜欢

热点阅读