[iOS开发]ShareSDK

  可以自动在App中创建分享功能,节省开发时间,提供开发效率,不必在去研究各个网站的Api

   继续挖坑。。。。。T.T  

  看看表,马上凌晨1点。把今晚看的先分享上来一些,周末要考试,下周了继续写写在App中如何直接关注他人。

下载网站:http://sharesdk.cn/

注册后,创建一个Appkey

 

 

在Xcode中初始化,4点注意

1.

  导入ShareSDK文件夹 

2.

  导入框架:

    SystemConfiguration.framework

    QuartzCore.framework

    MessageUI.framework

    CoreTelephony.framework

    libicucore.dylib

    libstdc++.dylib

    libz.1.2.5.dylib

    libsqlite3.dylib

 

3.

  在AppDelegate中导入头文件#import <ShareSDK/ShareSDK.h>

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

  中写[ShareSDKregisterApp:@"这里填写的是在ShareSDK网址上申请下的APP KEY"];

  可以从Demo中复制-(void)initializePlat方法,

  同时写入这两个方法

1 //授权返回时的数据处理,避免授权失败或者分享失败
2 -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
3     return [ShareSDK handleOpenURL:url wxDelegate:nil];
4 }
5 
6 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
7     return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];
8 }

4.

  注意修改-(void)initializePlat方法中的参数

1     [ShareSDK connectSinaWeiboWithAppKey:@"2334996814"//这里都是从open.weibo.com中申请下来的接口
2                                appSecret:@"a8cac4ac49f2278a9e2e8ff77c45c966"
3                              redirectUri:@"http://www.cnblogs.com"];

实例代码:

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 #import "MainViewController.h"
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 
5 @property (strong, nonatomic) UIWindow *window;
6 @end

