100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > iOS 第三方分享 支付原生平台集成

iOS 第三方分享 支付原生平台集成

时间:2020-04-17 07:13:33

相关推荐

iOS 第三方分享 支付原生平台集成

github链接地址(一个包含QQ,微信,新浪微博原生平台集成以及支付宝微信支付的Demo)

一、准备第三方资源文件:

1.创建podfile,导入第三方库

pod 'YYKit'pod 'Masonry'pod 'WechatOpenSDK'pod 'Weibo_SDK', :git => '/sinaweibosdk/weibo_ios_sdk.git'

2.下载TencentOpenAPI.framework:

下载地址: TencentOpenAPI.framework3.下载AlipaySDK.framework

下载地址: AlipaySDK.framework

二、添加依赖库:

libc++.tbdlibz.tbdSystemConfiguration.frameworkCoreTelephony.frameworkQuartzCore.frameworkCoreText.frameworkCoreGraphics.frameworkUIKit.frameworkFoundation.frameworkCFNetwork.frameworkCoreMotion.framework

三、添加应用白名单:

在项目中的info.plist中加入应用白名单

<key>LSApplicationQueriesSchemes</key><array><string>wechat</string><string>weixin</string><string>sinaweibohd</string><string>sinaweibo</string><string>sinaweibosso</string><string>weibosdk</string><string>weibosdk2.5</string><string>mqqapi</string><string>mqq</string><string>mqqOpensdkSSoLogin</string><string>mqqconnect</string><string>mqqopensdkdataline</string><string>mqqopensdkgrouptribeshare</string><string>mqqopensdkfriend</string><string>mqqopensdkapi</string><string>mqqopensdkapiV2</string><string>mqqopensdkapiV3</string><string>mqzoneopensdk</string><string>wtloginmqq</string><string>wtloginmqq2</string><string>mqqwpa</string><string>mqzone</string><string>mqzonev2</string><string>mqzoneshare</string><string>wtloginqzone</string><string>mqzonewx</string><string>mqzoneopensdkapiV2</string><string>mqzoneopensdkapi19</string><string>mqzoneopensdkapi</string><string>mqzoneopensdk</string><string>alipay</string><string>alipayshare</string></array><key>LSRequiresIPhoneOS</key><true/><key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>

四、URL Scheme:

1.QQtencent+腾讯QQ互联应用appID2.新浪微博:wb+新浪微博Appkey3.微信:wx+微信Appkey

五、代码:

WHAppDelegate.h

#import <UIKit/UIKit.h>#import <TencentOpenAPI/QQApiInterface.h>#import <TencentOpenAPI/TencentOAuth.h>#import "WeiboSDK.h"@interface WHAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (nonatomic, strong) TencentOAuth *tencentOAuth;@property (nonatomic, strong) WeiboSDK *weibo;@property (nonatomic, strong) NSString *wbtoken;@end

WHAppDelegate.m

#import "WHAppDelegate.h"#import "WHAppDelegate+WHAppService.h" // 应用服务#import "WHAppDelegate+WHThirdResponse.h" // 第三方回调, 包含支付、分享@interface WHAppDelegate ()@end@implementation WHAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];[self setUpAppService];[self setUpWXShareService];[self setUpQQService];[self setUpSinaWeiBoService];return YES;}@end

WHAppDelegate+WHThirdResponse.h

#import "WHAppDelegate.h"#import "WXApi.h"@interface WHAppDelegate (WHThirdResponse)<WXApiDelegate, QQApiInterfaceDelegate, TencentSessionDelegate, WeiboSDKDelegate>- (void)setUpWXShareService; // 微信- (void)setUpQQService; // QQ- (void)setUpSinaWeiBoService; // 新浪微博@end

WHAppDelegate+WHThirdResponse.m

