对ShareSDK分享的封装

这几天写项目的时候,发现,有好多的控制器都有分享.每一次,分享的内容虽然不一样,但是格式大致一样,而且代码繁多,出不多几十行.所以考虑之后,就封装了一下.
写一个类,继承NSObject

+ (instancetype)share;

/**
 分享

 @param urlStr 分享的网页URL
 @param imageUrl 图片URL
 @param title 分享标题
 @param descr 分享内容的描述
 */
- (void)shareURL:(NSString *)urlStr image:(NSString *)imageUrl shareTitle:(NSString *)title descr:(NSString *)descr;

在.m中实现方法

// 获取
+ (instancetype)share {
    return [[self alloc] init];
}
// 实现分享的方法
- (void)shareURL:(NSString *)urlStr image:(NSString *)imageUrl shareTitle:(NSString *)title descr:(NSString *)descr {
    self.urlStr = urlStr;
    self.imageUrl = imageUrl;
    self.title = title;
    self.descr = descr;
    // 这里面分享的平台是我们公司要求分享到的. 大家可以根据自己的要求改
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_Qzone)]];
    [[UMSocialShareUIConfig shareInstance] shareCancelControlConfig].isShow = NO;
    [[UMSocialShareUIConfig shareInstance] shareTitleViewConfig].isShow = NO;
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [self shareWebPageToPlatformType:platformType];
    }];
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType {
    LJLog(@"%@",self.urlStr);
    NSURL *url = [NSURL URLWithString:self.imageUrl];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.3f);
    UIImage* shareImg = [UIImage imageWithData:imageData];
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.descr thumImage:shareImg];
    //设置网页地址
    shareObject.webpageUrl = self.urlStr;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:[self getCurrentVC] completion:^(id data, NSError *error) {
        if (error) {
            [YLAlertViewTool showAlertViewWithTitle:@"分享失败" message:nil cancelBtnTitle:@"确定"];
        } else {
            [YLAlertViewTool showAlertViewWithTitle:@"分享成功" message:nil cancelBtnTitle:@"确定"];
        }
    }];
}

好多地方都要用到弹框,就自己封装一下了

+ (void)showAlertViewWithTitle:(NSString *)alertTitle message:(NSString *)msg cancelBtnTitle:(NSString *)cancelBtnTitle
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:action];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

获取当前的控制器.如果你用这个方法的地方比较多,那么直接写一个类封装一下吧.

- (UIViewController *)getCurrentVC {
    
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }
    
    id nextResponder = [tempView nextResponder];
    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];
        
        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}

这样就搞定了.来测试一下吧

NSString *strUrl = [NSString stringWithFormat:@"m.51tsg.com/51tsg-classify/product-detail.html?goods_id=%@",self.goods_id];
    NSString *imageUrl = self.goodsDetail.goods_image_more[0][@"image_url"];
    NSString *title = @"买特产,就来51特色购";
    NSString *descr = self.goodsDetail.goods_name;
    YLShareFunction *shareFunction = [YLShareFunction share];
    [shareFunction shareURL:strUrl image:imageUrl shareTitle:title descr:descr];

效果如下

对ShareSDK分享的封装_第1张图片
效果图.png

你可能感兴趣的:(对ShareSDK分享的封装)