天天看点

iOS开发之微信支付 pragma mark - tabBar隐藏 pragma mark - tabbar还原

具体支付代码: 

//调起微信支付 

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 

通过这个方法,你就可以获取微信支付后的信息了。但是如果这样,所有的支付代码都在appdelegate里,不好,至少微信给的例子就是这样。我的方法是添加通知,代码如下:

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,添加通知

(void)viewwillappear:(bool)animated { 

[super viewwillappear:animated]; 

if([wxapi iswxappinstalled]) // 判断 用户是否安装微信 

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(getorderpayresult:) name:order_pay_notification object:nil];//监听一个通知 

(void)viewwilldisappear:(bool)animated 

[super viewwilldisappear:animated]; 

[[nsnotificationcenter defaultcenter