iOS開發之第三方分享QQ分享,史上最新最全第三方分享QQ方式實現


本文章源碼地址: https://github.com/zhonggaorong/QQLoginDemo

 

項目搭建參考:  (包含QQ登錄源碼下載 、 QQ sdk集成)

http://blog.csdn.net/zhonggaorong/article/details/51699623

 

分享第三方分享之QQ分享各種坑的總結:

1. 分享老是提示未注冊QQ,解決辦法就是在程序已啟動,就向QQ進行授權。代碼如下

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     [[TencentOAuth alloc] initWithAppId:TENCENT_CONNECT_APP_KEY andDelegate:self];  
  3.     return YES;  
  4. }  

 

 

QQ未注冊錯誤代號 EQQAPIAPPNOTREGISTED

2. 分享的時候, 不跳轉到QQ界面,解決辦法就是在openurl中,添加如下代碼:

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{  
  2.     /** 
  3.      處理由手Q喚起的跳轉請求 
  4.      \param url 待處理的url跳轉請求 
  5.      \param delegate 第三方應用用於處理來至QQ請求及響應的委托對象 
  6.      \return 跳轉請求處理結果,YES表示成功處理,NO表示不支持的請求協議或處理失敗 
  7.      */  
  8.     if ([url.absoluteString hasPrefix:[NSString stringWithFormat:@"tencent%@",TENCENT_CONNECT_APP_KEY]]) {  
  9.         [QQApiInterface handleOpenURL:url delegate:self];  
  10.         return [TencentOAuth HandleOpenURL:url];  
  11.     }  
  12.     return YES;  
  13. }  

 

 

3. iOS9白名單的問題,沒有吧QQ的相關白名單填完成。 完成的如下。

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. <key>LSApplicationQueriesSchemes</key>  
  2. <array>  
  3.     <string>mqqapi</string>  
  4.     <string>mqq</string>  
  5.     <string>mqqOpensdkSSoLogin</string>  
  6.     <string>mqqconnect</string>  
  7.     <string>mqqopensdkdataline</string>  
  8.     <string>mqqopensdkgrouptribeshare</string>  
  9.     <string>mqqopensdkfriend</string>  
  10.     <string>mqqopensdkapi</string>  
  11.     <string>mqqopensdkapiV2</string>  
  12.     <string>mqqopensdkapiV3</string>  
  13.     <string>mqzoneopensdk</string>  
  14.     <string>wtloginmqq</string>  
  15.     <string>wtloginmqq2</string>  
  16.     <string>mqqwpa</string>  
  17.     <string>mqzone</string>  
  18.     <string>mqzonev2</string>  
  19.     <string>mqzoneshare</string>  
  20.     <string>wtloginqzone</string>  
  21.     <string>mqzonewx</string>  
  22.     <string>mqzoneopensdkapiV2</string>  
  23.     <string>mqzoneopensdkapi19</string>  
  24.     <string>mqzoneopensdkapi</string>  
  25.     <string>mqzoneopensdk</string>  
  26. </array>  



 

進入正題:

 

分享的對象包含:

 

QQApiTextObject     文本對象

 

QQApiURLObject      URL對象類型

QQApiExtendObject   擴展數據類型

 

QQApiImageObject    圖片對象

QQApiImageArrayForQZoneObject  圖片數組對象 用於分享圖片到空間,走寫說說路徑,是一個指定為圖片類型的,當圖片數組為空時,默認走文本寫說說

 

QQApiVideoForQZoneObject      視頻對象

 

QQApiWebImageObject           網絡圖片對象

QQApiFileObject               本地文件對象

 

QQApiAudioObject              音頻URL對象

 

QQApiVideoObject        視頻URL對象 :用於分享目標內容為視頻的URL的對象

 

QQApiNewsObject       新聞URL對象

等等。

 