#import "WHAppDelegate+WHThirdResponse.h"#import <AlipaySDK/AlipaySDK.h>@implementation WHAppDelegate (WHThirdResponse)#pragma mark - —————— 注册第三方服务 ——————- (void)setUpWXShareService { // 微信[WXApi registerApp:kWXAppKey];}- (void)setUpQQService { // QQself.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppID andDelegate:self];}- (void)setUpSinaWeiBoService { // 新浪微博[WeiboSDK enableDebugMode:NO];[WeiboSDK registerApp:kSinaAppKey];}#pragma mark - —————— WeiboSDKDelegate ——————- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {}- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {if ([response isKindOfClass:WBSendMessageToWeiboResponse.class]) {WBSendMessageToWeiboResponse *sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse *)response;NSString *accessToken = [sendMessageToWeiboResponse.authResponse accessToken];if (accessToken) {self.wbtoken = accessToken;}if (response.statusCode == WeiboSDKResponseStatusCodeSuccess) {// [WHView showAlertwithTitle:nil message:@"分享成功" okAction:nil cancelAction:nil];} else if (response.statusCode == WeiboSDKResponseStatusCodeUserCancel) {// [WHView showAlertwithTitle:nil message:@"取消分享" okAction:nil cancelAction:nil];} else {// [WHView showAlertwithTitle:nil message:@"分享失败" okAction:nil cancelAction:nil];}}else if ([response isKindOfClass:WBAuthorizeResponse.class]) {if (response.statusCode == 0) {// 新浪微博授权成功// [WHView showAlertwithTitle:nil message:@"授权成功" okAction:nil cancelAction:nil];} else {// 新浪微博授权失败// [WHView showAlertwithTitle:nil message:@"授权失败" okAction:nil cancelAction:nil];}}}#pragma mark - —————— QQApiInterfaceDelegate | WXApiDelegate ——————- (void)onReq:(QQBaseReq *)req {DLog(@"——————req—————— %@", req);}- (void)onResp:(NSObject *)resp {DLog(@"——————resp—————— %@", resp);// SendMessageToQQResp应答帮助类if ([resp.class isSubclassOfClass:[SendMessageToQQResp class]]) { //QQ分享回应SendMessageToQQResp *msgToQQ = (SendMessageToQQResp *)resp;NSLog(@"code %@ errorDescription %@ infoType %@", msgToQQ.result, msgToQQ.errorDescription, msgToQQ.extendInfo);SendMessageToQQResp * tmpResp = (SendMessageToQQResp *)resp;if (tmpResp.type == ESENDMESSAGETOQQRESPTYPE && tmpResp.result.integerValue == 0) { // 分享成功// [WHView showAlertwithTitle:nil message:@"分享成功" okAction:nil cancelAction:nil];} else if (tmpResp.type == ESENDMESSAGETOQQRESPTYPE && tmpResp.result.integerValue == -4) { // 取消分享// [WHView showAlertwithTitle:nil message:@"取消分享" okAction:nil cancelAction:nil];} else {// [WHView showAlertwithTitle:nil message:@"分享失败" okAction:nil cancelAction:nil];}}/** 微信 App 分享功能调整* https://open./cgi-bin/announce?spm=a311a.9588098.0.0&action=getannouncement&key=11534138374cE6li&version=*/// if ([resp isKindOfClass:[SendMessageToWXResp class]]) {// SendMessageToWXResp *msgToWX = (SendMessageToWXResp *)resp;// if (msgToWX.errCode == 0) {// [WHView showAlertwithTitle:nil message:@"分享成功" okAction:nil cancelAction:nil];// } else {// [WHView showAlertwithTitle:nil message:@"分享失败" okAction:nil cancelAction:nil];// }// }if ([resp isKindOfClass:[PayResp class]]) {PayResp *payResp = (PayResp *)resp;NSString *payResult = [NSString stringWithFormat:@"errcode: %d", payResp.errCode];// 支付返回结果,实际支付结果需要去微信服务器端查询switch (payResp.errCode) {case 0:payResult = @"支付成功";break;case -1:payResult = @"支付结果:失败!";break;case -2:payResult = @"用户已经退出支付!";break;default:payResult = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", payResp.errCode, payResp.errStr];}// [WHView showAlertwithTitle:nil message:payResult okAction:nil cancelAction:nil];}}- (void)isOnlineResponse:(NSDictionary *)response {}#pragma mark - —————— TencentSessionDelegate ——————/** 登录成功 */- (void)tencentDidLogin {if (self.tencentOAuth.accessToken && 0 != [self.tencentOAuth.accessToken length]) {// 记录登录用户的OpenID、Token以及过期时间[self.tencentOAuth getUserInfo];} else {// 登录不成功 没有获取accesstoken}}- (void)getUserInfoResponse:(APIResponse *)response {DLog(@"response:%@", response.jsonResponse);}/** 非网络错误导致登录失败 */- (void)tencentDidNotLogin:(BOOL)cancelled {if (cancelled) { //用户取消登录DLog(@"用户取消登录");} else { // 登录失败DLog(@"登录失败");}}/** 网络错误导致登录失败 */- (void)tencentDidNotNetWork {// 无网络连接,请设置网络}#pragma mark - —————— Handle openURL ——————- (void)gridResult:(NSDictionary *)resultDic {NSNumber *resultStatus = resultDic[@"resultStatus"];if (resultStatus.intValue == 9000) {// [WHView showAlertwithTitle:nil message:@"支付成功" okAction:nil cancelAction:nil];} else {// [WHView showAlertwithTitle:nil message:@"支付失败" okAction:nil cancelAction:nil];}}- (BOOL)handleOpenURL:(NSURL *)url {if ([url.absoluteString hasPrefix:NSStringFormat(@"tencent%@", kQQAppID)]) {[QQApiInterface handleOpenURL:url delegate:self];return [TencentOAuth HandleOpenURL:url];}if ([url.absoluteString hasPrefix:NSStringFormat(@"wb%@", kSinaAppKey)]) {return [WeiboSDK handleOpenURL:url delegate:self];}if ([url.host isEqualToString:@"platformId=wechat"] || [url.host isEqualToString:@"pay"] || [url.scheme isEqualToString:kWXAppKey]) {return [WXApi handleOpenURL:url delegate:self];}if ([url.host isEqualToString:@"safepay"]) { // 处理支付宝支付结果[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {DLog(@"result2 = %@", resultDic);[self gridResult:resultDic];}];}return YES;}// NOTE: 9.0以后使用新API接口- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {return [self handleOpenURL:url];}- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {return [self handleOpenURL:url];}- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation {return [self handleOpenURL:url];}@end

