美文网首页
OC-友盟更新版本6.9.5后的坑(优化封装)

OC-友盟更新版本6.9.5后的坑(优化封装)

作者: SK丿希望 | 来源:发表于2018-11-30 15:43 被阅读0次

    先来封装的使用方法吧

        HWShareItem *item = [[HWShareItem alloc] init];
        item.title = @"我是标题";
        item.content = @"我是内容";
        item.url = @"https://www.hao123.com";
        item.image = [UIImage hw_imageNamed:@"九真医道"];
    //    item.shareType = ShareTypeUIActivity; // 如果不设置默认未友盟 实在=ShareTypeUIActivity调用系统分享
        [HWShareManage showShareUIWithItem:item];
    

    问题

    通过Cocoapods更新版本后

      # U-Share SDK UI模块(分享面板,建议添加)
      pod 'UMCShare/UI'
      # 集成微信(精简版0.2M)
      pod 'UMCShare/Social/ReducedWeChat'
      # 集成QQ/QZone/TIM(精简版0.5M)
      pod 'UMCShare/Social/ReducedQQ'
    

    报错我晕(下面方法没了)

    /* 设置友盟appkey */
        [[UMSocialManager defaultManager] setUmSocialAppkey:@"友盟Appkey"];
    

    我还以为不要了,还以为友盟这么友好,试运行了一下,结果嘿嘿,废了

    image.png

    怎么办,不要急,.h文件都看了下发现,这个

    image.png
    试一试可以
    [UMConfigure initWithAppkey:UMAppkey channel:@"App Store"]
    

    下面进行封装,直接代码了 设置还是看这篇
    AppDelegate进行扩展

    #import "AppDelegate.h"
    
    #define UMAppkey @""
    #define WXAppkey @""
    #define WXAppSecret @""
    #define QQAppkey @""
    #define QQAppSecret @""
    
    @interface AppDelegate (HWShare)
    -(void)setUpUM;
    @end
    

    .m文件 (回调方法好像给废弃了 暂时还没有去研究)

    #import "AppDelegate+HWShare.h"
    #import <UMCommon/UMCommon.h>
    #import <UShareUI/UShareUI.h>
    #import <UMShare/UMShare.h>
    
    @implementation AppDelegate (HWShare)
    
    - (void)setUpUM{
        /* 打开调试日志 */
        [[UMSocialManager defaultManager] openLog:YES];
        /* 设置友盟appkey */
        [UMConfigure initWithAppkey:UMAppkey channel:@"App Store"];
        if (WXAppkey.length > 0) {
            [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WXAppkey appSecret:WXAppSecret redirectURL:nil];
        }
        if (QQAppkey.length > 0) {
            [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAppkey appSecret:QQAppSecret redirectURL:nil];
        }
    }
    @end
    

    分享管理.h文件

    #import <Foundation/Foundation.h>
    
    
    NS_ASSUME_NONNULL_BEGIN
    typedef NS_OPTIONS(NSUInteger, ShareType) {
        ShareTypeDefault = 0, // 友盟
        ShareTypeUIActivity, // 系统 不可自定义界面
    };
    @interface HWShareItem : NSObject
    /** 分享的链接 */
    @property(nonatomic, strong) NSString *url;
    /** 分享的标题 */
    @property(nonatomic, strong) NSString *title;
    /** 分享的内容 */
    @property(nonatomic, strong) NSString *content;
    /** 分享的网络图标 */
    @property(nonatomic, strong) NSString *icon;
    /** 分享图片 优先取image */
    @property(nonatomic, strong) UIImage *image;
    /** 分享类型 没有设置默认友盟 */
    @property(nonatomic, assign) ShareType shareType;
    
    @end
    
    @interface HWShareManage : NSObject
    
    + (void)showShareUIWithItem:(HWShareItem *)item;
    
    @end
    
    
    NS_ASSUME_NONNULL_END
    

    .m文件

    #import "HWShareManage.h"
    #import <Social/Social.h>
    #import "AppDelegate+HWShare.h"
    #import <UShareUI/UShareUI.h>
    #import <UMShare/UMShare.h>
    
    @implementation HWShareItem
    @end
    @implementation HWShareManage
    
    + (void)showShareUIWithItem:(HWShareItem *)item {
        //分享的标题
        if (item.shareType == ShareTypeUIActivity) {
            [self showActivityUI:item];
        } else { // 友盟
            // 显示分享面板
            NSMutableArray *array = [NSMutableArray array];
            if (WXAppkey.length > 0) {
                [array addObjectsFromArray:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine)]];
            }
            if (QQAppkey.length > 0) {
                [array addObjectsFromArray:@[@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_Qzone)]];
            }
            [UMSocialUIManager setPreDefinePlatforms:array];
            [UMSocialUIManager  showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
                // 根据获取的platformType确定所选平台进行下一步操作
                [self shareWebPageToPlatformType:platformType andItem:item];
            }];
        }
    }
    
    + (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType andItem:(HWShareItem *)item{
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        // 判断是否有图片
        if (item.image || item.icon) { // 有图 可能是网页与图片
            messageObject.title = item.title;
            messageObject.text = item.content;
            UIImage *image = [self getImage:item];
            if (item.url) {
                //创建网页内容对象
                UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:item.title descr:item.content thumImage:image];
                //设置网页地址
                shareObject.webpageUrl = item.url;
                //分享消息对象设置分享内容对象
                messageObject.shareObject = shareObject;
            } else {
                UMShareImageObject *shareObject = [UMShareImageObject shareObjectWithTitle:item.title descr:item.content thumImage:image];
                shareObject.shareImage = image;
                //分享消息对象设置分享内容对象
                messageObject.shareObject = shareObject;
            }
        } else { // 没图 文本分享
            if (item.title) {
                messageObject.title = item.title;
            }
            if (item.content) {
                messageObject.text = item.content;
            } else { // 如果没有内容就不分享了
                return;
            }
        }
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:[UIApplication sharedApplication].keyWindow.rootViewController completion:^(id data, NSError *error) {
            if (error) {
                HWLog(@"失败");
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    HWLog(@"成功");
                    UMSocialShareResponse *resp = data;
                    //分享结果消息
                    UMSocialLogInfo(@"response message is %@",resp.message);
                    //第三方原始返回的数据
                    UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                    HWLog(@"失败");
                }
            }
        }];
    }
    + (UIImage *)getImage:(HWShareItem *)item {
        UIImage *image;
        if (item.image) {
            image = item.image;
        } else if (item.icon.length > 0) {
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.icon]];
            image = [UIImage imageWithData:data];
        } else {
            image = [UIImage imageNamed:@"默认图片"];
        }
        return image;
    }
    + (void)showActivityUI:(HWShareItem *)item {
        if (!item.title || !item.url) {return;}
        if (!item.image && !item.icon) {return;}
        NSString *textToShare = item.title;
        //分享的图片
        UIImage *imageToShare = [self getImage:item];
        //分享的url
        NSURL *urlToShare = [NSURL URLWithString:item.url];
        // 分享的图片不能为空 //在这里呢 如果想分享图片 就把图片添加进去  文字什么的通上
        NSArray *activityItems = @[textToShare, imageToShare, urlToShare];
        UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
        // 排除(UIActivityTypeAirDrop)AirDrop 共享、(UIActivityTypePostToFacebook)Facebook //不出现在活动项目
        activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVC animated:YES completion:nil];
        // 通过block接收结果处理
        UIActivityViewControllerCompletionWithItemsHandler completionHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
            if (completed) {
                //分享 成功
            }else{
                //分享 取消
            }
        };
        activityVC.completionWithItemsHandler = completionHandler;
    }
    @end
    

    相关文章

      网友评论

          本文标题:OC-友盟更新版本6.9.5后的坑(优化封装)

          本文链接:https://www.haomeiwen.com/subject/julhcqtx.html