相關的分享對象的權限看下圖:

     分享的核心代碼為: (分享 新聞URL對象為例子)

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. /**   
  2.      分享 新聞URL對象 。 
  3.      獲取一個autorelease的<code>QQApiAudioObject</code> 
  4.      @param url 音頻內容的目標URL 
  5.      @param title 分享內容的標題 
  6.      @param description 分享內容的描述 
  7.      @param previewURL 分享內容的預覽圖像URL 
  8.      @note 如果url為空,調用<code>QQApi#sendMessage:</code>時將返回FALSE 
  9.      */  
  10.       
  11.     NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  12.     NSURL *preimageUrl = [NSURL URLWithString:@"http://www.sizzee.com/index.php/catalog/product/view/id/55730/s/10196171/?SID=au0lhpg54f11nenmrjvhsh0rq6?uk=Y3VzdG9tZXJfaWQ9Mjc0fHByb2R1Y3RfaWQ9NTU3MzA"];  
  13.     QQApiNewsObject* img = [QQApiNewsObject objectWithURL:url title:@"測試分享" description:[NSString stringWithFormat:@"分享內容------新聞URL對象分享 ------test"] previewImageURL:preimageUrl];  
  14.       
  15.     //請求幫助類,分享的所有基礎對象,都要封裝成這種請求對象。  
  16.     SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];  
  17.     QQApiSendResultCode sent = [QQApiInterface sendReq:req];  
  18.       
  19.     //通過自定義的qqdelegate來通知本controller,是否成功分享  
  20.     appdelegate.qqDelegate = self;  
  21.       
  22.     NSLog(@"QQApiSendResultCode %d",sent);  
  23.       
  24.     [self handleSendResult:sent];  
  25.   /*  QQApiSendResultCode 說明 
  26.     EQQAPISENDSUCESS = 0,                      操作成功 
  27.     EQQAPIQQNOTINSTALLED = 1,                   沒有安裝QQ 
  28.     EQQAPIQQNOTSUPPORTAPI = 2, 
  29.     EQQAPIMESSAGETYPEINVALID = 3,              參數錯誤 
  30.     EQQAPIMESSAGECONTENTNULL = 4, 
  31.     EQQAPIMESSAGECONTENTINVALID = 5, 
  32.     EQQAPIAPPNOTREGISTED = 6,                   應用未注冊 
  33.     EQQAPIAPPSHAREASYNC = 7, 
  34.     EQQAPIQQNOTSUPPORTAPI_WITH_ERRORSHOW = 8, 
  35.     EQQAPISENDFAILD = -1,                       發送失敗 
  36.     //qzone分享不支持text類型分享 
  37.     EQQAPIQZONENOTSUPPORTTEXT = 10000, 
  38.     //qzone分享不支持image類型分享 
  39.     EQQAPIQZONENOTSUPPORTIMAGE = 10001, 
  40.     //當前QQ版本太低,需要更新至新版本才可以支持 
  41.     EQQAPIVERSIONNEEDUPDATE = 10002, 
  42.    */  
  43. }  
  44. - (void)handleSendResult:(QQApiSendResultCode)sendResult  
  45. {  
  46.     switch (sendResult)  
  47.     {  
  48.         case EQQAPIAPPNOTREGISTED:  
  49.         {  
  50.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注冊" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  51.             [msgbox show];  
  52.               
  53.               
  54.             break;  
  55.         }  
  56.         case EQQAPIMESSAGECONTENTINVALID:  
  57.         case EQQAPIMESSAGECONTENTNULL:  
  58.         case EQQAPIMESSAGETYPEINVALID:  
  59.         {  
  60.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發送參數錯誤" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  61.             [msgbox show];  
  62.               
  63.               
  64.             break;  
  65.         }  
  66.         case EQQAPIQQNOTINSTALLED:  
  67.         {  
  68.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安裝手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  69.             [msgbox show];  
  70.               
  71.               
  72.             break;  
  73.         }  
  74.         case EQQAPIQQNOTSUPPORTAPI:  
  75.         {  
  76.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  77.             [msgbox show];  
  78.               
  79.               
  80.             break;  
  81.         }  
  82.         case EQQAPISENDFAILD:  
  83.         {  
  84.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發送失敗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  85.             [msgbox show];  
  86.               
  87.               
  88.             break;  
  89.         }  
  90.         case EQQAPIVERSIONNEEDUPDATE:  
  91.         {  
  92.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"當前QQ版本太低,需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  93.             [msgbox show];  
  94.             break;  
  95.         }  
  96.         default:  
  97.         {  
  98.             break;  
  99.         }  
  100.     }  
  101. }  

 

 