WHThirdShareTool.h

#import <Foundation/Foundation.h>typedef NS_ENUM(NSUInteger, WHQQShareType) {WHQQShareTypeToQQ,WHQQShareTypeToQZone,};@interface WHThirdShareTool : NSObject#pragma mark —————— QQ分享 ——————/** 纯文本分享 */+ (void)QQMakeShareText:(NSString *)text;/** 纯图片分享 */+ (void)QQMakeShareTitle:(NSString *)title description:(NSString *)description image:(UIImage *)image;/** 新闻分享 */+ (void)QQMakeShareNewsUrlStr:(NSString *)urlStr title:(NSString *)title description:(NSString *)description previewImageUrl:(NSString *)previewImageUrl shareType:(WHQQShareType)shareType;/** 音乐分享 */+ (void)QQMakeShareAudioUrlStr:(NSString *)urlStr title:(NSString *)title description:(NSString *)description previewImageUrl:(NSString *)previewImageUrl shareType:(WHQQShareType)shareType;/** 视频分享 */+ (void)QQMakeShareVideoUrlStr:(NSString *)urlStr title:(NSString *)title description:(NSString *)description previewImageUrl:(NSString *)previewImageUrl flashURL:(NSString *)flashURL image:(UIImage *)image shareType:(WHQQShareType)shareType;#pragma mark —————— 微信分享 ——————+ (void)WXMakeShareTitle:(NSString *)title description:(NSString *)description webpageUrl:(NSString *)webpageUrl image:(UIImage *)image type:(int)type;#pragma mark —————— 新浪微博分享 ——————+ (void)SinaWeiBoMakeShareText:(NSString *)text image:(UIImage *)image objectID:(NSString *)objectID title:(NSString *)title description:(NSString *)description thumbImage:(UIImage *)thumbImage webpageUrl:(NSString *)webpageUrl;@end

WHThirdShareTool.m

