InviteController.m 4.8 KB
//
//  InviteController.m
//  DreamSleep
//
//  Created by peter on 2022/4/18.
//

#import "InviteController.h"
#import <YYWebImage/YYWebImage.h>
#import <UMShare/UMShare.h>

@interface InviteController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) YYAnimatedImageView *animatedView;
@end

@implementation InviteController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"邀请好友";
    self.view.backgroundColor = DSWhite;
    [self.view addSubview:self.scrollView];
    
    UIButton *shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [shareBtn setImage:[UIImage imageNamed:@"shareIcon"] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
}

- (void)shareAction {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"模拟分享" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"微信好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self shareTextToPlatformType:UMSocialPlatformType_WechatSession];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"朋友圈" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self shareTextToPlatformType:UMSocialPlatformType_WechatTimeLine];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"QQ好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self shareTextToPlatformType:UMSocialPlatformType_QQ];
    }];
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertVC addAction:action1];
    [alertVC addAction:action2];
    [alertVC addAction:action3];
    [alertVC addAction:action4];
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - 友盟分享
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType {
    // 创建分享消息对象
    UMSocialMessageObject*messageObject = [UMSocialMessageObject messageObject];
    // 创建图片内容对象
    UMShareImageObject*shareObject =[[UMShareImageObject alloc] init];
    // 如果有缩略图,则设置缩略图
    shareObject.thumbImage = [UIImage imageNamed:@"icon"];
    UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"invite_friend.webp"]];
    [shareObject setShareImage:img];
    // 分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    // 调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data,NSError*error) {
        if (error) {
            if (error.code == 2008) {
                [DSProgressHUD showToast:(platformType == UMSocialPlatformType_QQ) ? @"您的设备未安装QQ" : @"您的设备未安装微信"];
            }
            UMSocialLogInfo(@"************Share fail with error %@*********", error);
        } else {
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                // 分享结果消息
                UMSocialLogInfo(@"response message is %@", resp.message);
                // 第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@", resp.originalResponse);
            } else {
                UMSocialLogInfo(@"response data is %@", data);
            }
        }
    }];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopHeight(0))];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_scrollView addSubview:self.animatedView];
    }
    return _scrollView;
}

- (YYAnimatedImageView *)animatedView {
    if (!_animatedView) {
        UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"invite_friend.webp"]];
        _animatedView = [[YYAnimatedImageView alloc] initWithFrame:self.scrollView.bounds];
        CGFloat h = kScreenWidth * img.size.height / img.size.width;
        _animatedView.height = h;
        _scrollView.contentSize = CGSizeMake(kScreenWidth, h);
        _animatedView.image = img;
    }
    return _animatedView;
}

@end