iOS微信支付和Universal Link|撸码云内测分发平台
搜索撸码云内测分发平台(https://www.lumayun.com/#/ )了解更多。
撸码云内测分发平台为全球开发者提供专业和稳定的应用分发服务,我们愿与开发者一起学习、交流与成长,共同打造良好的开发者生态圈!
1.https服务器。
2.在微信开放平台创建应用,申请支付功能,获取App ID。
打开Xcode,通过Pod安装微信SDK:
pod 'WechatOpenSDK'
pod install
2.1:配置 URL Types
2.2:info.plist 添加白名单,weixin、weixinULAPI
2.3:打开苹果开发者网站(http://developer.apple.com),勾选 Associated Domains。
2.4:Xcode中,在 Capability 中添加 Associated Domains,Domains:填写【applinks:域名】,如
applinks:www.xxxx.com
第三步:配置 Universal Link(通用链接)
创建空白文本,命名为apple-app-site-association(无后缀),
内容如:
说明:appID填写格式: "TeamID.Bundle ID",“苹果账号团队ID.BundleID”
文件交给后台程序员,上传至根目录或 .well-known 目录下。形成链接后,可下载即成功。
https://cloud.xxxxxx.com/.well-known/apple-app-site-association
Universal Links 配置举
假设域名为:https://www.mydomain.com
1.微信开放平台设置为:
https://www.mydomain.com/app/
2.apple-app-site-association 配置文件:
{
"appID": "2EX886S24H.com.ZXKJ.Maintenance",
"paths": ["/app/*"]
}
3.在 Xcode -> Targets -> Signing & Capability -> Associated Domains,Domains 填写:
applinks:www.mydomain.com
4.Xcode中代码配置为:
[WXApi registerApp:@"App Id" universalLink:@"https://www.mydomain.com/app/"];
AppDelegate.m 中导入头文件
#import "WXApi.h"
: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中加入代码:
//微信调试代码 /* [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) { NSLog(@"WeChatSDK: %@", log); }]; */ /* 微信 */ BOOL success = [WXApi registerApp:WeChat_AppKey universalLink:WeChat_universalLink]; if (success) { NSLog(@"微信 registerApp success!"); } //调用自检函数,调试代码 /* [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) { NSLog(@"WeChatSDK step = %@, ?, %@ result=%@", @(step), result.success, result.errorInfo, result.suggestion, result); }]; */
不能少!否则无法调起微信
#pragma mark - 设置系统回调// NOTE: 9.0以后使用新API接口 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options { BOOL result_wechat = [WXTools handleOpenURL:url]; if (result_wechat) { return result_wechat; } return NO; }// Universal Link(通用链接) - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler { BOOL result_wechat = [WXTools handleOpenUniversalLink:userActivity]; if (result_wechat) { return result_wechat; } return NO;}
微信支付工具类:
#import <Foundation/Foundation.h>#import "WXApi.h"#define WXTools [WXPayTools sharedInstance]#define WeChat_AppKey @""@interface WXPayTools : NSObject+ (WXPayTools *)sharedInstance;// 回调- (BOOL)handleOpenURL:(NSURL *)url;// 回调 Universal Link(通用链接)- (BOOL)handleOpenUniversalLink:(NSUserActivity *)userActivity;/** wechat支付 */- (void)doWXPay:(NSDictionary *)reqDict paySuccess:(void (^)(void))paySuccessBlock payFailed:(void (^)(void))payFailedBlock;@end
WXPayTool.m
#import "WXPayTools.h"@interface WXPayTools ()<WXApiDelegate>@property (nonatomic, copy) void (^authSuccessBlock)(NSString *reqCode);@property (nonatomic, copy) void (^paySuccessBlock)(void);@property (nonatomic, copy) void (^payFailedBlock)(void);@end@implementation WXPayTools+ (WXPayTools *)sharedInstance { static WXPayTools *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [WXPayTools new]; }); return instance;}// 回调- (BOOL)handleOpenURL:(NSURL *)url { if ([[url absoluteString] rangeOfString:[NSString stringWithFormat:@"%@://pay/", WeChat_AppKey]].location == 0) { return [WXApi handleOpenURL:url delegate:self]; } return NO;}// 回调 Universal Link(通用链接)- (BOOL)handleOpenUniversalLink:(NSUserActivity *)userActivity { return [WXApi handleOpenUniversalLink:userActivity delegate:self];}/** wechat支付 */- (void)doWXPay:(NSDictionary *)dataDict paySuccess:(void (^)(void))paySuccessBlock payFailed:(void (^)(void))payFailedBlock; { self.paySuccessBlock = paySuccessBlock; self.payFailedBlock = payFailedBlock; if ([dataDict isKindOfClass:[NSDictionary class]]) { PayReq *request = [[PayReq alloc] init]; request.partnerId = dataDict[@"partnerId"]; request.prepayId = dataDict[@"prepayId"]; request.package = @"Sign=WXPay"; request.nonceStr = dataDict[@"nonceStr"]; request.timeStamp = [dataDict[@"timeStamp"] intValue]; request.sign = dataDict[@"sign"]; [WXApi sendReq:request completion:^(BOOL success) { NSLog(@"success - %d", success); }]; }}#pragma mark - Delegate回调方法- (void)onResp:(id)resp { if ([resp isKindOfClass:[PayResp class]]) { PayResp *response = (PayResp *)resp; switch(response.errCode) { case WXSuccess: { NSLog(@"支付成功"); if (self.paySuccessBlock) { self.paySuccessBlock(); } } break; default: { NSLog(@"支付失败"); if (self.payFailedBlock) { self.payFailedBlock(); } } break; } } } @end
支付页面调用工具类:
- (void)wxPay { NSDictionary *data = @{ @"appId" : @"wx862e7a21b8df70b7", @"nonceStr" : @"li8tjxvshpxcmryefu8b87xftdf4ljnn", @"partnerId": @"1611925030", @"prepayId" : @"wx0913444284359740c9002e4995965f0000", @"sign" : @"03359D5970F910EE5609E7BB4CA52DA8", @"timeStamp": @"1631166282" }; [WXTools doWXPay:data paySuccess:^{ NSLog(@"success!"); } payFailed:^{ NSLog(@"支付失败"); }];}
全部配置完后,如果微信调用不起来,可使用SDK 自检函数排查问题WeChatSDK step = 0, 1, check passed,WeChatSDK step = 1, 1, check passed,WeChatSDK step = 2, 1, check passed,WeChatSDK step = 3, 1, check passed,WeChatSDK step = 4, 1, check passed,WeChatSDK step = 5, 1, Universal Link check passed. The application is launched by WeChat via Universal Link,WeChatSDK step = 6, 1, All Check Passed!,
step 6 走完,说明sdk接入和配置都没有问题。
举报/反馈
上一篇:假如世界各大媒体都来报道鸿门宴
下一篇:《江湖儿女》:一半是江湖,一半是儿女
最近更新影视资讯
- 韵府群玉
- 老年临终关怀护理集锦9篇
- 如何评价剧场版动画《和谐(harmony/ハーモニー)》原作:伊藤计划 ?
- 智人战胜尼人的决定性因素 是神灵崇拜与艺术品 在3万7千年前智人击败了远比自己强
- 沈阳参考消息(2017年1月11日)
- 密集架区密集架书库图书馆负一楼期刊阅览区中外文期刊图书馆一楼图书借阅区(A-H
- 费维光:脾胃病17方
- 土耳其身为伊斯兰国家,为什么允许“风俗产业”合法化?
- 高中教师教学反思
- 三观尽毁!90后公务员出轨50岁女上司,聊天言语暧昧,妻子怒举报
- 22应用心理学考研347 首师360有调剂院校吗?
- 铃木凉美女士,你仍期待同时收获怜爱与尊敬吗?
- 团建别墅 | 确认过眼神,是能疯一起的人!Boss,今年年会我们泡私家温
- 《归来》观后感
- 翻译伦理的重要性和译者荣辱观建设研究
- 高二语文期末考试测试题及答案
- 国医大师名单!在北京看中医该找谁,这下全知道!
- 这些年爱过的同人文(BG)
- 荷兰深陷风俗业?日本都要甘拜下风,为何能稳坐世界顶尖位置!
- 戴安娜25年前私密录像首次解密:自述性生活,全英国都被炸懵逼了
- 原创上官婉儿为什么必须死,她做的这件事太无耻,李隆基忍无可忍
- 「医药速读社」Paxlovid临床失败 礼来斥巨资引进Kv1.3抑制剂
- 她是韩国性感女神,靠出演“三级片”走红,今41岁韵味不减当年!
- 电影市场有望点燃 好莱坞大片排队上映
- 评荐《传染病(Contagion)》