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接入和配置都没有问题。
举报/反馈