iOS ShareSDK 三方分享/登陸使用

 

原文 http://www.cnblogs.com/CoderAlex/p/4860352.htmlphp

 

一: 快速集成html

1.前言  

做爲如今App裏必不可少的用戶分享須要,社交化分享顯然是咱們開發app裏較爲經常使用的。
最近由於公司App有社交化分享的須要,就特此研究了會,拿出來與你們分享。
想要集成社交會分享,咱們可使用
ShareSDK - 優勢功能豐富,缺點體積較大
百度分享SDK - 缺點功能相對ShareSDK較少,優勢體積較小
 
這是如今較爲經常使用的兩種社交化分享工具。
使用哪種,就看我的的app的須要來決定了。
今天我主要說的是ShareSDK的簡單集成和使用。
 

2.集成前的準備工做  

2.1. 拿本身的Appkey 去下載ShareSDK  

 
下載地址: http://sharesdk.cn/Download
 

2.2. 申請分享工具的Appkey  

 
例如: 新浪微博、騰訊微博、豆瓣應用、人人網、QQ空間
 
去各大社交網站的開發者平臺,進行註冊申請便可。
 
獲取以下:
 
複製代碼
複製代碼
    //新浪微博:

    //App Key:2258477553

    //App Secret:1e2f275afc375109e456f550fb3918e8

    //騰訊微博:

    //App key:2620460989

    //App secret:58c55f572d5ae35e0c355f4c0ee11283
複製代碼
複製代碼
 

3.集成ShareSDK     

3.1.註冊使用ShareSDK   

複製代碼
複製代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    //註冊ShareSDK

    [ShareSDK registerApp:@"1983bf0916db」];

    return YES;

}
複製代碼
複製代碼

3.2.添加要集成的分享平臺  

 

注意: 新浪微博須要提供回調地址才行web

回調地址去新浪開發者平臺獲取數據庫

如圖:微信

 

 

複製代碼
複製代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    //註冊ShareSDK

    [ShareSDK registerApp:@"1983bf0916db」];

   //添加新浪微博應用

    [ShareSDK connectSinaWeiboWithAppKey:@"2620460989"

                               appSecret:@"58c55f572d5ae35e0c355f4c0ee11283"

                             redirectUri:@"http://weibo.cn/ext/share?ru=http%3A%2F%2F16kxs.com%2Fwap%2FBook%2FShow.aspx%3Fid%3D7983%26lmid%3D0%26uid%3D0%26ups%3D0&rt=%E9%83%BD%E5%B8%82%E7%89%A7%E9%AC%BC%E4%BA%BA&st=1301645308&appkey=2620460989」];



     //添加騰訊微博應用

    [ShareSDK connectTencentWeiboWithAppKey:@"801307650"

                                  appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"

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

    //添加豆瓣應用

    [ShareSDK connectDoubanWithAppKey:@"07d08fbfc1210e931771af3f43632bb9"

                            appSecret:@"e32896161e72be91"

                          redirectUri:@"http://dev.kumoway.com/braininference/infos.php"];

  

    //添加人人網應用

    [ShareSDK connectRenRenWithAppKey:@"fc5b8aed373c4c27a05b712acba0f8c3"

                            appSecret:@"f29df781abdd4f49beca5a2194676ca4"];

   

    //添加Facebook應用

    [ShareSDK connectFacebookWithAppKey:@"107704292745179"

                              appSecret:@"38053202e1a5fe26c80c753071f0b573"];

}
複製代碼
複製代碼

3.3.彈出分享View  

a.初始化默認分享內容app

複製代碼
複製代碼
  NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"123"  ofType:@"png"];
    
 
    //構建優秀的SDK
    
    
    //構造分享內容
    id<ISSContent> publishContent = [ShareSDK content:@"iOS社交化分享測試內容。"
                                       defaultContent:@"默認分享內容,沒內容時顯示"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"ShareSDK"
                                                  url:@"http://www.sharesdk.cn"
                                          description:@"這是一條測試信息"
                                            mediaType:SSPublishContentMediaTypeNews];
複製代碼
複製代碼

 

b.彈出分享View異步

 

複製代碼
複製代碼
 [ShareSDK showShareActionSheet:nil
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions: nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                if (state == SSResponseStateSuccess)
                                {
                                    NSLog(@"分享成功");
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(@"分享失敗,錯誤碼:%d,錯誤描述:%@", [error errorCode], [error errorDescription]);
                                }
                            }];
複製代碼
複製代碼

 

 

效果圖:工具

 

 

 

 

 

二: post

 

(1)官方下載ShareSDK IOS 2.9.6,地址:http:// http://www.cnblogs.com/CoderAlex/p/4860352.html/Download測試

(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 <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>

@implementation AppDelegate
@synthesize rootVC;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (self.rootVC==nil) {
self.rootVC = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
}
UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:self.rootVC];
self.window.rootViewController = rootNav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];


[ShareSDK registerApp:@"1a2e7ab5fb6c"];

//添加新浪微博應用 註冊網址 http://open.weibo.com wdl@pmmq.com 此處須要替換成本身應用的
[ShareSDK connectSinaWeiboWithAppKey:@"3201194191"
appSecret:@"0334252914651e8f76bad63337b3b78f"
redirectUri:@"http://appgo.cn"];

