IOS项目集成ShareSDK实现第三方登录、分享、关注等功能

原文章链接:http://blog.csdn.net/daleiwang/article/details/34081231

(1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/

(2)根据实际情况,引入相关的库,参考官方文档

(3)在项目的AppDelegate中一般情况下有三个操作,第一是注册ShareSDK,第二是注册各个平台的账号,第三是关于微信等应用的回调处理。

//

//  AppDelegate.m

//  ShareSDKTest

//

//  Created by wangdalei on 14-6-23.

//  Copyright (c) 2014年 王大雷. All rights reserved.

//

#import "AppDelegate.h"

#import "RootViewController.h"

#import 

#import "WeiboApi.h"

#import 

#import 

#import "WXApi.h"

#import 

#import 

@implementationAppDelegate

@synthesizerootVC;

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

self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

if(self.rootVC==nil) {

self.rootVC= [[RootViewControlleralloc]initWithNibName:@"RootViewController"bundle:nil];

}

UINavigationController*rootNav = [[UINavigationControlleralloc]initWithRootViewController:self.rootVC];

self.window.rootViewController= rootNav;

self.window.backgroundColor= [UIColorwhiteColor];

[self.windowmakeKeyAndVisible];

[ShareSDKregisterApp:@"1a2e7ab5fb6c"];

//添加新浪微博应用 注册网址 http://open.weibo.com  wdl@pmmq.com 此处需要替换成自己应用的

[ShareSDKconnectSinaWeiboWithAppKey:@"3201194191"

appSecret:@"0334252914651e8f76bad63337b3b78f"

redirectUri:@"http://appgo.cn"];

//添加腾讯微博应用 注册网址 http://dev.t.qq.com wdl@pmmq.com 此处需要替换成自己应用的

[ShareSDKconnectTencentWeiboWithAppKey:@"801307650"

appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"

redirectUri:@"http://www.sharesdk.cn"

wbApiCls:[WeiboApiclass]];

//添加QQ空间应用 注册网址  http://connect.qq.com/intro/login/ wdl@pmmq.com 此处需要替换成自己应用的

[ShareSDKconnectQZoneWithAppKey:@"100371282"

appSecret:@"aed9b0303e3ed1e27bae87c33761161d"

qqApiInterfaceCls:[QQApiInterfaceclass]

tencentOAuthCls:[TencentOAuthclass]];

//此参数为申请的微信AppID wdl@pmmq.com 此处需要替换成自己应用的

[ShareSDKconnectWeChatWithAppId:@"wx4868b35061f87885"wechatCls:[WXApiclass]];

//添加QQ应用 该参数填入申请的QQ AppId wdl@pmmq.com 此处需要替换成自己应用的

[ShareSDKconnectQQWithQZoneAppKey:@"100371282"

qqApiInterfaceCls:[QQApiInterfaceclass]

tencentOAuthCls:[TencentOAuthclass]];

returnYES;

}