上面提到了分享。那么如何來監聽分享是否成功呢。這就要回到我們的appdelegate里面。監聽 

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. - (void)onResp:(QQBaseResp *)resp  


appdelegate中的相關代碼如下:

 

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. **  
  2.  處理來至QQ的響應  
  3.  */  
  4. - (void)onResp:(QQBaseResp *)resp{  
  5.      NSLog(@" ----resp %@",resp);  
  6.       
  7.     // SendMessageToQQResp應答幫助類  
  8.     if ([resp.class isSubclassOfClass: [SendMessageToQQResp class]]) {  //QQ分享回應  
  9.         if (_qqDelegate) {  
  10.             if ([_qqDelegate respondsToSelector:@selector(shareSuccssWithQQCode:)]) {  
  11.                 SendMessageToQQResp *msg = (SendMessageToQQResp *)resp;  
  12.                 NSLog(@"code %@  errorDescription %@  infoType %@",resp.result,resp.errorDescription,resp.extendInfo);  
  13.                 [_qqDelegate shareSuccssWithQQCode:[msg.result integerValue]];  
  14.             }  
  15.         }  
  16.     }  
  17. }  



 

 

完整的代碼如下

appdelegate.h

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  AppDelegate.h  
  3. //  QQLoginDemo  
  4. //  
  5. //  Created by 張國榮 on 16/6/17.  
  6. //  Copyright © 2016年 BateOrganization. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @protocol QQShareDelegate <NSObject>  
  12.   
  13. -(void)shareSuccssWithQQCode:(NSInteger)code;  
  14. @end  
  15.   
  16. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  17.   
  18. @property (strong, nonatomic) UIWindow *window;  
  19. @property (weak  , nonatomic) id<QQShareDelegate> qqDelegate;  
  20.   
  21. @end  

 

 

