iOS開發之share第三方登錄以及分享

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

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

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

?

[objc]?view plaincopy
print?在CODE上查看代碼片派生到我的代碼片
  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?<ShareSDK/ShareSDK.h>??
  12. #import?"WeiboApi.h"??
  13. #import?<TencentOpenAPI/QQApiInterface.h>??
  14. #import?<TencentOpenAPI/TencentOAuth.h>??
  15. #import?"WXApi.h"??
  16. #import?<TencentOpenAPI/QQApiInterface.h>??
  17. #import?<TencentOpenAPI/TencentOAuth.h>??
  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. ????<span?style="color:#ff0000;">[ShareSDK?registerApp:@"1a2e7ab5fb6c"];</span>??
  34. ??????
  35. ???<span?style="color:#3366ff;">?//添加新浪微博應用?注冊網址?http://open.weibo.com??wdl@pmmq.com?此處需要替換成自己應用的??
  36. ????[ShareSDK?connectSinaWeiboWithAppKey:@"3201194191"??
  37. ???????????????????????????????appSecret:@"0334252914651e8f76bad63337b3b78f"??
  38. ?????????????????????????????redirectUri:@"http://appgo.cn"];??
  39. ??????
  40. ????//添加騰訊微博應用?注冊網址?http://dev.t.qq.com?wdl@pmmq.com?此處需要替換成自己應用的??
  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/?wdl@pmmq.com?此處需要替換成自己應用的??
  47. ????[ShareSDK?connectQZoneWithAppKey:@"100371282"??
  48. ???????????????????????????appSecret:@"aed9b0303e3ed1e27bae87c33761161d"??
  49. ???????????????????qqApiInterfaceCls:[QQApiInterface?class]??
  50. ?????????????????????tencentOAuthCls:[TencentOAuth?class]];??
  51. ??????
  52. ????//此參數為申請的微信AppID?wdl@pmmq.com?此處需要替換成自己應用的??
  53. ????[ShareSDK?connectWeChatWithAppId:@"wx4868b35061f87885"?wechatCls:[WXApi?class]];??
  54. ??????
  55. ????//添加QQ應用?該參數填入申請的QQ?AppId?wdl@pmmq.com?此處需要替換成自己應用的??
  56. ????[ShareSDK?connectQQWithQZoneAppKey:@"100371282"??
  57. ?????????????????????qqApiInterfaceCls:[QQApiInterface?class]??
  58. ???????????????????????tencentOAuthCls:[TencentOAuth?class]];</span>??
  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. <span?style="color:#ff6600;">#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. </span>??
  119. @end??

?

?

(4)信息分享。

?

[objc]?view plaincopy
print?在CODE上查看代碼片派生到我的代碼片
  1. -(IBAction)share:(id)sender{??
  2. ????NSString?*imagePath?=?[[NSBundle?mainBundle]?pathForResource:@"card"??ofType:@"png"];??
  3. ????//構造分享內容??
  4. ????id<ISSContent>?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<ISSPlatformShareInfo>?statusInfo,?id<ICMErrorInfo>?error,?BOOL?end)?{??
  18. ????????????????????????????????if?(state?==?SSResponseStateSuccess)??
  19. ????????????????????????????????{??
  20. ????????????????????????????????????NSLog(@"分享成功");??
  21. ????????????????????????????????}??
  22. ????????????????????????????????else?if?(state?==?SSResponseStateFail)??
  23. ????????????????????????????????{??
  24. ????????????????????????????????????NSLog(@"分享失敗");??
  25. ????????????????????????????????}??
  26. ????????????????????????????}];??
  27. }??


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

?

