你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS項目集成ShareSDK實現第三方登錄、分享、關注等功能。

IOS項目集成ShareSDK實現第三方登錄、分享、關注等功能。

編輯:IOS開發綜合

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

\

(2)根據實際情況,引入相關的庫,參考官方文檔。

\

(3)在項目的AppDelegate中一般情況下有三個操作,第一是注冊ShareSDK,第二是注冊各個平台的賬號,第三是關於微信等應用的回調處理。

 

  1. //
  2. // AppDelegate.m
  3. // ShareSDKTest
  4. //
  5. // Created by wangdalei on 14-6-23.
  6. // Copyright (c) 2014年 王大雷. All rights reserved.
  7. //
  8.  
  9. #import AppDelegate.h
  10. #import RootViewController.h
  11. #import
  12. #import WeiboApi.h
  13. #import
  14. #import
  15. #import WXApi.h
  16. #import
  17. #import
  18.  
  19. @implementation AppDelegate
  20. @synthesize rootVC;
  21.  
  22. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  23. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  24. if (self.rootVC==nil) {
  25. self.rootVC = [[RootViewController alloc]initWithNibName:@RootViewController bundle:nil];
  26. }
  27. UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:self.rootVC];
  28. self.window.rootViewController = rootNav;
  29. self.window.backgroundColor = [UIColor whiteColor];
  30. [self.window makeKeyAndVisible];
  31.  
  32.  
  33. [ShareSDK registerApp:@1a2e7ab5fb6c];
  34.  
  35. //添加新浪微博應用 注冊網址 http://open.weibo.com [email protected] 此處需要替換成自己應用的
  36. [ShareSDK connectSinaWeiboWithAppKey:@3201194191
  37. appSecret:@0334252914651e8f76bad63337b3b78f
  38. redirectUri:@http://appgo.cn];
  39.  
  40. //添加騰訊微博應用 注冊網址 http://dev.t.qq.com [email protected] 此處需要替換成自己應用的
  41. [ShareSDK connectTencentWeiboWithAppKey:@801307650
  42. appSecret:@ae36f4ee3946e1cbb98d6965b0b2ff5c
  43. redirectUri:@http://www.sharesdk.cn
  44. wbApiCls:[WeiboApi class]];
  45.  
  46. //添加QQ空間應用 注冊網址 http://connect.qq.com/intro/login/ [email protected] 此處需要替換成自己應用的
  47. [ShareSDK connectQZoneWithAppKey:@100371282
  48. appSecret:@aed9b0303e3ed1e27bae87c33761161d
  49. qqApiInterfaceCls:[QQApiInterface class]
  50. tencentOAuthCls:[TencentOAuth class]];
  51.  
  52. //此參數為申請的微信AppID [email protected] 此處需要替換成自己應用的
  53. [ShareSDK connectWeChatWithAppId:@wx4868b35061f87885 wechatCls:[WXApi class]];
  54.  
  55. //添加QQ應用 該參數填入申請的QQ AppId [email protected] 此處需要替換成自己應用的
  56. [ShareSDK connectQQWithQZoneAppKey:@100371282
  57. qqApiInterfaceCls:[QQApiInterface class]
  58. tencentOAuthCls:[TencentOAuth class]];
  59.  
  60. return YES;
  61. }
  62.  
  63.  
  64. - (void)applicationWillResignActive:(UIApplication *)application {
  65. // 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.
  66. // 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.
  67. }
  68.  
  69. - (void)applicationDidEnterBackground:(UIApplication *)application {
  70. // 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.
  71. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  72. }
  73.  
  74. - (void)applicationWillEnterForeground:(UIApplication *)application {
  75. // 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.
  76. }
  77.  
  78. - (void)applicationDidBecomeActive:(UIApplication *)application {
  79. // 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.
  80. }
  81.  
  82. - (void)applicationWillTerminate:(UIApplication *)application {
  83. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  84. }
  85.  
  86.  
  87. #pragma mark - WX回調
  88.  
  89. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  90. return [ShareSDK handleOpenURL:url wxDelegate:self];
  91. }
  92.  
  93. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  94. return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];
  95. }
  96.  
  97. #pragma mark - WXApiDelegate
  98.  
  99. /*! @brief 收到一個來自微信的請求,第三方應用程序處理完後調用sendResp向微信發送結果
  100. *
  101. * 收到一個來自微信的請求,異步處理完成後必須調用sendResp發送處理結果給微信。
  102. * 可能收到的請求有GetMessageFromWXReq、ShowMessageFromWXReq等。
  103. * @param req 具體請求內容,是自動釋放的
  104. */
  105. -(void) onReq:(BaseReq*)req{
  106.  
  107. }
  108.  
  109. /*! @brief 發送一個sendReq後,收到微信的回應
  110. *
  111. * 收到一個來自微信的處理結果。調用一次sendReq後會收到onResp。
  112. * 可能收到的處理結果有SendMessageToWXResp、SendAuthResp等。
  113. * @param resp具體的回應內容,是自動釋放的
  114. */
  115. -(void) onResp:(BaseResp*)resp{
  116.  
  117. }
  118.  
  119. @end

     

     

    (4)信息分享。

     

    1. -(IBAction)share:(id)sender{
    2. NSString *imagePath = [[NSBundle mainBundle] pathForResource:@card ofType:@png];
    3. //構造分享內容
    4. id publishContent = [ShareSDK content:@分享內容測試
    5. defaultContent:@默認分享內容測試,沒內容時顯示
    6. image:[ShareSDK imageWithPath:imagePath]
    7. title:@pmmq
    8. url:@http://www.sharesdk.cn
    9. description:@這是一條測試信息
    10. mediaType:SSPublishContentMediaTypeNews];
    11. [ShareSDK showShareActionSheet:nil
    12. shareList:nil
    13. content:publishContent
    14. statusBarTips:YES
    15. authOptions:nil
    16. shareOptions: nil
    17. result:^(ShareType type, SSResponseState state, id statusInfo, id error, BOOL end) {
    18. if (state == SSResponseStateSuccess)
    19. {
    20. NSLog(@分享成功);
    21. }
    22. else if (state == SSResponseStateFail)
    23. {
    24. NSLog(@分享失敗);
    25. }
    26. }];
    27. }
      (5)登錄、登出、獲取授權信息、關注制定微博

       

       

      1. //
      2. // LoginViewController.m
      3. // ShareSDKTest
      4. //
      5. // Created by wangdalei on 14-6-23.
      6. // Copyright (c) 2014年 王大雷. All rights reserved.
      7. //
      8.  
      9. #import LoginViewController.h
      10. #import
      11.  
      12. @interface LoginViewController ()
      13.  
      14. -(IBAction)loginWithSina:(id)sender;
      15.  
      16. -(IBAction)loginWithQQ:(id)sender;
      17.  
      18. -(IBAction)loginoutWithSina:(id)sender;
      19.  
      20. -(IBAction)loginoutWithQQ:(id)sender;
      21.  
      22. -(IBAction)guanzhuUs:(id)sender;
      23.  
      24. -(void)reloadStateWithType:(ShareType)type;
      25.  
      26. @end
      27.  
      28. @implementation LoginViewController
      29.  
      30. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
      31. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      32. if (self) {
      33. }
      34. return self;
      35. }
      36.  
      37. - (void)viewDidLoad {
      38. [super viewDidLoad];
      39. }
      40.  
      41. - (void)didReceiveMemoryWarning {
      42. [super didReceiveMemoryWarning];
      43. }
      44.  
      45. - (IBAction)loginWithSina:(id)sender {
      46. [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id userInfo, id error) {
      47. NSLog(@%d,result);
      48. if (result) {
      49. //成功登錄後,判斷該用戶的ID是否在自己的數據庫中。
      50. //如果有直接登錄,沒有就將該用戶的ID和相關資料在數據庫中創建新用戶。
      51. [self reloadStateWithType:ShareTypeSinaWeibo];
      52. }
      53. }];
      54. }
      55.  
      56.  
      57. -(IBAction)loginWithQQ:(id)sender{
      58. [ShareSDK getUserInfoWithType:ShareTypeQQSpace authOptions:nil result:^(BOOL result, id userInfo, id error) {
      59. NSLog(@%d,result);
      60. if (result) {
      61. //成功登錄後,判斷該用戶的ID是否在自己的數據庫中。
      62. //如果有直接登錄,沒有就將該用戶的ID和相關資料在數據庫中創建新用戶。
      63. [self reloadStateWithType:ShareTypeQQSpace];
      64. }
      65. }];
      66. }
      67.  
      68. -(IBAction)loginoutWithSina:(id)sender{
      69. [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
      70. [self reloadStateWithType:ShareTypeSinaWeibo];
      71. }
      72.  
      73. -(IBAction)loginoutWithQQ:(id)sender{
      74. [ShareSDK cancelAuthWithType:ShareTypeQQSpace];
      75. [self reloadStateWithType:ShareTypeQQSpace];
      76. }
      77.  
      78. -(void)reloadStateWithType:(ShareType)type{
      79. //現實授權信息,包括授權ID、授權有效期等。
      80. //此處可以在用戶進入應用的時候直接調用,如授權信息不為空且不過期可幫用戶自動實現登錄。
      81. id credential = [ShareSDK getCredentialWithType:type];
      82. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@TEXT_TIPS, @提示)
      83. message:[NSString stringWithFormat:
      84. @uid = %@ token = %@ secret = %@ expired = %@ extInfo = %@,
      85. [credential uid],
      86. [credential token],
      87. [credential secret],
      88. [credential expired],
      89. [credential extInfo]]
      90. delegate:nil
      91. cancelButtonTitle:NSLocalizedString(@TEXT_KNOW, @知道了)
      92. otherButtonTitles:nil];
      93. [alertView show];
      94. }
      95.  
      96. //關注用戶
      97. -(IBAction)guanzhuUs:(id)sender{
      98. [ShareSDK followUserWithType:ShareTypeSinaWeibo //平台類型
      99. field:@ShareSDK //關注用戶的名稱或ID
      100. fieldType:SSUserFieldTypeName //字段類型,用於指定第二個參數是名稱還是ID
      101. authOptions:nil //授權選項
      102. viewDelegate:nil //授權視圖委托
      103. result:^(SSResponseState state, id userInfo, id error) {
      104. if (state == SSResponseStateSuccess) {
      105. NSLog(@關注成功);
      106. } else if (state == SSResponseStateFail) {
      107. NSLog(@%@, [NSString stringWithFormat:@關注失敗:%@, error.errorDescription]);
      108. }
      109. }];
      110. }
      111.  
      112.  
      113. @end
        (5)你可能會看到一些應用需要第三方登錄的,一種是彈出webView加載的新浪微博或者qq的網頁授權,還有一種是跳轉到本地的已經安裝的新浪微博應用或者qq應用進行授權。第二種授權方式較SSO授權,體驗會比較好一些,因為不需要用戶輸入新浪微博或QQ的用戶名與密碼。

         

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

        \

         

        (6)測試DEMO截圖:

        \

        \

         

         

         

         

         

         

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved