勤之時 - 表示層(三)

應用很早就上線了,歡迎大家下載使用: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];
                   }
               }];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容