//添加騰訊微博應用 註冊網址 http://dev.t.qq.com wdl@pmmq.com 此處須要替換成本身應用的
[ShareSDK connectTencentWeiboWithAppKey:@"801307650"
appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
redirectUri:@"http://www.sharesdk.cn"
wbApiCls:[WeiboApi class]];

//添加QQ空間應用 註冊網址 http://connect.qq.com/intro/login/ wdl@pmmq.com 此處須要替換成本身應用的
[ShareSDK connectQZoneWithAppKey:@"100371282"
appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];

//此參數爲申請的微信AppID wdl@pmmq.com 此處須要替換成本身應用的
[ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885" wechatCls:[WXApi class]];

//添加QQ應用 該參數填入申請的QQ AppId wdl@pmmq.com 此處須要替換成本身應用的
[ShareSDK connectQQWithQZoneAppKey:@"100371282"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];

return YES;
}


- (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:.
}


<span style="color:#ff6600;">#pragma mark - WX回調

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

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate: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 = [[NSBundle mainBundle] pathForResource:@"card" ofType:@"png"];
//構造分享內容
id<ISSContent> publishContent = [ShareSDK content:@"分享內容測試"
defaultContent:@"默認分享內容測試,沒內容時顯示"
image:[ShareSDK imageWithPath:imagePath]
title:@"pmmq"
url:@"http://www.sharesdk.cn"
description:@"這是一條測試信息"
mediaType:SSPublishContentMediaTypeNews];
[ShareSDK showShareActionSheet:nil
shareList:nil
content:publishContent
statusBarTips:YES
authOptions:nil
shareOptions: nil
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (state == SSResponseStateSuccess)
{
NSLog(@"分享成功");
}
else if (state == SSResponseStateFail)
{
NSLog(@"分享失敗");
}
}];
}

(5)登陸、登出、獲取受權信息、關注制定微博

//
// LoginViewController.m
// ShareSDKTest
//
// Created by wangdalei on 14-6-23.
// Copyright (c) 2014年 王大雷. All rights reserved.
//

#import "LoginViewController.h"
#import <ShareSDK/ShareSDK.h>

@interface LoginViewController ()

-(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

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (IBAction)loginWithSina:(id)sender {
[ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
NSLog(@"%d",result);
if (result) {
//成功登陸後,判斷該用戶的ID是否在本身的數據庫中。
//若是有直接登陸,沒有就將該用戶的ID和相關資料在數據庫中建立新用戶。
[self reloadStateWithType:ShareTypeSinaWeibo];
}
}];
}


-(IBAction)loginWithQQ:(id)sender{
[ShareSDK getUserInfoWithType:ShareTypeQQSpace authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
NSLog(@"%d",result);
if (result) {
//成功登陸後,判斷該用戶的ID是否在本身的數據庫中。
//若是有直接登陸,沒有就將該用戶的ID和相關資料在數據庫中建立新用戶。
[self reloadStateWithType:ShareTypeQQSpace];
}
}];
}

-(IBAction)loginoutWithSina:(id)sender{
[ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
[self reloadStateWithType:ShareTypeSinaWeibo];
}

-(IBAction)loginoutWithQQ:(id)sender{
[ShareSDK cancelAuthWithType:ShareTypeQQSpace];
[self reloadStateWithType:ShareTypeQQSpace];
}

-(void)reloadStateWithType:(ShareType)type{
//現實受權信息,包括受權ID、受權有效期等。
//此處能夠在用戶進入應用的時候直接調用,如受權信息不爲空且不過時可幫用戶自動實現登陸。
id<ISSPlatformCredential> credential = [ShareSDK getCredentialWithType:type];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示")
message:[NSString stringWithFormat:
@"uid = %@\ntoken = %@\nsecret = %@\n expired = %@\nextInfo = %@",
[credential uid],
[credential token],
[credential secret],
[credential expired],
[credential extInfo]]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了")
otherButtonTitles:nil];
[alertView show];
}

//關注用戶
-(IBAction)guanzhuUs:(id)sender{
[ShareSDK followUserWithType:ShareTypeSinaWeibo //平臺類型
field:@"ShareSDK" //關注用戶的名稱或ID
fieldType:SSUserFieldTypeName //字段類型,用於指定第二個參數是名稱仍是ID
authOptions:nil //受權選項
viewDelegate:nil //受權視圖委託
result:^(SSResponseState state, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
if (state == SSResponseStateSuccess) {
NSLog(@"關注成功");
} else if (state == SSResponseStateFail) {
NSLog(@"%@", [NSString stringWithFormat:@"關注失敗:%@", error.errorDescription]);
}
}];
}


@end
複製代碼
複製代碼

 

(5)你可能會看到一些應用須要第三方登陸的,一種是彈出webView加載的新浪微博或者qq的網頁受權,還有一種是跳轉到本地的已經安裝的新浪微博應用或者qq應用進行受權。第二種受權方式較SSO受權,體驗會比較好一些,由於不須要用戶輸入新浪微博或QQ的用戶名與密碼。

 

第二種受權方式須要在plist中配置Scheme。SSO默認是打開的不須要配置。在AppDelegate中實現回調。

 

(6)測試DEMO截圖:

相關文章
相關標籤/搜索