[objc]?view plaincopy
print?在CODE上查看代碼片派生到我的代碼片
  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?<ShareSDK/ShareSDK.h>??
  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<ISSPlatformUser>?userInfo,?id<ICMErrorInfo>?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<ISSPlatformUser>?userInfo,?id<ICMErrorInfo>?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<ISSPlatformCredential>?credential?=?[ShareSDK?getCredentialWithType:type];??
  82. ????UIAlertView?*alertView?=?[[UIAlertView?alloc]?initWithTitle:NSLocalizedString(@"TEXT_TIPS",?@"提示")??
  83. ????????????????????????????????????????????????????????message:[NSString?stringWithFormat:??
  84. ?????????????????????????????????????????????????????????????????@"uid?=?%@\ntoken?=?%@\nsecret?=?%@\n?expired?=?%@\nextInfo?=?%@",??
  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<ISSPlatformUser>?userInfo,?id<ICMErrorInfo>?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 ?

?

?

?

?

?

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

轉載于:https://www.cnblogs.com/lys-iOS-study/p/5693924.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256765.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256765.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256765.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux磁盤的劃分

磁盤的組成&#xff1a; 磁道&#xff1a;track 扇區&#xff1a;sector (512字節) 磁頭&#xff1a;head 柱面&#xff1a;cylinder MBR/msdos 分區模式 1--4個主分區&#xff0c;或者0--3個主分區加1個擴展分區&#xff08;n個邏輯分區&#xff09; 最大支持容量為2.2TB的磁…

opencv的pnp()算法接口是相對于3D點,輸出的是相機與3D點之間的R和T

1、情況一&#xff1a; 兩幀圖像 -》 提取特征-》特征匹配-》通過2d-2d計算 F基礎矩陣、E 本質矩陣 、H 單一性矩陣 -》解析出 相機自身的運動R和T -》再通過三角化&#xff0c;將2d點轉為相機的3d點&#xff08;每個空間點在兩個相機坐標系下的投影3D坐標與像素2D坐標&#…

有限元課堂筆記03:鋼架(Frame)

1.平面鋼架(Frame)&#xff1a;是桁架(Truss)和梁(Beam)的合成&#xff0c;兩節點六自由度 2.空間鋼架&#xff1a;兩節點12自由度 相對于平面鋼架來說每一個節點增加了z軸線性變形、繞x軸扭矩&#xff0c;繞y軸扭矩 剛度矩陣

關于系統性能檢測的一些使用

1.安裝sysstat&#xff1a;yum install sysstat---------- iostat -x 1 10 如果 %util 接近 100%&#xff0c;說明產生的I/O請求太多&#xff0c;I/O系統已經滿負荷&#xff0c;該磁盤可能存在瓶頸。 idle小于70% IO壓力就較大了,一般讀取速度有較多的wait. 2.如果想對硬盤…

Python tab 補全

1. 先準備一個tab.py的腳本 shell> cat tab.py 12345678910111213141516171819#!/usr/bin/python# python tab fileimport sys import readline import rlcompleter import atexit import os # tab completionreadline.parse_and_bind(tab: complete) # history filehistfil…

Docker新手入門:基本用法

Docker新手入門&#xff1a;基本用法 1.Docker簡介 1.1 第一本Docker書 工作中不斷碰到Docker&#xff0c;今天終于算是正式開始學習了。在挑選系統學習Docker以及虛擬化技術的書籍時還碰到了不少麻煩&#xff0c;主要就是沒有特別經典的書&#xff01;Docker的《第一版Docker書…

有限元筆記04:二維實體單元

1.二維實體即平面問題 創建單元的步驟&#xff1a; 型函數&#xff08;插值函數&#xff09;>>>應變矩陣>>>剛度矩陣>>>質量矩陣>>>力的分量 1&#xff09;三角形單元 2&#xff09;面坐標 3&#xff09;線性矩形單元 4)高斯積分 6)任意…

oracle中的常用函數

一、運算符算術運算符&#xff1a; - * / 可以在select 語句中使用連接運算符&#xff1a;|| select deptno|| dname from dept; 比較運算符&#xff1a;> > ! < < like between is null in邏輯運算符&#xff1a;not and or 集合運算符&#xff1a; 集合操作不適…

SLAM后端優化之-核函數

1、核函數作用&#xff1a;保證每條邊的誤差不會大的沒邊&#xff0c;掩蓋掉其他的邊 在SLAM后端優化中&#xff0c;BA優化了所有的相機姿態和所有路標點&#xff0c;使用的最小化誤差項作的二范數平方和作為目標函數&#xff1b;當我們的誤差來源特別大的時候&#xff1b;BA優…

線程與內核對象的同步-2

等待定時器內核事件 CreateWaitableTimer( PSECURITY_ATTRIBUTES psa, BOOL fManualReset, PCTSTR pszName); 進程可以獲得它自己的與進程相關的現有等待定時器的句柄。 HANDLE OpenWaitableTimer( DWORD dwDesiredAccess, BOOL bInheritHandle, PCTSTR pszName); 等待定時器對…

【Win10 應用開發】自定義應用標題欄

Win 10 app對窗口標題欄的自定義包括兩個層面&#xff1a;一是只定義標題中各部分的顏色&#xff0c;如標題欄上文本的顏色、三個系統按鈕&#xff08;最大化&#xff0c;最小化&#xff0c;關閉&#xff09;的背景顏色等&#xff1b;另一層是把窗口的可視區域直接擴展到標題欄…

學習筆記(59):Python實戰編程-Graphics

立即學習:https://edu.csdn.net/course/play/19711/343123?utm_sourceblogtoedu 1.graphics&#xff1a;圖形界面組件的繪制&#xff0c;利用的是坐標的定位來對各個組件進行相對地位置布局 2.graphics與thinkter的區別 1&#xff09;窗口的創建上&#xff1a; win graphics…

IIS6配置Asp.net MVC運行環境

Windows server 2003 IIS6 搭建Asp.net MVC運行環境 1、安裝.Net Framework4.0。 下載地址&#xff1a; http://www.microsoft.com/zh-cn/download/details.aspx?id17718 2、安裝WindowsServer2003-KB968930-x86-CHS.exe&#xff0c;PowerShell 2.0的補丁 下載地址&#xff1…

VIO-slam 系統構建

文章目錄 目錄 文章目錄 前言 一、VIO系統數據的獲取&#xff1a;圖像傳感器選型、IMU傳感器選型 二、建立linux系統ROS環境 三、如何讀取圖像數據 四、如何讀取IMU傳感器數據 五、標定圖像和IMU數據的外參、相機的內參 六、移植VINS-MONO或者VINS-FUSION&#xff1a;主要調試獲…

MySQL5.7多源復制的實驗

MySQL5.7多源復制的實驗 node1: 192.168.2.171 master1 node2: 192.168.2.172 slave node3: 192.168.2.170 master2 node2上執行&#xff1a; change master to master_host192.168.2.171, master_userrpl, master_passwordAbcd1234, master_port3306, master_log…

最優化課堂筆記04:非線性規劃(考點4-5例題)

目錄 4.1 多元函數的泰勒展開 4.2方向導數與梯度 4.2.1方向導數 n元函數在點沿特定方向的方向導數 4.2.2梯度 4.3二次函數及正定矩陣 4.4凸函數與凸規劃 4.4.1凸函數 4.4.2凸規劃 4.4無約束優化問題的極值條件 4.5約束優化問題的極值條件&#xff08;重點考點&#x…

k近鄰算法C++二維情況下的實現

k近鄰算法C二維實現 這是一個k近鄰算法的二維實現&#xff08;即K2的情況&#xff09;。 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <cmath> u…

java——對象學習筆記

1.面向對象&#xff08;OOP&#xff09;的三大特性 對象的行為&#xff08;behavior&#xff09;&#xff1a;可以對對象施加哪些操作&#xff0c;或者可以對對象施加哪些方法。 對象的狀態&#xff08;state&#xff09;&#xff1a;當施加那些方法后&#xff0c;對象如何響應…

C++獲取一段算法程序耗時方法

1、添加頭文件庫#include <chrono> 2、代碼編寫 std::chrono::steady_clock::time_point t1 std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point t2 std::chrono::steady_clock::now(); std::chrono::duration<double> time_used12 st…