iOS开发笔记14:微博/微信登录与分享、微信/支付宝支付
2016-04-01 00:12
579 查看
产品中接入了微博/微信的第三方登录分享功能、微信和支付宝的第三方支付功能,之前在开发过程中涉及到这些部分,于是抽空将接入过程梳理了一遍。
设置URL Scheme,用于从第三方应用或浏览器中启动app
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/b86f5043f5e76931687d28619bd09647.png)
将SDK添加到工程中(支持CocoaPods),app启动时(didFinishLaunchingWithOptions)注册appkey
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/8156fc6cbc6010caf48a4176a23d539e.png)
重写AppDelegate 的handleOpenURL和openURL方法,其它引用类库等开发环境设置具体参考SDK中提供的《微博iOS平台SDK 档》即可。后续即可针对登录分享场景调用SDK
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/720b86713c4fd7c711c040e12c21dee4.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/575a8edd6e0015c5366deb2eb1f0e4fc.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/11f0f923be72b4560413833cd925e1e5.png)
开放平台SDK相关说明
配置相关与微博SDK基本一致,设置URL Scheme、添加相关类库、启动时注册appKey及重写handleOpenURL和openURL方法。
需要注意的是如果没有安装微信客户端,是无法像微博或支付宝那样调起网页进行操作的,所以需要使用SDK提供的方法“[WXApi isWXAppInstalled]”检测微信是否安装,若没有安装,则微信相关的操作无法进行,微信登录或分享的标识需要隐藏。
集成流程文档
设置URL Scheme、添加相关类库,设置开发环境,详细参考DEMO及集成流程文档。
需要特别注意的点:
①Header Search Paths的设置
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/99ac0dd472ac37eb3a2d03df1c130213.png)
②白名单设置
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/e6ad80ee48a012e033c4b1fff7db9e3d.png)
③私钥生成
RSA私钥及公钥生成 支付宝集成获取私钥与公钥
在支付宝支付sdk demo里提供了相关工具
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/ef0502ffb97c9c9918d6978d91e8de6b.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/223d8b6c37c12580bf649c251872a50a.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/ee7896e040405541574b084912644e58.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/70f1d1a77e530968e2280fc9d026f206.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/46b45c63494eafbb0f162863a4c9d8e5.png)
这里需要注意三点:
①微博字数限制为140字,超过无法分享。
②分享操作前需要更新access_token,防止过期导致无法进行相关操作
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/69c2afedb5c551f1ec9bed72676f3036.png)
响应方法didReceiveWeiboResponse中拿到申请认证返回结果,刷新存储access_token等值即可
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/6a21d4dadac0a822ab8b4af379923685.png)
③分享时图片参数可以是一个url或者WBImageObject,二选一即可,如果二者都有值,则url被忽略。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/340f8da5aae46709c2f3530fa5e29275.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/bf3350ad13436bb040ec3182accd74d9.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/5f11c2ad5d7b9aca930a75061b437a58.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/086fd0e37f9beab27905a7c62d8c5ce5.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/a79a73998fa6d5bccb3d5b247f57abd0.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/0e77d9a3f6a7ae0d97a106475ee8e7d7.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/3061da50bb56a5bfb2a9001eb1827728.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/f07dc1a86ba5efe00cdb613db50c1cc8.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/bf088732c0cdae35e0234f6f0aedf700.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/aa78c87392a63d71cab53ae21ed0ecd6.png)
点击分享的网页,调起客户端则在微信的协议方法“onReq”里进行处理,根据相应规则逻辑在应用内跳转页面等等。
参考微信支付提供的demo进行接入,设计的是客户端与后台交互,由后台来调用微信支付接口,主要流程如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/55810e0b5cf49726cfa35f5330317f40.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/8f7e6206cbb7975414880c169ca7f15d.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/25c2ef5b32f9e1417ec81e0842ce39c7.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/b8eb60d5d5a653d73516fcc1b2e05e82.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/fe89c5adb1bbb14af44025f3bedeb5d6.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/e082f219beca82269a6a3676019604ac.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/170363ad725002980582a98e58a2ad7a.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/1c21e7eb3f0101bbc9cd63186c74a006.png)
在AppDelagate的“openURL”及“handleOpenURL”中判断支付宝支付操作,切回应用时在回调block中做相关业务逻辑处理
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/544d98d51868e9ec7338ca6cecb5472b.png)
1.微博、微信、支付宝SDK相关接入设置
(1)微博SDK
SDK下载设置URL Scheme,用于从第三方应用或浏览器中启动app
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/b86f5043f5e76931687d28619bd09647.png)
将SDK添加到工程中(支持CocoaPods),app启动时(didFinishLaunchingWithOptions)注册appkey
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/8156fc6cbc6010caf48a4176a23d539e.png)
重写AppDelegate 的handleOpenURL和openURL方法,其它引用类库等开发环境设置具体参考SDK中提供的《微博iOS平台SDK 档》即可。后续即可针对登录分享场景调用SDK
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/720b86713c4fd7c711c040e12c21dee4.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/575a8edd6e0015c5366deb2eb1f0e4fc.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/11f0f923be72b4560413833cd925e1e5.png)
(2)微信SDK
下载地址开放平台SDK相关说明
配置相关与微博SDK基本一致,设置URL Scheme、添加相关类库、启动时注册appKey及重写handleOpenURL和openURL方法。
需要注意的是如果没有安装微信客户端,是无法像微博或支付宝那样调起网页进行操作的,所以需要使用SDK提供的方法“[WXApi isWXAppInstalled]”检测微信是否安装,若没有安装,则微信相关的操作无法进行,微信登录或分享的标识需要隐藏。
(3)支付宝SDK
移动支付DEMO及SDK集成流程文档
设置URL Scheme、添加相关类库,设置开发环境,详细参考DEMO及集成流程文档。
需要特别注意的点:
①Header Search Paths的设置
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/99ac0dd472ac37eb3a2d03df1c130213.png)
②白名单设置
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/e6ad80ee48a012e033c4b1fff7db9e3d.png)
③私钥生成
RSA私钥及公钥生成 支付宝集成获取私钥与公钥
在支付宝支付sdk demo里提供了相关工具
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/ef0502ffb97c9c9918d6978d91e8de6b.png)
2.微博登录
(1)发起微博授权请求,切换到微博
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/223d8b6c37c12580bf649c251872a50a.png)
(2)授权操作后返回客户端进行相关处理
在weibo sdk的协议方法“- (void)didReceiveWeiboResponse:(WBBaseResponse *)response”中判断操作行为及状态,通过认证申请后即可去请求获取用户信息,获得用户信息后再进行登录相关处理即可![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/ee7896e040405541574b084912644e58.png)
3.微博分享
微博分享其实就是发微博操作,对应的是一个接口,不像微信分享那样会跳转过去,在微信里确认输入信息后分享,所以在应用里需要构建一个页面,将分享到微博的信息先展示预览出来,供用户修改文字等等,点击确定再执行分享操作。根据分享对象构造相关参数调用接口即可![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/70f1d1a77e530968e2280fc9d026f206.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/46b45c63494eafbb0f162863a4c9d8e5.png)
这里需要注意三点:
①微博字数限制为140字,超过无法分享。
②分享操作前需要更新access_token,防止过期导致无法进行相关操作
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/69c2afedb5c551f1ec9bed72676f3036.png)
响应方法didReceiveWeiboResponse中拿到申请认证返回结果,刷新存储access_token等值即可
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/6a21d4dadac0a822ab8b4af379923685.png)
③分享时图片参数可以是一个url或者WBImageObject,二选一即可,如果二者都有值,则url被忽略。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/340f8da5aae46709c2f3530fa5e29275.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/bf3350ad13436bb040ec3182accd74d9.png)
4.微信登录
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/5f11c2ad5d7b9aca930a75061b437a58.png)
(1)请求授权临时票据code参数
code在获取access_token后失效![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/086fd0e37f9beab27905a7c62d8c5ce5.png)
(2)通过code获取acces_token
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/a79a73998fa6d5bccb3d5b247f57abd0.png)
(3)根据上一步获得的access_token及openId请求获取用户信息,实现相关业务逻辑
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/0e77d9a3f6a7ae0d97a106475ee8e7d7.png)
5.微信分享
微信分享有分享到朋友圈(WXSceneTimeline)、分享到对话(WXSceneSession)两种,最常见的也就是图文类型的网页。![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/3061da50bb56a5bfb2a9001eb1827728.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/f07dc1a86ba5efe00cdb613db50c1cc8.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/bf088732c0cdae35e0234f6f0aedf700.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/aa78c87392a63d71cab53ae21ed0ecd6.png)
点击分享的网页,调起客户端则在微信的协议方法“onReq”里进行处理,根据相应规则逻辑在应用内跳转页面等等。
6.微信支付
微信支付开发文档参考微信支付提供的demo进行接入,设计的是客户端与后台交互,由后台来调用微信支付接口,主要流程如下:
(1)提交订单数据
选择商品并填写相关信息后提交订单,后台返回订单数据(后台调用微信的统一下单api),后续支付时需要用到。![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/55810e0b5cf49726cfa35f5330317f40.png)
(2)调起微信准备支付
根据之前拿到的订单数据(订包括单id),向后台发起微信支付的请求,拿到请求结果后调起微信客户端准备支付![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/8f7e6206cbb7975414880c169ca7f15d.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/25c2ef5b32f9e1417ec81e0842ce39c7.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/b8eb60d5d5a653d73516fcc1b2e05e82.png)
(3)支付完成返回客户端进行相关处理
在“-(void) onResp:(BaseResp*)resp”方法判断支付操作状态,进行相应处理,例如支付成功后跳转到订单详情页给用户展示订单及支付信息等等。7.支付宝支付
同样,参考支付宝支付demo进行接入,客户端与后台交互,由后台来调用支付宝支付的接口,主要步骤如下:(1)提交订单数据
选择商品并填写相关信息后提交订单,后台返回订单数据。![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/fe89c5adb1bbb14af44025f3bedeb5d6.png)
(2)调起支付宝客户端或网页准备支付
根据之前拿到的订单数据(订包括单id),向后台发起支付宝支付的请求,拿到请求结果后调起支付宝客户端准备支付,若没有安装支付宝,则调用支付宝网页进行支付。![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/e082f219beca82269a6a3676019604ac.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/170363ad725002980582a98e58a2ad7a.png)
(3)支付完成返回客户端进行相关处理
在支付结果回调block中根据支付操作状态进行相关业务逻辑处理![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/1c21e7eb3f0101bbc9cd63186c74a006.png)
在AppDelagate的“openURL”及“handleOpenURL”中判断支付宝支付操作,切回应用时在回调block中做相关业务逻辑处理
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/544d98d51868e9ec7338ca6cecb5472b.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/1cac400d173cb658c74f42ed92f7898d.png)
相关文章推荐
- zabbix 通过微信企业小助手发送报警
- Android中的微信、支付宝支付功能的简单实现--摘自肖玲玲老师博客160305
- iOS微信支持遇到的问题:只显示一个确定按钮、onResp不回调、闪回
- 小程序 - 最大递增数
- Java实现微信菜单json字符串拼接
- 安卓小程序(一)计算器
- 微信样式相册选择器
- 微信开发中用dom4j解析消息时的错误
- 微信公众号开发时,按物理返回键时,提示是否退出对话框
- 微信开发中的编码问题
- 微信支付常见问题
- ViewPage+Fragment + ListView可左右滑动翻页(仿微信页面)
- 微信支付学习记录1
- 函数——小程序,大作用
- 微信服务升级——指尖上的超级表格
- 微信调试环境介绍
- [算法]微信红包
- 微信公众号开发--微信JS-SDK分享到朋友圈和分享给朋友
- 如何在微信浏览器的H5商城获取用户openid
- Android 仿微信朋友圈9宫格图片展示&多选图片