博客> 友盟分享类
友盟分享类
2018-04-20 02:04 评论:0 阅读:327 易_君
ios oc 分享 友盟

/*.h文件*****/ // // UMPlatfomrSupportType.h // CultureHome // // Created by Apple on 2017/10/10. // Copyright © 2017年 易元江. All rights reserved. //

import

import

@interface UMPlatfomrSupportType : NSObject

//单例方法

  • (instancetype)share;

/**

  • 分享文本
  • @param text 文本内容 */
    • (void)shareTextToPlatformType:(UMSocialPlatformType)platformType :(NSString *)text;

/**

  • 分享图片
  • @param thumbImage 缩略图 UIImage或者NSData类型或者NSString类型(图片url)
  • @param shareImage 图片内容(可以是UIImage类对象,也可以是NSdata类对象,也可以是图片链接imageUrl NSString类对象)图片大小根据各个平台限制而定 */
    • (void)shareImageToPlatformType:(UMSocialPlatformType)platformType thumbImage:(id)thumbImage shareImage:(id)shareImage;

/**

  • 分享网络图片
  • @param htimg 缩略图 UIImage或者NSData类型或者NSString类型(图片url)
  • @param httpImg (图片链接imageUrl NSString类对象)图片大小根据各个平台限制而定 */
    • (void)shareImageURLToPlatformType:(UMSocialPlatformType)platformType httpImage:(id)htimg ShareHttpImage:(id)httpImg;

