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.mzph.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…