#import "WHThirdShareTool.h"#import <TencentOpenAPI/QQApiInterface.h>#import <TencentOpenAPI/QQApiInterfaceObject.h>#import "WXApi.h"#import "WeiboSDK.h"#import "WHAppDelegate.h"@implementation WHThirdShareTool#pragma mark —————— QQ分享 ——————+ (void)handleSendResult:(QQApiSendResultCode)sendResult {NSString *errorMsg = @"";switch (sendResult) {case EQQAPISENDSUCESS:errorMsg = @"成功";break;case EQQAPIAPPNOTREGISTED:errorMsg = @"App未注册";break;case EQQAPIMESSAGECONTENTINVALID:case EQQAPIMESSAGECONTENTNULL:case EQQAPIMESSAGETYPEINVALID:errorMsg = @"发送参数错误";break;case EQQAPIQQNOTINSTALLED:errorMsg = @"未安装手Q";break;case EQQAPIQQNOTSUPPORTAPI:errorMsg = @"API接口不支持";break;case EQQAPISENDFAILD:errorMsg = @"发送失败";break;case EQQAPIVERSIONNEEDUPDATE:errorMsg = @"当前QQ版本太低,需要更新";break;default:errorMsg = @"未知错误!";break;}DLog(@"errorMsg = %@", errorMsg);}+ (void)QQMakeShare:(QQBaseReq *)req shareType:(WHQQShareType)shareType {if (shareType == WHQQShareTypeToQQ) { // 将内容分享到QQQQApiSendResultCode sent = [QQApiInterface sendReq:req];[self handleSendResult:sent];} else if (shareType == WHQQShareTypeToQZone) { // 将内容分享到qzoneQQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];[self handleSendResult:sent];}}/** 纯文本分享* [WHThirdShareTool QQMakeShareText:@"文本分享测试"];*/+ (void)QQMakeShareText:(NSString *)text {QQApiTextObject *txtObj = [QQApiTextObject objectWithText:text];SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];[self QQMakeShare:req shareType:WHQQShareTypeToQQ];}/** 纯图片分享* [WHThirdShareTool QQMakeShareTitle:@"测试标题" description:@"测试描述" image:IMAGE_NAMED(@"share_back_png")];*/+ (void)QQMakeShareTitle:(NSString *)title description:(NSString *)description image:(UIImage *)image {NSData *imgData = UIImagePNGRepresentation(image);QQApiImageObject *imgObj = [QQApiImageObject objectWithData:imgDatapreviewImageData:imgDatatitle:titledescription:description];SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];[self QQMakeShare:req shareType:WHQQShareTypeToQQ];}/** 新闻分享* [WHThirdShareTool QQMakeShareNewsUrlStr:@"" title:@"测试标题" description:@"测试描述" previewImageUrl:nil shareType:WHQQShareTypeToQQ];*/+ (void)QQMakeShareNewsUrlStr:(NSString *)urlStr title:(NSString *)title description:(NSString *)description previewImageUrl:(NSString *)previewImageUrl shareType:(WHQQShareType)shareType {QQApiNewsObject *newsObj = [QQApiNewsObjectobjectWithURL:[NSURL URLWithString:urlStr]title:titledescription:descriptionpreviewImageURL:[NSURL URLWithString:previewImageUrl]];SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];[self QQMakeShare:req shareType:shareType];}/** 音乐分享 */+ (void)QQMakeShareAudioUrlStr:(NSString *)urlStr title:(NSString *)title description:(NSString *)description previewImageUrl:(NSString *)previewImageUrl shareType:(WHQQShareType)shareType {QQApiAudioObject *audioObj =[QQApiAudioObject objectWithURL:[NSURL URLWithString:urlStr]title:titledescription:descriptionpreviewImageURL:[NSURL URLWithString:previewImageUrl]];SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:audioObj];[self QQMakeShare:req shareType:shareType];}/** 视频分享 */+ (void)QQMakeShareVideoUrlStr:(NSString *)urlStr title:(NSString *)title description:(NSString *)description previewImageUrl:(NSString *)previewImageUrl flashURL:(NSString *)flashURL image:(UIImage *)image shareType:(WHQQShareType)shareType {NSData *imgData = UIImagePNGRepresentation(image);QQApiVideoObject *videoObj = [QQApiVideoObject objectWithURL:[NSURL URLWithString:urlStr]title:titledescription:descriptionpreviewImageData:imgData];[videoObj setFlashURL:[NSURL URLWithString:flashURL]];SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:videoObj];[self QQMakeShare:req shareType:shareType];}#pragma mark —————— 微信分享 ——————+ (void)WXMakeShareTitle:(NSString *)title description:(NSString *)description webpageUrl:(NSString *)webpageUrl image:(UIImage *)image type:(int)type {if ([WXApi isWXAppInstalled]) {SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];sendReq.bText = NO; //不使用文本信息sendReq.scene = type; //0 = 好友列表 1 = 朋友圈 2 = 收藏// 创建分享内容对象WXMediaMessage *urlMessage = [WXMediaMessage message];urlMessage.title = title; //分享标题urlMessage.description = description; //分享描述[urlMessage setThumbImage:image];// 创建多媒体对象WXWebpageObject *webObj = [WXWebpageObject object];webObj.webpageUrl = webpageUrl; //分享链接// 完成发送对象实例urlMessage.mediaObject = webObj;sendReq.message = urlMessage;// 发送分享信息[WXApi sendReq:sendReq];} else {// [WHView showMessage:@"未安装微信!" inView:nil];}}#pragma mark —————— 新浪微博分享 ——————+ (void)SinaWeiBoMakeShareText:(NSString *)text image:(UIImage *)image objectID:(NSString *)objectID title:(NSString *)title description:(NSString *)description thumbImage:(UIImage *)thumbImage webpageUrl:(NSString *)webpageUrl {WHAppDelegate *myDelegate = (WHAppDelegate *)[[UIApplication sharedApplication] delegate];WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];authRequest.redirectURI = @"/oauth2/default.html";authRequest.scope = @"all";WBMessageObject *message = [WBMessageObject message];message.text = text;if (image) {WBImageObject *imageObject = [WBImageObject object];imageObject.imageData = UIImagePNGRepresentation(image);message.imageObject = imageObject;} else {WBWebpageObject *webpage = [WBWebpageObject object];webpage.objectID = objectID;webpage.title = title;webpage.description = description;UIGraphicsBeginImageContext(CGSizeMake(thumbImage.size.width*0.5, thumbImage.size.height*0.5));[image drawInRect:CGRectMake(0, 0, thumbImage.size.width*0.5, thumbImage.size.height*0.5)];UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();NSData *resultThumbImageData = UIImageJPEGRepresentation(resultImage, 0.5);long thumbImageSize = (long)(resultThumbImageData.length / 1024);// DLog(@"resultThumbImageDataLength: %ld k", (long)(resultThumbImageData.length / 1024));if (thumbImageSize < 32) {webpage.thumbnailData = resultThumbImageData;}webpage.webpageUrl = webpageUrl;message.mediaObject = webpage;}WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:myDelegate.wbtoken];[WeiboSDK sendRequest:request];}@end

