iOS开发之微信支付
2015-06-01 13:31
597 查看
具体支付代码:
//调起微信支付
PayReq* req = [[[PayReq alloc] init]autorelease];
req.openID = APPI_ID;
req.partnerId = PARTNER_ID;
req.prepayId = prePayid;
req.nonceStr = nonce_str;
req.timeStamp = now;
req.package = package;
req.sign = sign;
[WXApi safeSendReq:req];
不得不吐槽,微信支付的SDK,这里连个代理都没有,所以,你想获取支付后的结果的返回信息,需要去AppDelegate里,添加如下代码,添加微信的代理:
(BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
然后,再实现这个方法:
-(void) onResp:(BaseResp*)resp
1
通过这个方法,你就可以获取微信支付后的信息了。但是如果这样,所有的支付代码都在AppDelegate里,不好,至少微信给的例子就是这样。我的方法是添加通知,代码如下:
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp response = (PayResp )resp;
// NSString *strTitle = [NSString stringWithFormat:@”支付结果”];
// NSString *strMsg = [NSString stringWithFormat:@”errcode:%d”, response.errCode];
//
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
// message:strMsg
// delegate:self
// cancelButtonTitle:@”OK”
// otherButtonTitles:nil, nil];
// [alert show];
}
然后,在支付那个VC,添加通知
[super viewWillAppear:animated];
if([WXApi isWXAppInstalled]) // 判断 用户是否安装微信
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:nil];//监听一个通知
}
}
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter
//调起微信支付
PayReq* req = [[[PayReq alloc] init]autorelease];
req.openID = APPI_ID;
req.partnerId = PARTNER_ID;
req.prepayId = prePayid;
req.nonceStr = nonce_str;
req.timeStamp = now;
req.package = package;
req.sign = sign;
[WXApi safeSendReq:req];
不得不吐槽,微信支付的SDK,这里连个代理都没有,所以,你想获取支付后的结果的返回信息,需要去AppDelegate里,添加如下代码,添加微信的代理:
(BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
然后,再实现这个方法:
-(void) onResp:(BaseResp*)resp
1
通过这个方法,你就可以获取微信支付后的信息了。但是如果这样,所有的支付代码都在AppDelegate里,不好,至少微信给的例子就是这样。我的方法是添加通知,代码如下:
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
PayResp response = (PayResp )resp;
// NSString *strTitle = [NSString stringWithFormat:@”支付结果”];
// NSString *strMsg = [NSString stringWithFormat:@”errcode:%d”, response.errCode];
//
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
// message:strMsg
// delegate:self
// cancelButtonTitle:@”OK”
// otherButtonTitles:nil, nil];
// [alert show];
switch (response.errCode) { case WXSuccess: { NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"]; [[NSNotificationCenter defaultCenter] postNotification:notification]; break; } default: { NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"]; [[NSNotificationCenter defaultCenter] postNotification:notification]; break; } } }
}
然后,在支付那个VC,添加通知
pragma mark - tabBar隐藏
(void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];
if([WXApi isWXAppInstalled]) // 判断 用户是否安装微信
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:nil];//监听一个通知
}
}
pragma mark - tabbar还原
(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter
相关文章推荐
- 个体户如何开通微信公众平台服务号
- 基于第三方微信授权登录的iOS代码分析
- 微信支付
- 微信开发 企业号、服务号、订阅号 区别
- 一个下载图片的小程序
- 接入微信SDK64位包报错
- 用c#开发微信 (9) 微渠道 - 推广渠道管理系统 4 部署测试 (最终效果图)
- 如何获取微信用户的名字等信息
- iOS支付宝密码框Demo/微信密码框/邀请码/验证码
- Java小程序:打印空心菱形
- 微信开放平台JS SDK接入sha1算法
- JAVA微信公众号开发入门
- QQ微信的备份
- 微信|公众平台开发者文档
- 微信支付开发(4) 动态链接Native支付
- JS-SDK微信支付开发攻略
- 微信公众号辅助工具汇总 推广加粉统统有
- [注]微信公众号的运营推广总结方案(持续更新)
- 基于Android硬件设备跟微信服务器通信的项目心得,以UDP Byte通信为例,建立无向连接,0530手札
- 想要突破,你需要做些什么?