appdelegate,m

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. //  
  2. //  AppDelegate.m  
  3. //  QQLoginDemo  
  4. //  
  5. //  Created by 張國榮 on 16/6/17.  
  6. //  Copyright © 2016年 BateOrganization. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10. #import <TencentOpenAPI/QQApiInterface.h>  
  11. #import <TencentOpenAPI/TencentOAuth.h>  
  12. #define TENCENT_CONNECT_APP_KEY @"app id"  
  13. @interface AppDelegate ()<QQApiInterfaceDelegate,TencentSessionDelegate>  
  14.   
  15. @end  
  16.   
  17. @implementation AppDelegate  
  18.   
  19.   
  20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  21.     // Override point for customization after application launch.  
  22.       
  23. //    [QQApiInterface ]  
  24.       
  25.     [[TencentOAuth alloc] initWithAppId:TENCENT_CONNECT_APP_KEY andDelegate:self];  
  26. //    NSLog(@"%@",oauth.appId);  
  27.     return YES;  
  28. }  
  29.   
  30. - (void)applicationWillResignActive:(UIApplication *)application {  
  31.     // 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.  
  32.     // 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.  
  33. }  
  34.   
  35.   
  36.   
  37. -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{  
  38.     /** 
  39.      處理由手Q喚起的跳轉請求 
  40.      \param url 待處理的url跳轉請求 
  41.      \param delegate 第三方應用用於處理來至QQ請求及響應的委托對象 
  42.      \return 跳轉請求處理結果,YES表示成功處理,NO表示不支持的請求協議或處理失敗 
  43.      */  
  44.     if ([url.absoluteString hasPrefix:[NSString stringWithFormat:@"tencent%@",TENCENT_CONNECT_APP_KEY]]) {  
  45.         [QQApiInterface handleOpenURL:url delegate:self];  
  46.         return [TencentOAuth HandleOpenURL:url];  
  47.           
  48.     }  
  49.     return YES;  
  50. }  
  51.   
  52. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{  
  53.     /** 
  54.      處理由手Q喚起的跳轉請求 
  55.      \param url 待處理的url跳轉請求 
  56.      \param delegate 第三方應用用於處理來至QQ請求及響應的委托對象 
  57.      \return 跳轉請求處理結果,YES表示成功處理,NO表示不支持的請求協議或處理失敗 
  58.      */  
  59.     if ([url.absoluteString hasPrefix:[NSString stringWithFormat:@"tencent%@",TENCENT_CONNECT_APP_KEY]]) {  
  60.          [QQApiInterface handleOpenURL:url delegate:self];  
  61.         return [TencentOAuth HandleOpenURL:url];  
  62.           
  63.     }  
  64.     return YES;  
  65. }  
  66. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{  
  67.     /** 
  68.      處理由手Q喚起的跳轉請求 
  69.      \param url 待處理的url跳轉請求 
  70.      \param delegate 第三方應用用於處理來至QQ請求及響應的委托對象 
  71.      \return 跳轉請求處理結果,YES表示成功處理,NO表示不支持的請求協議或處理失敗 
  72.      */  
  73.     if ([url.absoluteString hasPrefix:[NSString stringWithFormat:@"tencent%@",TENCENT_CONNECT_APP_KEY]]) {  
  74.         [QQApiInterface handleOpenURL:url delegate:self];  
  75.         return [TencentOAuth HandleOpenURL:url];  
  76.     }  
  77.     return YES;  
  78. }  
  79. /** 
  80.  處理來至QQ的請求 
  81.  */  
  82. - (void)onReq:(QQBaseReq *)req{  
  83.     NSLog(@" ----req %@",req);  
  84. }  
  85.   
  86. /** 
  87.  處理來至QQ的響應 
  88.  */  
  89. - (void)onResp:(QQBaseResp *)resp{  
  90.      NSLog(@" ----resp %@",resp);  
  91.       
  92.     // SendMessageToQQResp應答幫助類  
  93.     if ([resp.class isSubclassOfClass: [SendMessageToQQResp class]]) {  //QQ分享回應  
  94.         if (_qqDelegate) {  
  95.             if ([_qqDelegate respondsToSelector:@selector(shareSuccssWithQQCode:)]) {  
  96.                 SendMessageToQQResp *msg = (SendMessageToQQResp *)resp;  
  97.                 NSLog(@"code %@  errorDescription %@  infoType %@",resp.result,resp.errorDescription,resp.extendInfo);  
  98.                 [_qqDelegate shareSuccssWithQQCode:[msg.result integerValue]];  
  99.             }  
  100.         }  
  101.     }  
  102. }  
  103.   
  104. /** 
  105.  處理QQ在線狀態的回調 
  106.  */  
  107. - (void)isOnlineResponse:(NSDictionary *)response{  
  108.   
  109. }  
  110.   
  111.   
  112. @end  

 

 

viewcontroller.h

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5.   
  6. @end  

 

 

viewcontroller.m

 