AppDelegate.m

  1 #import "AppDelegate.h"
  2 #import <ShareSDK/ShareSDK.h>
  3 
  4 #import "WeiboApi.h"
  5 #import "WXApi.h"
  6 #import "YXApi.h"
  7 
  8 #import <TencentOpenAPI/TencentOAuth.h>
  9 #import <TencentOpenAPI/QQApiInterface.h>
 10 
 11 @implementation AppDelegate
 12 
 13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 14 {
 15     [ShareSDK registerApp:@"1120db661270"];
 16     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 17     // Override point for customization after application launch.
 18     self.window.backgroundColor = [UIColor whiteColor];
 19     MainViewController *main_vc = [[MainViewController alloc] init];
 20     [self initializePlat];
 21     self.window.rootViewController = main_vc;
 22 
 23     [self.window makeKeyAndVisible];
 24     return YES;
 25 }
 26 
 27 //授权返回时的数据处理,避免授权失败或者分享失败
 28 -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
 29     return [ShareSDK handleOpenURL:url wxDelegate:nil];
 30 }
 31 
 32 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
 33     return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];
 34 }
 35 
 36 - (void)initializePlat//根据自己需要选择
 37 {
 38     /**
 39      连接新浪微博开放平台应用以使用相关功能,此应用需要引用SinaWeiboConnection.framework
 40      http://open.weibo.com上注册新浪微博开放平台应用,并将相关信息填写到以下字段
 41      **/
 42     [ShareSDK connectSinaWeiboWithAppKey:@"XXXXXXXXX"
 43                                appSecret:@"XXXXXXXXXXXX"
 44                             //XXXXX部分都是需要到open.weibo.com中为APP申请AppKey
 45                              redirectUri:@"http://www.cnblogs.com"];
 46     //网站是在高级信息中手动设置
 47    /**
 48      连接腾讯微博开放平台应用以使用相关功能,此应用需要引用TencentWeiboConnection.framework
 49      http://dev.t.qq.com上注册腾讯微博开放平台应用,并将相关信息填写到以下字段
 50      
 51      如果需要实现SSO,需要导入libWeiboSDK.a,并引入WBApi.h,将WBApi类型传入接口
 52      **/
 53     [ShareSDK connectTencentWeiboWithAppKey:@"801307650"
 54                                   appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
 55                                 redirectUri:@"http://www.sharesdk.cn"
 56                                    wbApiCls:[WeiboApi class]];
 57     
 58     //连接短信分享
 59     [ShareSDK connectSMS];
 60     
 61     /**
 62      连接QQ空间应用以使用相关功能,此应用需要引用QZoneConnection.framework
 63      http://connect.qq.com/intro/login/上申请加入QQ登录,并将相关信息填写到以下字段
 64      
 65      如果需要实现SSO,需要导入TencentOpenAPI.framework,并引入QQApiInterface.h和TencentOAuth.h,将QQApiInterface和TencentOAuth的类型传入接口
 66      **/
 67     [ShareSDK connectQZoneWithAppKey:@"100371282"
 68                            appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
 69                    qqApiInterfaceCls:[QQApiInterface class]
 70                      tencentOAuthCls:[TencentOAuth class]];
 71     
 72     /**
 73      连接微信应用以使用相关功能,此应用需要引用WeChatConnection.framework和微信官方SDK
 74      http://open.weixin.qq.com上注册应用,并将相关信息填写以下字段
 75      **/
 76     [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885" wechatCls:[WXApi class]];
 77     
 78     /**
 79      连接QQ应用以使用相关功能,此应用需要引用QQConnection.framework和QQApi.framework库
 80      http://mobile.qq.com/api/上注册应用,并将相关信息填写到以下字段
 81      **/
 82     //旧版中申请的AppId(如:QQxxxxxx类型),可以通过下面方法进行初始化
 83     //    [ShareSDK connectQQWithAppId:@"QQ075BCD15" qqApiCls:[QQApi class]];
 84     
 85     [ShareSDK connectQQWithQZoneAppKey:@"100371282"
 86                      qqApiInterfaceCls:[QQApiInterface class]
 87                        tencentOAuthCls:[TencentOAuth class]];
 88     
 89     /**
 90      连接Facebook应用以使用相关功能,此应用需要引用FacebookConnection.framework
 91      https://developers.facebook.com上注册应用,并将相关信息填写到以下字段
 92      **/
 93     [ShareSDK connectFacebookWithAppKey:@"107704292745179"
 94                               appSecret:@"38053202e1a5fe26c80c753071f0b573"];
 95     
 96     /**
 97      连接Twitter应用以使用相关功能,此应用需要引用TwitterConnection.framework
 98      https://dev.twitter.com上注册应用,并将相关信息填写到以下字段
 99      **/
100     [ShareSDK connectTwitterWithConsumerKey:@"mnTGqtXk0TYMXYTN7qUxg"
101                              consumerSecret:@"ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc"
102                                 redirectUri:@"http://www.sharesdk.cn"];
103     
104     /**
105      连接Google+应用以使用相关功能,此应用需要引用GooglePlusConnection.framework、GooglePlus.framework和GoogleOpenSource.framework库
106      https://code.google.com/apis/console上注册应用,并将相关信息填写到以下字段
107      **/
108 //    [ShareSDK connectGooglePlusWithClientId:@"232554794995.apps.googleusercontent.com"
109 //                               clientSecret:@"PEdFgtrMw97aCvf0joQj7EMk"
110 //                                redirectUri:@"http://localhost"
111 //                                  signInCls:[GPPSignIn class]
112 //                                   shareCls:[GPPShare class]];
113 //    
114 //    /**
115 //     连接人人网应用以使用相关功能,此应用需要引用RenRenConnection.framework
116 //     http://dev.renren.com上注册人人网开放平台应用,并将相关信息填写到以下字段
117 //     **/
118 //    [ShareSDK connectRenRenWithAppId:@"226427"
119 //                              appKey:@"fc5b8aed373c4c27a05b712acba0f8c3"
120 //                           appSecret:@"f29df781abdd4f49beca5a2194676ca4"
121 //                   renrenClientClass:[RennClient class]];
122     
123     /**
124      连接开心网应用以使用相关功能,此应用需要引用KaiXinConnection.framework
125      http://open.kaixin001.com上注册开心网开放平台应用,并将相关信息填写到以下字段
126      **/
127     [ShareSDK connectKaiXinWithAppKey:@"358443394194887cee81ff5890870c7c"
128                             appSecret:@"da32179d859c016169f66d90b6db2a23"
129                           redirectUri:@"http://www.sharesdk.cn/"];
130     
131     /**
132      连接易信应用以使用相关功能,此应用需要引用YiXinConnection.framework
133      http://open.yixin.im/上注册易信开放平台应用,并将相关信息填写到以下字段
134      **/
135     [ShareSDK connectYiXinWithAppId:@"yx0d9a9f9088ea44d78680f3274da1765f"
136                            yixinCls:[YXApi class]];
137     
138     //连接邮件
139     [ShareSDK connectMail];
140     
141     //连接打印
142     [ShareSDK connectAirPrint];
143     
144     //连接拷贝
145     [ShareSDK connectCopy];
146     
147     /**
148      连接搜狐微博应用以使用相关功能,此应用需要引用SohuWeiboConnection.framework
149      http://open.t.sohu.com上注册搜狐微博开放平台应用,并将相关信息填写到以下字段
150      **/
151     [ShareSDK connectSohuWeiboWithConsumerKey:@"SAfmTG1blxZY3HztESWx"
152                                consumerSecret:@"yfTZf)!rVwh*3dqQuVJVsUL37!F)!yS9S!Orcsij"
153                                   redirectUri:@"http://www.sharesdk.cn"];
154     
155     /**
156      连接网易微博应用以使用相关功能,此应用需要引用T163WeiboConnection.framework
157      http://open.t.163.com上注册网易微博开放平台应用,并将相关信息填写到以下字段
158      **/
159     [ShareSDK connect163WeiboWithAppKey:@"T5EI7BXe13vfyDuy"
160                               appSecret:@"gZxwyNOvjFYpxwwlnuizHRRtBRZ2lV1j"
161                             redirectUri:@"http://www.shareSDK.cn"];
162     
163     
164     /**
165      连接豆瓣应用以使用相关功能,此应用需要引用DouBanConnection.framework
166      http://developers.douban.com上注册豆瓣社区应用,并将相关信息填写到以下字段
167      **/
168     [ShareSDK connectDoubanWithAppKey:@"02e2cbe5ca06de5908a863b15e149b0b"
169                             appSecret:@"9f1e7b4f71304f2f"
170                           redirectUri:@"http://www.sharesdk.cn"];
171     
172     /**
173      连接印象笔记应用以使用相关功能,此应用需要引用EverNoteConnection.framework
174      http://dev.yinxiang.com上注册应用,并将相关信息填写到以下字段
175      **/
176     [ShareSDK connectEvernoteWithType:SSEverNoteTypeSandbox
177                           consumerKey:@"sharesdk-7807"
178                        consumerSecret:@"d05bf86993836004"];
179     
180     /**
181      连接LinkedIn应用以使用相关功能,此应用需要引用LinkedInConnection.framework库
182      https://www.linkedin.com/secure/developer上注册应用,并将相关信息填写到以下字段
183      **/
184     [ShareSDK connectLinkedInWithApiKey:@"ejo5ibkye3vo"
185                               secretKey:@"cC7B2jpxITqPLZ5M"
186                             redirectUri:@"http://sharesdk.cn"];
187     
188     /**
189      连接Pinterest应用以使用相关功能,此应用需要引用Pinterest.framework库
190      http://developers.pinterest.com/上注册应用,并将相关信息填写到以下字段
191      **/
192 //    [ShareSDK connectPinterestWithClientId:@"1432928"
193 //                              pinterestCls:[Pinterest class]];
194     
195     /**
196      连接Pocket应用以使用相关功能,此应用需要引用PocketConnection.framework
197      http://getpocket.com/developer/上注册应用,并将相关信息填写到以下字段
198      **/
199     [ShareSDK connectPocketWithConsumerKey:@"11496-de7c8c5eb25b2c9fcdc2b627"
200                                redirectUri:@"pocketapp1234"];
201     
202     /**
203      连接Instapaper应用以使用相关功能,此应用需要引用InstapaperConnection.framework
204      http://www.instapaper.com/main/request_oauth_consumer_token上注册Instapaper应用,并将相关信息填写到以下字段
205      **/
206     [ShareSDK connectInstapaperWithAppKey:@"4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA"
207                                 appSecret:@"GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe"];
208     /**
209      连接有道云笔记应用以使用相关功能,此应用需要引用YouDaoNoteConnection.framework
210      http://note.youdao.com/open/developguide.html#app上注册应用,并将相关信息填写到以下字段
211      **/
212     [ShareSDK connectYouDaoNoteWithConsumerKey:@"dcde25dca105bcc36884ed4534dab940"
213                                 consumerSecret:@"d98217b4020e7f1874263795f44838fe"
214                                    redirectUri:@"http://www.sharesdk.cn/"];
215     
216     /**
217      连接搜狐随身看应用以使用相关功能,此应用需要引用SohuConnection.framework
218      https://open.sohu.com上注册应用,并将相关信息填写到以下字段
219      **/
220     [ShareSDK connectSohuKanWithAppKey:@"e16680a815134504b746c86e08a19db0"
221                              appSecret:@"b8eec53707c3976efc91614dd16ef81c"
222                            redirectUri:@"http://sharesdk.cn"];
223     
224     
225     /**
226      链接Flickr,此平台需要引用FlickrConnection.framework框架。
227      http://www.flickr.com/services/apps/create/上注册应用,并将相关信息填写以下字段。
228      **/
229     [ShareSDK connectFlickrWithApiKey:@"33d833ee6b6fca49943363282dd313dd"
230                             apiSecret:@"3a2c5b42a8fbb8bb"];
231     
232     /**
233      链接Tumblr,此平台需要引用TumblrConnection.framework框架
234      http://www.tumblr.com/oauth/apps上注册应用,并将相关信息填写以下字段。
235      **/
236     [ShareSDK connectTumblrWithConsumerKey:@"2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM"
237                             consumerSecret:@"3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo"
238                                callbackUrl:@"http://sharesdk.cn"];
239     
240     /**
241      连接Dropbox应用以使用相关功能,此应用需要引用DropboxConnection.framework库
242      https://www.dropbox.com/developers/apps上注册应用,并将相关信息填写以下字段。
243      **/
244     [ShareSDK connectDropboxWithAppKey:@"7janx53ilz11gbs"
245                              appSecret:@"c1hpx5fz6tzkm32"];
246     
247     /**
248      连接Instagram应用以使用相关功能,此应用需要引用InstagramConnection.framework库
249      http://instagram.com/developer/clients/register/上注册应用,并将相关信息填写以下字段
250      **/
251     [ShareSDK connectInstagramWithClientId:@"ff68e3216b4f4f989121aa1c2962d058"
252                               clientSecret:@"1b2e82f110264869b3505c3fe34e31a1"
253                                redirectUri:@"http://sharesdk.cn"];
254     
255     /**
256      连接VKontakte应用以使用相关功能,此应用需要引用VKontakteConnection.framework库
257      http://vk.com/editapp?act=create上注册应用,并将相关信息填写以下字段
258      **/
259     [ShareSDK connectVKontakteWithAppKey:@"3921561"
260                                secretKey:@"6Qf883ukLDyz4OBepYF1"];
261 }
262                                         

ViewController.m

 1 #import "MainViewController.h"
 2 #import <ShareSDK/ShareSDK.h>
 3 @interface MainViewController ()
 4 
 5 @end
 6 
 7 @implementation MainViewController
 8 
 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
10 {
11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
12     if (self) {
13         // Custom initialization
14     }
15     return self;
16 }
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     // Do any additional setup after loading the view from its nib.
22     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
23     btn.frame = CGRectMake(160, 300, 40, 40);
24     [btn setTitle:@"分享" forState:UIControlStateNormal];
25     [btn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
26     [self.view addSubview:btn];
27     
28     
29   }
30 
31 -(void)share:(id)sender{
32 /**
33  *    @brief    创建分享内容对象,根据以下每个字段适用平台说明来填充参数值
34  *
35  *    @param     content     分享内容(新浪、腾讯、网易、搜狐、豆瓣、人人、开心、有道云笔记、facebook、twitter、邮件、打印、短信、微信、QQ、拷贝)
36  *    @param     defaultContent     默认分享内容(新浪、腾讯、网易、搜狐、豆瓣、人人、开心、有道云笔记、facebook、twitter、邮件、打印、短信、微信、QQ、拷贝)
37  *    @param     image     分享图片(新浪、腾讯、网易、搜狐、豆瓣、人人、开心、facebook、twitter、邮件、打印、微信、QQ、拷贝)
38  *    @param     title     标题(QQ空间、人人、微信、QQ)
39  *    @param     url     链接(QQ空间、人人、instapaper、微信、QQ)
40  *    @param     description     主体内容(人人)
41  *    @param     mediaType     分享类型(QQ、微信)
42  *
43  *    @return    分享内容对象
44  */
45 
46     id <ISSContent> content = [ShareSDK content:@"hello world"
47                                  defaultContent:nil
48                                           image:[ShareSDK imageWithPath:[[NSBundle mainBundle] pathForResource:@"明日香" ofType:@"jpg"]]
49                                           title:@"hello"
50                                             url:@"http://www.cnblogs.com/"
51                                     description:@"description"
52                                       mediaType:SSPublishContentMediaTypeText];
53     
54 
55 /**
56  *    @brief    显示分享菜单
57  *
58  *    @param     container     用于显示分享界面的容器,如果只显示在iPhone客户端可以传入nil。如果需要在iPad上显示需要指定容器。
59  *    @param     shareList     平台类型列表
60  *    @param     content     分享内容
61  *  @param  statusBarTips   状态栏提示标识:YES:显示; NO:隐藏
62  *  @param  authOptions 授权选项,用于指定接口在需要授权时的一些属性(如:是否自动授权,授权视图样式等),默认可传入nil
63  *  @param  shareOptions    分享选项,用于定义分享视图部分属性(如:标题、一键分享列表、功能按钮等),默认可传入nil
64  *  @param  result  分享返回事件处理
65  */
66 
67     //弹出菜单
68     [ShareSDK showShareActionSheet:nil
69                          shareList:nil
70                            content:content
71                      statusBarTips:YES
72                        authOptions:nil
73                       shareOptions:nil
74                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
75                                 if (state == SSPublishContentStateSuccess) {
76                                     NSLog(@"success");
77                                 }else if (state == SSPublishContentStateFail){
78                                     NSLog(@"fail");
79                                 }
80                             }];
81 }
82 
83 
84 - (void)didReceiveMemoryWarning
85 {
86     [super didReceiveMemoryWarning];
87     // Dispose of any resources that can be recreated.
88 }
89 
90 @end

运行结果:

 

原文地址:https://www.cnblogs.com/mo-shou/p/3514663.html