/**

  • 分享图片和文字
  • text 文本内容
  • thumbImage 缩略图 UIImage或者NSData类型或者NSString类型(图片url)
  • @param shareImage 图片内容(可以是UIImage类对象,也可以是NSdata类对象,也可以是图片链接imageUrl NSString类对象)图片大小根据各个平台限制而定 */
    • (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType text:(NSString *)text thumbImage:(id)thumbImage shareImage:(id)shareImage;

/**

  • 网页分享
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param webpageUrl 网页的url地址 不能为空且长度不能超过10K */
    • (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage webpageUrl:(NSString *)webpageUrl;

/**

  • 音乐分享
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param musicUrl 音乐网页的url地址 长度不能超过10K
  • @param musicDataUrl 音乐数据url地址 长度不能超过10K */
    • (void)shareMusicToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage webpageUrl:(NSString )webpageUrl musicUrl:(NSString )musicUrl musicDataUrl:(NSString *)musicDataUrl;

/**

  • 视频分享
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param videoUrl 视频网页的url 长度不能超过10K */
    • (void)shareVedioToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage videoUrl:(NSString *)videoUrl;

/**

  • gif表情
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param path 表情数据来源路径,如GIF等 微信的话大小不能超过10M */
    • (void)shareEmoticonToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage Path:(NSString *)path;

/**

  • 文件
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param path 文件真实数据内容来源路径 微信的话大小不能超过10M */
    • (void)shareFileToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage Path:(NSString *)path;

/**

  • 分享小程序
  • @param title 标题
  • @param descr 描述
  • @param icon 缩略图(UIImage或者NSData类型,或者image_url)
  • @param webpageUrl 低版本微信网页链接
  • @param userName 描述
  • @param path 小程序页面的路径
  • */

    • (void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType UMSProgTitle:(NSString )title UMSText:(NSString )descr thumImage:(id)icon webpageUrl:(NSString )webpageUrl userName:(NSString )userName path:(NSString *)path;

@end

/*.m文件*****/ // // UMPlatfomrSupportType.m // CultureHome // // Created by Apple on 2017/10/10. // Copyright © 2017年 易元江. All rights reserved. //

import "UMPlatfomrSupportType.h"

@implementation UMPlatfomrSupportType

//全局变量 static UMPlatfomrSupportType *_umShare;

//单例方法

  • (instancetype)share { //只进行一次 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _umShare = [[self alloc] init]; });

    return _umShare; }

//alloc会调用allocWithZone:

  • (instancetype)allocWithZone:(struct _NSZone *)zone { //只进行一次 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _umShare = [super allocWithZone:zone]; });

    return _umShare; }

//copy在底层 会调用copyWithZone:

  • (id)copyWithZone:(struct _NSZone *)zone { return _umShare; }

pragma mark - share type

/**

  • 分享文本
  • @param text 文本内容 */

    • (void)shareTextToPlatformType:(UMSocialPlatformType)platformType :(NSString )text { //创建分享消息对象 UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject]; //设置文本 messageObject.text = text;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

/**

  • 分享图片
  • @param thumbImage 缩略图 UIImage或者NSData类型或者NSString类型(图片url)
  • @param shareImage 图片内容(可以是UIImage类对象,也可以是NSdata类对象,也可以是图片链接imageUrl NSString类对象)图片大小根据各个平台限制而定 */

    • (void)shareImageToPlatformType:(UMSocialPlatformType)platformType thumbImage:(id)thumbImage shareImage:(id)shareImage { //创建分享消息对象 UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    //创建图片内容对象 UMShareImageObject *shareObject = [[UMShareImageObject alloc] init]; //如果有缩略图,则设置缩略图本地 shareObject.thumbImage = thumbImage;

    [shareObject setShareImage:shareImage];

    // 设置Pinterest参数 if (platformType == UMSocialPlatformType_Pinterest) { [self setPinterstInfo:messageObject]; }

    // 设置Kakao参数 if (platformType == UMSocialPlatformType_KakaoTalk) { messageObject.moreInfo = @{@"permission" : @1}; // @1 = KOStoryPermissionPublic }

    //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }]; }

/**

  • 分享网络图片
  • @param htimg 缩略图 UIImage或者NSData类型或者NSString类型(图片url)
  • @param httpImg (图片链接imageUrl NSString类对象)图片大小根据各个平台限制而定 */

    • (void)shareImageURLToPlatformType:(UMSocialPlatformType)platformType httpImage:(id)htimg ShareHttpImage:(id)httpImg { //创建分享消息对象 UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    //创建图片内容对象 UMShareImageObject *shareObject = [[UMShareImageObject alloc] init]; //如果有缩略图,则设置缩略图 shareObject.thumbImage = htimg;

    [shareObject setShareImage:httpImg];

    // 设置Pinterest参数 if (platformType == UMSocialPlatformType_Pinterest) { [self setPinterstInfo:messageObject]; }

    // 设置Kakao参数 if (platformType == UMSocialPlatformType_KakaoTalk) { messageObject.moreInfo = @{@"permission" : @1}; // @1 = KOStoryPermissionPublic }

    //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

/**

  • 分享图片和文字
  • text 文本内容
  • thumbImage 缩略图 UIImage或者NSData类型或者NSString类型(图片url)
  • @param shareImage 图片内容(可以是UIImage类对象,也可以是NSdata类对象,也可以是图片链接imageUrl NSString类对象)图片大小根据各个平台限制而定 */

    • (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType text:(NSString )text thumbImage:(id)thumbImage shareImage:(id)shareImage { //创建分享消息对象 UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject];

    //设置文本 messageObject.text = text;

    //创建图片内容对象 UMShareImageObject *shareObject = [[UMShareImageObject alloc] init]; //如果有缩略图,则设置缩略图 if (platformType == UMSocialPlatformType_Linkedin) { // linkedin仅支持URL图片 shareObject.thumbImage = thumbImage; [shareObject setShareImage:shareImage]; } else { shareObject.thumbImage = [UIImage imageNamed:thumbImage]; shareObject.shareImage = [UIImage imageNamed:shareImage]; }

    //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }]; }

/**

  • 网页分享
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param webpageUrl 网页的url地址 不能为空且长度不能超过10K */

    • (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage webpageUrl:(NSString )webpageUrl { //创建分享消息对象 UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject]; //创建网页内容对象 UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:descr thumImage:thumImage]; //设置网页地址 shareObject.webpageUrl = webpageUrl;

    //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) { NSLog(@"调用分享回调%@",data); [self altershow:error data:data]; }]; }

/**

  • 音乐分享
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param musicUrl 音乐网页的url地址 长度不能超过10K
  • @param musicDataUrl 音乐数据url地址 长度不能超过10K */

    • (void)shareMusicToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage webpageUrl:(NSString )webpageUrl musicUrl:(NSString )musicUrl musicDataUrl:(NSString )musicDataUrl { //创建分享消息对象 UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject];

    //创建音乐内容对象 UMShareMusicObject *shareObject = [UMShareMusicObject shareObjectWithTitle:title descr:descr thumImage:thumImage]; //设置音乐网页播放地址 shareObject.musicUrl = musicUrl; shareObject.musicDataUrl = musicDataUrl; //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

/**

  • 视频分享
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param videoUrl 视频网页的url 长度不能超过10K */

    • (void)shareVedioToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage videoUrl:(NSString )videoUrl { //创建分享消息对象 UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject];

    UMShareVideoObject *shareObject = [UMShareVideoObject shareObjectWithTitle:title descr:descr thumImage:thumImage]; //设置视频网页播放地址 shareObject.videoUrl = videoUrl;

    //分享消息对象设置分享内容对象 messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

/**

  • gif表情
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param path 表情数据来源路径,如GIF等 微信的话大小不能超过10M */

    • (void)shareEmoticonToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage Path:(NSString )path { UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject];

    UMShareEmotionObject *shareObject = [UMShareEmotionObject shareObjectWithTitle:title descr:descr thumImage:thumImage];

    NSString filePath = [[NSBundle mainBundle] pathForResource:path ofType:@"gif"]; NSData emoticonData = [NSData dataWithContentsOfFile:filePath]; shareObject.emotionData = emoticonData;

    messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

/**

  • 文件
  • @param title 标题
  • @param descr 描述
  • @param thumImage 缩略图(UIImage或者NSData类型,或者image_url)
  • @param path 文件真实数据内容来源路径 微信的话大小不能超过10M */

    • (void)shareFileToPlatformType:(UMSocialPlatformType)platformType title:(NSString )title descr:(NSString )descr thumImage:(id)thumImage Path:(NSString )path { UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject];

    UMShareFileObject *shareObject = [UMShareFileObject shareObjectWithTitle:title descr:descr thumImage:thumImage];

    NSString kFileExtension = @"txt"; NSString filePath = [[NSBundle mainBundle] pathForResource:path ofType:kFileExtension]; NSData *fileData = [NSData dataWithContentsOfFile:filePath]; shareObject.fileData = fileData; shareObject.fileExtension = kFileExtension;

    messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

/**

  • 分享小程序
  • @param title 标题
  • @param descr 描述
  • @param icon 缩略图(UIImage或者NSData类型,或者image_url)
  • @param webpageUrl 低版本微信网页链接
  • @param userName 描述
  • @param path 小程序页面的路径
  • */

    • (void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType UMSProgTitle:(NSString )title UMSText:(NSString )descr thumImage:(id)icon webpageUrl:(NSString )webpageUrl userName:(NSString )userName path:(NSString )path { //创建分享消息对象 UMSocialMessageObject messageObject = [UMSocialMessageObject messageObject];

    UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:title descr:descr thumImage:[UIImage imageNamed:icon]]; shareObject.webpageUrl = webpageUrl;//@"http://sdk.umsns.com/download/demo/" shareObject.userName = userName;//@"gh_3ac2059ac66f" shareObject.path = path; //@"pages/page10007/page10007" messageObject.shareObject = shareObject;

    UIViewController *currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentVC completion:^(id data, NSError *error) {

    [self altershow:error data:data];

    }];

}

  • (void)altershow:(NSError *)error data:(id)data { if (error) { 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);

// [[NSNotificationCenter defaultCenter] postNotificationName:@"ShareSucesess" object:nil];

    }else{
        UMSocialLogInfo(@"response data is %@",data);
    }
}

// [self alertWithError:error]; }

  • (void)alertWithError:(NSError )error { NSString result = nil; if (!error) { result = [NSString stringWithFormat:@"分享成功"]; }else{ NSMutableString str = [NSMutableString string]; if (error.userInfo) { for (NSString key in error.userInfo) { [str appendFormat:@"%@ = %@\n", key, error.userInfo[key]]; } } if (error) { result = [NSString stringWithFormat:@"分享失败code: %d\n%@",(int)error.code, str]; } else{ result = [NSString stringWithFormat:@"分享失败"]; } } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享" message:result delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", @"sure") otherButtonTitles:nil]; [alert show]; }

  • (void)setPinterstInfo:(UMSocialMessageObject *)messageObj { messageObj.moreInfo = @{@"source_url": @"http://www.umeng.com", @"app_name": @"U-Share", @"suggested_board_name": @"UShareProduce", @"description": @"U-Share: best social bridge"}; }

@end

收藏
0
sina weixin mail 回到顶部