码迷,mamicode.com
首页 > 移动开发 > 详细

iOS:ShareSDk的分享

时间:2016-04-26 23:55:05      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

使用分享类的SDK其实有很多,例如友盟、ShareSDK等等,参照他们的文档集成起来并不是很难,可能出的一些问题也就是配置文件的问题,这里我个人使用了ShareSDK分享,具体操作可出现的问题如下:

1.首先去ShareSDK官方网站注册开发者账号,获取AppKey用来注册SDK:http://www.mob.com/#/

2.下载SDK:http://www.mob.com/#/downloadDetail/ShareSDK/ios

(1) ShareSDK.framework:核心静态库。(必要)
(2) Support 文件夹。其中包含三个文件夹 :
     (a) Required (需要的类库) :
         ----- MOBFoundation.framework:基础功能框架。(必要)
         ----- ShareSDK.bundle:ShareSDK资源文件。(必要)
         ----- ShareSDKConnector.framework:用于ShareSDK框架与外部框架连接的代理框架插件。(使用第三方SDK时必要。)
 
     (b) Optional (可选类库):
         ----- ShareSDKExtension.framework:对ShareSDK功能的扩展框架插件。
        (主要提供第三方平台登录、 一键分享、截屏分享、摇一摇分享等相关功能。需要使用以上功能时必要。)
         ----- ShareSDKInterfaceAdapter.framework:ShareSDK v2.x对简洁版的接口兼容包。
         ----- ShareSDKUI.bundle:分享菜单栏和分享编辑页面资源包。(如果自定义这些UI可直接移除)
         ----- ShareSDKUI.framework:分享菜单栏和分享编辑页面。(如果自定义这些UI可直接移除)
 
     (c) PlatformSDK 第三方平台SDK。(不需要的平台的SDK可直接移除)

 

3.去新浪微博开放平台、腾讯开放平台、微信开放平台等去注册账号,获取AppKey、AppID、APPScret等

平台
开放平台地址
APPkey 申请流程
新浪微博
腾讯微博
QQ空间[2]
微信[3]
Facebook
facebook审核流程:iOS  Android
Twitter
google+ https://console.developers.google.com/
豆瓣
有道云笔记
印象笔记
Linkedin
Flickr
Pinterest
Tumblr
Dropbox
Instagram
VKontakte
易信好友[4]
明道
人人网
开心网
搜狐随身看 https://open.sohu.com/

 

4.在plist文件配置属性

(1)Htpp协议,如果适配iSO9,需要暂时回退到http协议 info plist加

        <key>NSAppTransportSecurity</key>
        <dict>
             <key>NSAllowsArbitraryLoads</key>
             <true/>
       </dict>

(2)设置白名单在plist文件的URL Types中设置URL Schemes白名单:具体设置可以参照官方文档,很详细
技术分享
(3)涉及到客户端跳转,一定要设置scheme到LSApplicationQueriesSchemes,只有设置了这个,在真机才能实现客户端的跳转,进入QQ、微信等
    QQ:

             技术分享

    Weixin:

             技术分享


好了,一定准备就绪,直接上代码:
//1.创建一个AppDelegate分类
#import "AppDelegate.h"

//shareSDK
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
//新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"
#import "WeiboSDK.h"

@interface AppDelegate (KJShareSDk)

-(void)shareApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

@end
//  AppDelegate+KJShareSDk.m
//  KenJiao
//
//  Created by mac on 16/4/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppDelegate+KJShareSDk.h"

@implementation AppDelegate (KJShareSDk)

-(void)shareApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    /**
     *  设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到http://mob.com/login 登录后台进行应用注册,
     *  在将生成的AppKey传入到此方法中。
     *  方法中的第二个第三个参数为需要连接社交平台SDK时触发,
     *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。
     *  如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。
     */
    [ShareSDK registerApp:@"121f16c6xxxxx"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ)]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"117501xxx"
                                            appSecret:@"ac85119d8a70e9c954bbf62364xxxx"
                                            redirectUri:@"http://www.baidu.com"
                                            authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx365f7b8b89dxxxx"
                                       appSecret:@"652feede37bd1ff5484abb1ed51xxxxf"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"1105359xxx"
                                      appKey:@"ewgzpB2GOf3SIxxx"
                                    authType:SSDKAuthTypeBoth];
                 break;
             default:
                 break;
         }
     }];
}

@end

//2.在AppDelegate类中直接调用即可

#import "AppDelegate.h"
#import "AppDelegate+KJShareSDk.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //注册shareSDK
    [self shareApplication:application didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}
@end

//3、最后一步就是分享内容了

//点击微信
-(void)weiixnBtnClicked:(UIButton *)sender{
    
    //1.隐藏分享视图
    [self cancelBtnClicked:nil];
    

    //2、创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"班级号:%@",self.currentClass.class_id]
                                     images:nil
                                        url:nil
                                      title:@"将该班级号分享到微信"
                                       type:SSDKContentTypeAuto];
    //3、分享(微信平台)
    [ShareSDK share:SSDKPlatformTypeWechat parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        
            switch (state) {
                case SSDKResponseStateSuccess:
                {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                    break;
                }
                case SSDKResponseStateFail:
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                default:
                    break;
            }
    }];
    
}
//点击QQ
-(void)QQBtnClicked:(UIButton *)sender{
    
    //1.隐藏分享视图
    [self cancelBtnClicked:nil];
    
    
     //2、创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"班级号:%@",self.currentClass.class_id]
                                     images:nil
                                        url:nil
                                      title:@"将该班级号分享到QQ"
                                       type:SSDKContentTypeAuto];
 
    
    //3、分享(QQ平台)
    [ShareSDK share:SSDKPlatformTypeQQ parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {

            switch (state) {
                case SSDKResponseStateSuccess:
                {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                    break;
                }
                case SSDKResponseStateFail:
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                default:
                    break;
            }
    }];

    
}
//点击微博
-(void)weiBoBtnClicked:(UIButton *)sender{
    
    //1.隐藏分享视图
    [self cancelBtnClicked:nil];
    
    
    //2、创建分享参数
     NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"班级号:%@",self.currentClass.class_id]
                                     images:nil
                                        url:nil
                                      title:@"将该班级号分享到微博"
                                       type:SSDKContentTypeAuto];
    //3、分享(新浪微博平台)
    [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            switch (state) {
                case SSDKResponseStateSuccess:
                {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                    break;
                }
                case SSDKResponseStateFail:
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                default:
                    break;
            }
    }];
}
//点击取消
-(void)cancelBtnClicked:(UIButton *)sender{
    
    [self.inviteStuView removeFromSuperview];
    [self.inviteStuView.backgroundView removeFromSuperview];
}

 

总结:

总结:像适配iOS9此类问题 使用第三方的直接看别人官网就行了,要不访问提供方的如微信,QQ,支付宝API官方看开发文档
如果是用sharesdk 友盟此类的 去看他们的开发文档都会第一时间给开发者提供解决方案的

sharesdk分享 iOS9适配文档地址
http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85%8Dios-9%E5%BF%85%E8%AF%BB%EF%BC%89/

友盟分享 iOS9适配地址
http://dev.umeng.com/social/ios/ios9

iOS:ShareSDk的分享

标签:

原文地址:http://www.cnblogs.com/XYQ-208910/p/5437194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!