免费开源的iOS开发学习平台

友盟 U-Share SDK:3-社会化分享

此篇文章我们通过一个实例简单介绍U-Share SDK中社会化分享的实现。

1、准备

  • 该实例我们自定义一个控制器ShareViewController。

  • 在AppDelegate.m中,设置ShareViewController为根控制器

社会化分享与第三方登录的初始化U-Share及第三方平台的方法是相同的,可参考《友盟 U-Share SDK:2-第三方登录》,此处不再讲解和展示代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ShareViewController *shareVC = [[ShareViewController alloc]init];
    self.window.rootViewController = shareVC;
    [self.window makeKeyAndVisible];
}
  • 重新创建一个自定义控制器是希望大家注意分享面板可能无法弹出的原因:

2、实现分享

  • 在ShareViewController.m中导入头文件。
#import <UMSocialCore/UMSocialCore.h>
#import <UShareUI/UShareUI.h>
  • 搭建界面,在xib中添加按钮控件、图像及文本控件,属性和点击方法与控制器建立关联。

  • 设置用户自定义的平台,即先设定要在哪些平台进行分享。
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置用户自定义的平台
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ)]];
}
  • 实现点击方法调用分享面板

点击“分享文本”按钮。

- (IBAction)shareText:(id)sender {
    WS(weakSelf);
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [weakSelf shareTextToPlatformType:platformType];
    }];
}

点击"分享网页"按钮。

- (IBAction)shareWebPage:(id)sender {
    WS(weakSelf);
    //
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [weakSelf shareWebPageToPlatformType:platformType];
    }];
}
  • 实现分享方法

分享文本

- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType {
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //设置文本
    messageObject.text = self.textView.text;    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}

分享网页

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType {
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];   
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"九九学院" descr:@"一位iOS布道者的博客" thumImage:[UIImage imageNamed:@"99logo"]];
    //设置网页地址
    shareObject.webpageUrl = self.urlTextView.text;
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}

运行效果: