iOS-应用分享

1.系统自带分享

添加Social.framework框架

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.判断服务是否可用
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        NSLog(@"分享服务不可用");
    }
    // 2.创建分享控制器
    SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    // 设置默认数据
    [composeVc setInitialText:@"今天天很好, 只不过没出门"];
    [composeVc addImage:[UIImage imageNamed:@"psb-2.jpeg"]];
    
    // 3.弹出分享控制器
    [self presentViewController:composeVc animated:YES completion:nil];
    
    // 4.监听分享状态
    composeVc.completionHandler = ^(SLComposeViewControllerResult result)
    {
        if (result == SLComposeViewControllerResultCancelled) {
            NSLog(@"取消发送");
        }else
        {
            NSLog(@"发送成功");
        }
    };
    
}


2.友盟分享

 1>快速分享

   1.导入SDK,如果需要其他插件功能,要导入UMSocial_Sdk_Extra_Frameworks

   2.在Appdelegate添加Appkey

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 设置友盟分享的秘钥
     [UMSocialData setAppKey:@"5458264dfd98c5b5c9007064"];
 };

  3.实现代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //注意:分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
    /*
     SheetView: 需要在哪个视图中展示
     appKey: 当前应用的友盟分享的key
     shareText: 需要分享的文本
     shareImage: 需要分享的图片
     shareToSnsNames: 需要分享到的平台
     */
   
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5458264dfd98c5b5c9007064"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren, UMShareToDouban, UMShareToFacebook, UMShareToFlickr, UMShareToInstagram, nil]
                                       delegate:nil];

}
 2>SSO分享

  以新浪微博为例子:

    1.添加SDK和UMSocial_Sdk_Extra_Frameworks里面额外对应的框架,AppKey

    2.在项目设置-Info-URL Types设置sina.Appkey Appkey是注册申请的

    4.AppDelegate打开微博SSO开关

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 设置友盟分享的密钥
     [UMSocialData setAppKey:@"5458264dfd98c5b5c9007064"];
    
    //打开新浪微博的SSO开关,设置新浪微博回调地址,这里必须要和你在新浪微博后台设置的回调地址一致。若在新浪后台设置我们的回调地址,“http://sns.whalecloud.com/sina2/callback”,这里可以传nil
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    return YES;
}

    5.AppDelegate实现跳转回调

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}

//iOS7以后的方法
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}

  6.实现代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //注意:分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
    /*
     SheetView: 需要在哪个视图中展示
     appKey: 当前应用的友盟分享的key
     shareText: 需要分享的文本
     shareImage: 需要分享的图片
     shareToSnsNames: 需要分享到的平台
     */

    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5458264dfd98c5b5c9007064"
                                      shareText:@"友盟社会化分享让您快速实现分享等社会化功能,www.umeng.com/social"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:@[UMShareToSina]
                                       delegate:self];
}

iOS-应用分享_第1张图片

你可能感兴趣的:(iOS-应用分享)