- (void)applicationWillResignActive:(UIApplication*)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication*)application {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication*)application {

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication*)application {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication*)application {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

#pragma mark - WX回调

- (BOOL)application:(UIApplication*)applicationhandleOpenURL:(NSURL*)url {

return[ShareSDKhandleOpenURL:urlwxDelegate:self];

}

- (BOOL)application:(UIApplication*)applicationopenURL:(NSURL*)urlsourceApplication:(NSString*)sourceApplicationannotation:(id)annotation {

return[ShareSDKhandleOpenURL:urlsourceApplication:sourceApplicationannotation:annotationwxDelegate:self];

}

#pragma mark - WXApiDelegate

/*! @brief 收到一个来自微信的请求,第三方应用程序处理完后调用sendResp向微信发送结果

*

* 收到一个来自微信的请求,异步处理完成后必须调用sendResp发送处理结果给微信。

* 可能收到的请求有GetMessageFromWXReq、ShowMessageFromWXReq等。

* @param req 具体请求内容,是自动释放的

*/

-(void)onReq:(BaseReq*)req{

}

/*! @brief 发送一个sendReq后,收到微信的回应

*

* 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。

* 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。

* @param resp具体的回应内容,是自动释放的

*/

-(void)onResp:(BaseResp*)resp{

}

@end

(4)信息分享。

-(IBAction)share:(id)sender{

NSString*imagePath = [[NSBundlemainBundle]pathForResource:@"card"ofType:@"png"];

//构造分享内容

id publishContent = [ShareSDKcontent:@"分享内容测试"

defaultContent:@"默认分享内容测试,没内容时显示"

image:[ShareSDKimageWithPath:imagePath]

title:@"pmmq"

url:@"http://www.sharesdk.cn"

description:@"这是一条测试信息"

mediaType:SSPublishContentMediaTypeNews];

[ShareSDKshowShareActionSheet:nil

shareList:nil

content:publishContent

statusBarTips:YES

authOptions:nil

shareOptions:nil

result:^(ShareType type, SSResponseState state,id statusInfo,id error,BOOLend) {

if(state == SSResponseStateSuccess)

{

NSLog(@"分享成功");

}

elseif(state == SSResponseStateFail)

{

NSLog(@"分享失败");

}

}];

}

(5)登录、登出、获取授权信息、关注制定微博

//

//  LoginViewController.m

//  ShareSDKTest

//

//  Created by wangdalei on 14-6-23.

//  Copyright (c) 2014年 王大雷. All rights reserved.

//

#import "LoginViewController.h"

#import 

@interfaceLoginViewController ()

-(IBAction)loginWithSina:(id)sender;

-(IBAction)loginWithQQ:(id)sender;

-(IBAction)loginoutWithSina:(id)sender;

-(IBAction)loginoutWithQQ:(id)sender;

-(IBAction)guanzhuUs:(id)sender;

-(void)reloadStateWithType:(ShareType)type;

@end

@implementationLoginViewController

- (id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil {

self= [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if(self) {

}

returnself;

}

- (void)viewDidLoad {

[superviewDidLoad];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

- (IBAction)loginWithSina:(id)sender {

[ShareSDKgetUserInfoWithType:ShareTypeSinaWeiboauthOptions:nilresult:^(BOOLresult,id userInfo,id error) {

NSLog(@"%d",result);

if(result) {

//成功登录后,判断该用户的ID是否在自己的数据库中。

//如果有直接登录,没有就将该用户的ID和相关资料在数据库中创建新用户。

[selfreloadStateWithType:ShareTypeSinaWeibo];

}

}];

}

-(IBAction)loginWithQQ:(id)sender{

[ShareSDKgetUserInfoWithType:ShareTypeQQSpaceauthOptions:nilresult:^(BOOLresult,id userInfo,id error) {

NSLog(@"%d",result);

if(result) {

//成功登录后,判断该用户的ID是否在自己的数据库中。

//如果有直接登录,没有就将该用户的ID和相关资料在数据库中创建新用户。

[selfreloadStateWithType:ShareTypeQQSpace];

}

}];

}

-(IBAction)loginoutWithSina:(id)sender{

[ShareSDKcancelAuthWithType:ShareTypeSinaWeibo];

[selfreloadStateWithType:ShareTypeSinaWeibo];

}

-(IBAction)loginoutWithQQ:(id)sender{

[ShareSDKcancelAuthWithType:ShareTypeQQSpace];

[selfreloadStateWithType:ShareTypeQQSpace];

}

-(void)reloadStateWithType:(ShareType)type{

//现实授权信息,包括授权ID、授权有效期等。

//此处可以在用户进入应用的时候直接调用,如授权信息不为空且不过期可帮用户自动实现登录。

id credential = [ShareSDKgetCredentialWithType:type];

UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:NSLocalizedString(@"TEXT_TIPS",@"提示")

message:[NSStringstringWithFormat:

@"uid = %@\ntoken = %@\nsecret = %@\n expired = %@\nextInfo = %@",

[credentialuid],

[credentialtoken],

[credentialsecret],

[credentialexpired],

[credentialextInfo]]

delegate:nil

cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW",@"知道了")

otherButtonTitles:nil];

[alertViewshow];

}

//关注用户

-(IBAction)guanzhuUs:(id)sender{

[ShareSDKfollowUserWithType:ShareTypeSinaWeibo//平台类型

field:@"ShareSDK"//关注用户的名称或ID

fieldType:SSUserFieldTypeName//字段类型,用于指定第二个参数是名称还是ID

authOptions:nil//授权选项

viewDelegate:nil//授权视图委托

result:^(SSResponseState state,id userInfo,id error) {

if(state == SSResponseStateSuccess) {

NSLog(@"关注成功");

}elseif(state == SSResponseStateFail) {

NSLog(@"%@", [NSStringstringWithFormat:@"关注失败:%@", error.errorDescription]);

}

}];

}

@end

(5)你可能会看到一些应用需要第三方登录的,一种是弹出webView加载的新浪微博或者qq的网页授权,还有一种是跳转到本地的已经安装的新浪微博应用或者qq应用进行授权。第二种授权方式较SSO授权,体验会比较好一些,因为不需要用户输入新浪微博或QQ的用户名与密码。

第二种授权方式需要在plist中配置Scheme。SSO默认是打开的不需要配置。在AppDelegate中实现回调。

(6)测试DEMO截图:

DEMO下载地址:http://download.csdn.net/download/daleiwang/7734321

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容