WHHomeController.m

#import "WHHomeController.h"#import "WHSharePlatformView.h"#import "WHThirdShareTool.h"@interface WHHomeController ()@end@implementation WHHomeController- (void)viewDidLoad {[super viewDidLoad];[self setUpUI];}- (void)setUpUI {WHSharePlatformView *sharView = [[WHSharePlatformView alloc] initWithImageArray:@[@"share_WXChat_icon", @"share_WXpengyou_icon", @"share_QQ_icon", @"share_Sina_icon"]];[self.view addSubview:sharView];[sharView mas_makeConstraints:^(MASConstraintMaker *make) {make.top.mas_equalTo(self.view.mas_bottom);make.left.right.mas_equalTo(self.view);make.height.mas_equalTo(kRealwidth(66));}];[self.view layoutIfNeeded];[UIView animateWithDuration:.5f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{[sharView mas_remakeConstraints:^(MASConstraintMaker *make) {make.top.mas_equalTo(self.view.mas_bottom).offset(-kRealwidth(100));make.left.right.mas_equalTo(self.view);make.height.mas_equalTo(kRealwidth(66));}];[self.view layoutIfNeeded];} completion:nil];kWeakSelf(self);sharView.clicked = ^(int index) {[weakself makeShareby:index];};}- (void)makeShareby:(int)index {DLog(@"index == %d", index);if (index == 2) { // QQ分享[self makeShareToQQ];}if (index == 0) {[self makeShareToWX:0];}if (index == 1) {[self makeShareToWX:1];}if (index == 3) {[self makeSinaShare];}}#pragma mark - —————— QQ分享 ——————- (void)makeShareToQQ {[WHThirdShareTool QQMakeShareTitle:@"测试标题" description:@"测试描述" image:IMAGE_NAMED(@"share_back_png")];}#pragma mark —————— 微信分享 ——————- (void)makeShareToWX:(int)type {[WHThirdShareTool WXMakeShareTitle:@"测试标题" description:@"测试描述" webpageUrl:@"" image:IMAGE_NAMED(@"share_back_png") type:type];}#pragma mark —————— 新浪微博分享 ——————- (void)makeSinaShare {[WHThirdShareTool SinaWeiBoMakeShareText:@"测试文本内容" image:nil objectID:@"hahahha" title:@"测试标题" description:@"测试描述" thumbImage:IMAGE_NAMED(@"share_back_png") webpageUrl:@""];}@end

github链接地址(一个包含QQ,微信,新浪微博原生平台集成以及支付宝微信支付的Demo)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。