[objc]  view plain  copy
 在CODE上查看代碼片派生到我的代碼片
  1. #import "ViewController.h"  
  2. #import <TencentOpenAPI/TencentOAuth.h>  
  3. #import <TencentOpenAPI/TencentApiInterface.h>  
  4. #import <TencentOpenAPI/QQApiInterfaceObject.h>  
  5. #import <TencentOpenAPI/QQApiInterface.h>  
  6. #import "AppDelegate.h"  
  7.   
  8. #define APP_ID @"app id"  
  9. @interface ViewController ()<TencentSessionDelegate,QQShareDelegate>  
  10. {  
  11.     TencentOAuth *_tencentOAuth;  
  12.     NSMutableArray *_permissionArray;   //權限列表  
  13.     AppDelegate *appdelegate;  
  14. }  
  15. @end  
  16.   
  17. @implementation ViewController  
  18.   
  19. - (void)viewDidLoad {  
  20.     [super viewDidLoad];  
  21.     appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;  
  22.     self.view.backgroundColor = [UIColor whiteColor];  
  23. }  
  24.   
  25. #pragma mark QQ登錄  
  26. - (IBAction)loginAction:(id)sender {  
  27.     _tencentOAuth=[[TencentOAuth alloc]initWithAppId:APP_ID andDelegate:self];  
  28.       
  29.     //設置權限數據 , 具體的權限名,在sdkdef.h 文件中查看。  
  30.     _permissionArray = [NSMutableArray arrayWithObjects: kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,nil];  
  31.       
  32.     //登錄操作  
  33.     [_tencentOAuth authorize:_permissionArray inSafari:NO];  
  34. }  
  35.   
  36. /** 
  37.  * 登錄成功后的回調 
  38.  */  
  39. - (void)tencentDidLogin{  
  40.       
  41.     /** Access Token憑證,用於后續訪問各開放接口 */  
  42.     if (_tencentOAuth.accessToken) {  
  43.           
  44.         //獲取用戶信息。 調用這個方法后,qq的sdk會自動調用  
  45.         //- (void)getUserInfoResponse:(APIResponse*) response  
  46.         //這個方法就是 用戶信息的回調方法。  
  47.           
  48.         [_tencentOAuth getUserInfo];  
  49.     }else{  
  50.       
  51.         NSLog(@"accessToken 沒有獲取成功");  
  52.     }  
  53.       
  54. }  
  55.   
  56. /** 
  57.  * 登錄失敗后的回調 
  58.  * \param cancelled 代表用戶是否主動退出登錄 
  59.  */  
  60. - (void)tencentDidNotLogin:(BOOL)cancelled{  
  61.     if (cancelled) {  
  62.         NSLog(@" 用戶點擊取消按鍵,主動退出登錄");  
  63.     }else{  
  64.         NSLog(@"其他原因, 導致登錄失敗");  
  65.     }  
  66. }  
  67.   
  68. /** 
  69.  * 登錄時網絡有問題的回調 
  70.  */  
  71. - (void)tencentDidNotNetWork{  
  72.     NSLog(@"沒有網絡了, 怎么登錄成功呢");  
  73. }  
  74.   
  75.   
  76. /** 
  77.  * 因用戶未授予相應權限而需要執行增量授權。在用戶調用某個api接口時,如果服務器返回操作未被授權,則觸發該回調協議接口,由第三方決定是否跳轉到增量授權頁面,讓用戶重新授權。 
  78.  * \param tencentOAuth 登錄授權對象。 
  79.  * \param permissions 需增量授權的權限列表。 
  80.  * \return 是否仍然回調返回原始的api請求結果。 
  81.  * \note 不實現該協議接口則默認為不開啟增量授權流程。若需要增量授權請調用\ref TencentOAuth#incrAuthWithPermissions: \n注意:增量授權時用戶可能會修改登錄的帳號 
  82.  */  
  83. - (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions{  
  84.       
  85.     // incrAuthWithPermissions是增量授權時需要調用的登錄接口  
  86.     // permissions是需要增量授權的權限列表  
  87.     [tencentOAuth incrAuthWithPermissions:permissions];  
  88.     return NO; // 返回NO表明不需要再回傳未授權API接口的原始請求結果;  
  89.     // 否則可以返回YES  
  90. }  
  91.   
  92. /** 
  93.  * [該邏輯未實現]因token失效而需要執行重新登錄授權。在用戶調用某個api接口時,如果服務器返回token失效,則觸發該回調協議接口,由第三方決定是否跳轉到登錄授權頁面,讓用戶重新授權。 
  94.  * \param tencentOAuth 登錄授權對象。 
  95.  * \return 是否仍然回調返回原始的api請求結果。 
  96.  * \note 不實現該協議接口則默認為不開啟重新登錄授權流程。若需要重新登錄授權請調用\ref TencentOAuth#reauthorizeWithPermissions: \n注意:重新登錄授權時用戶可能會修改登錄的帳號 
  97.  */  
  98. - (BOOL)tencentNeedPerformReAuth:(TencentOAuth *)tencentOAuth{  
  99.     return YES;  
  100. }  
  101.   
  102. /** 
  103.  * 用戶通過增量授權流程重新授權登錄,token及有效期限等信息已被更新。 
  104.  * \param tencentOAuth token及有效期限等信息更新后的授權實例對象 
  105.  * \note 第三方應用需更新已保存的token及有效期限等信息。 
  106.  */  
  107. - (void)tencentDidUpdate:(TencentOAuth *)tencentOAuth{  
  108.     NSLog(@"增量授權完成");  
  109.     if (tencentOAuth.accessToken  
  110.         && 0 != [tencentOAuth.accessToken length])  
  111.     { // 在這里第三方應用需要更新自己維護的token及有效期限等信息  
  112.         // **務必在這里檢查用戶的openid是否有變更,變更需重新拉取用戶的資料等信息** _labelAccessToken.text = tencentOAuth.accessToken;  
  113.     }  
  114.     else  
  115.     {  
  116.         NSLog(@"增量授權不成功,沒有獲取accesstoken");  
  117.     }  
  118.   
  119. }  
  120.   
  121. /** 
  122.  * 用戶增量授權過程中因取消或網絡問題導致授權失敗 
  123.  * \param reason 授權失敗原因,具體失敗原因參見sdkdef.h文件中\ref UpdateFailType 
  124.  */  
  125. - (void)tencentFailedUpdate:(UpdateFailType)reason{  
  126.       
  127.     switch (reason)  
  128.     {  
  129.         case kUpdateFailNetwork:  
  130.         {  
  131.             //            _labelTitle.text=@"增量授權失敗,無網絡連接,請設置網絡";  
  132.             NSLog(@"增量授權失敗,無網絡連接,請設置網絡");  
  133.             break;  
  134.         }  
  135.         case kUpdateFailUserCancel:  
  136.         {  
  137.             //            _labelTitle.text=@"增量授權失敗,用戶取消授權";  
  138.             NSLog(@"增量授權失敗,用戶取消授權");  
  139.             break;  
  140.         }  
  141.         case kUpdateFailUnknown:  
  142.         default:  
  143.         {  
  144.             NSLog(@"增量授權失敗,未知錯誤");  
  145.             break;  
  146.         }  
  147.     }  
  148.   
  149.       
  150. }  
  151.   
  152. #pragma mark 登錄成功后,回調 - 返回對應QQ的相關信息  
  153. /** 
  154.  * 獲取用戶個人信息回調 
  155.  * \param response API返回結果,具體定義參見sdkdef.h文件中\ref APIResponse 
  156.  * \remarks 正確返回示例: \snippet example/getUserInfoResponse.exp success 
  157.  *          錯誤返回示例: \snippet example/getUserInfoResponse.exp fail 
  158.  */  
  159. - (void)getUserInfoResponse:(APIResponse*) response{  
  160.       
  161.     //這里 與自己服務器進行對接,看怎么利用這獲取到的個人信息。  
  162.       
  163.     NSDictionary *dic = response.jsonResponse;  
  164.     NSLog(@" response %@",dic);  
  165.       
  166.       
  167. }  
  168.   
  169. #pragma mark QQ分享  
  170. - (IBAction)QQshareAction:(id)sender {  
  171.     /**   
  172.      分享 新聞URL對象 。 
  173.      獲取一個autorelease的<code>QQApiAudioObject</code> 
  174.      @param url 音頻內容的目標URL 
  175.      @param title 分享內容的標題 
  176.      @param description 分享內容的描述 
  177.      @param previewURL 分享內容的預覽圖像URL 
  178.      @note 如果url為空,調用<code>QQApi#sendMessage:</code>時將返回FALSE 
  179.      */  
  180.       
  181.     NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  182.     NSURL *preimageUrl = [NSURL URLWithString:@"http://www.sizzee.com/index.php/catalog/product/view/id/55730/s/10196171/?SID=au0lhpg54f11nenmrjvhsh0rq6?uk=Y3VzdG9tZXJfaWQ9Mjc0fHByb2R1Y3RfaWQ9NTU3MzA"];  
  183.     QQApiNewsObject* img = [QQApiNewsObject objectWithURL:url title:@"測試分享" description:[NSString stringWithFormat:@"分享內容------新聞URL對象分享 ------test"] previewImageURL:preimageUrl];  
  184.       
  185.     //請求幫助類,分享的所有基礎對象,都要封裝成這種請求對象。  
  186.     SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];  
  187.     QQApiSendResultCode sent = [QQApiInterface sendReq:req];  
  188.       
  189.     //通過自定義的qqdelegate來通知本controller,是否成功分享  
  190.     appdelegate.qqDelegate = self;  
  191.       
  192.     NSLog(@"QQApiSendResultCode %d",sent);  
  193.       
  194.     [self handleSendResult:sent];  
  195.   /*  QQApiSendResultCode 說明 
  196.     EQQAPISENDSUCESS = 0,                      操作成功 
  197.     EQQAPIQQNOTINSTALLED = 1,                   沒有安裝QQ 
  198.     EQQAPIQQNOTSUPPORTAPI = 2, 
  199.     EQQAPIMESSAGETYPEINVALID = 3,              參數錯誤 
  200.     EQQAPIMESSAGECONTENTNULL = 4, 
  201.     EQQAPIMESSAGECONTENTINVALID = 5, 
  202.     EQQAPIAPPNOTREGISTED = 6,                   應用未注冊 
  203.     EQQAPIAPPSHAREASYNC = 7, 
  204.     EQQAPIQQNOTSUPPORTAPI_WITH_ERRORSHOW = 8, 
  205.     EQQAPISENDFAILD = -1,                       發送失敗 
  206.     //qzone分享不支持text類型分享 
  207.     EQQAPIQZONENOTSUPPORTTEXT = 10000, 
  208.     //qzone分享不支持image類型分享 
  209.     EQQAPIQZONENOTSUPPORTIMAGE = 10001, 
  210.     //當前QQ版本太低,需要更新至新版本才可以支持 
  211.     EQQAPIVERSIONNEEDUPDATE = 10002, 
  212.    */  
  213. }  
  214. - (void)handleSendResult:(QQApiSendResultCode)sendResult  
  215. {  
  216.     switch (sendResult)  
  217.     {  
  218.         case EQQAPIAPPNOTREGISTED:  
  219.         {  
  220.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注冊" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  221.             [msgbox show];  
  222.               
  223.               
  224.             break;  
  225.         }  
  226.         case EQQAPIMESSAGECONTENTINVALID:  
  227.         case EQQAPIMESSAGECONTENTNULL:  
  228.         case EQQAPIMESSAGETYPEINVALID:  
  229.         {  
  230.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發送參數錯誤" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  231.             [msgbox show];  
  232.               
  233.               
  234.             break;  
  235.         }  
  236.         case EQQAPIQQNOTINSTALLED:  
  237.         {  
  238.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安裝手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  239.             [msgbox show];  
  240.               
  241.               
  242.             break;  
  243.         }  
  244.         case EQQAPIQQNOTSUPPORTAPI:  
  245.         {  
  246.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  247.             [msgbox show];  
  248.               
  249.               
  250.             break;  
  251.         }  
  252.         case EQQAPISENDFAILD:  
  253.         {  
  254.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發送失敗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  255.             [msgbox show];  
  256.               
  257.               
  258.             break;  
  259.         }  
  260.         case EQQAPIVERSIONNEEDUPDATE:  
  261.         {  
  262.             UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"當前QQ版本太低,需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  263.             [msgbox show];  
  264.             break;  
  265.         }  
  266.         default:  
  267.         {  
  268.             break;  
  269.         }  
  270.     }  
  271. }  
  272.   
  273. #pragma mark QQ分享回調代理  
  274. /* 返回碼 對照說明 
  275.  0   成功 
  276.  -1  參數錯誤 
  277.  -2  該群不在自己的群列表里面 
  278.  -3  上傳圖片失敗 
  279.  -4  用戶放棄當前操作 
  280.  -5  客戶端內部處理錯誤 
  281.  */  
  282. -(void)shareSuccssWithQQCode:(NSInteger)code{  
  283.     NSLog(@"code %ld",(long)code);  
  284.     if (code == 0) {  
  285.         UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"警告" message:@"分享成功" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];  
  286.         [aler show];  
  287.     }else{  
  288.         UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"警告" message:@"分享失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil];  
  289.         [aler show];  
  290.     }  
  291. }  
  292.   
  293.   
  294. - (void)didReceiveMemoryWarning {  
  295.     [super didReceiveMemoryWarning];  
  296.     // Dispose of any resources that can be recreated.  
  297. }  
  298.   
  299. @end  




 

大功告成。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM