WXApiManager.h
#import <Foundation/Foundation.h>
#import "WXApi.h"
@protocol WXApiManagerDelegate <NSObject>
@required
@optional
-(void)wxPayResponse:(PayResp *)resp;
-(void)wxSendAuthResponse:(SendAuthResp *)resp;
@end
@interface WXApiManager : NSObject<WXApiDelegate>
@property(nonatomic, weak) id <WXApiManagerDelegate> delegate;
+ (instancetype)sharedManager;
@end
WXApiManager.m
#import "WXApiManager.h"
@implementation WXApiManager
#pragma mark - 单例
+(instancetype)sharedManager {
static dispatch_once_t onceToken;
static WXApiManager *instance;
dispatch_once(&onceToken, ^{
instance = [[WXApiManager alloc] init];
});
return instance;
}
#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp
{
if([resp isKindOfClass:[PayResp class]]){
PayResp *payResp = (PayResp *)resp;
[self.delegate wxPayResponse:payResp];
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg;
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付结果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *sendAuthResp = (SendAuthResp *)resp;
[self.delegate wxSendAuthResponse:sendAuthResp];
}
}
@end
在AppDelegate.m中实现代理<WXApiDelegate>
#pragma mark WECHAT
//重写handleOpenURL和openURL方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
[WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
return YES;
}
在需要接收微信回调的Congroller中实现所需代理<WXApiManagerDelegate>
以登录为例
-(void)wxSendAuthResponse:(SendAuthResp *)resp {
if (resp.errCode == 0) {
//获取授权成功
} else if (resp.errCode == -4) {
//用户拒绝授权
} else if (resp.errCode == -2) {
//用户取消授权
}
}