1. 在AppDelegate.h中
(1) 導(dǎo)入微信類 #import @"WXApi.h".
(2) 遵守微信代理方法
2. 在APPDelegate.m中
(1) 注冊微信
#pragma mark 注冊微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//注冊 微信
/**
參數(shù)1 : 微信Appid
參數(shù)2 : 對項目的描述信息(用項目名稱)
*/
[WXApi registerApp:@"微信Appid" withDescription:@"云宴"];
return YES;
}
(2) 跳轉(zhuǎn)方法,并設(shè)置代理
#pragma mark 跳轉(zhuǎn)處理
//被廢棄的方法. 但是在低版本中會用到.建議寫上
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [WXApi handleOpenURL:url delegate:self];
}
//被廢棄的方法. 但是在低版本中會用到.建議寫上
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WXApi handleOpenURL:url delegate:self];
}
//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
return [WXApi handleOpenURL:url delegate:self];
}
(3) 微信回調(diào)方法 (注意: 不要寫成Req方法了)
#pragma mark 微信回調(diào)方法
- (void)onResp:(BaseResp *)resp
{
NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
NSLog(@"strMsg: %@",strMsg);
NSString * errStr = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
NSLog(@"errStr: %@",errStr);
NSString * strTitle;
//判斷是微信消息的回調(diào) --> 是支付回調(diào)回來的還是消息回調(diào)回來的.
if ([resp isKindOfClass:[SendMessageToWXResp class]])
{
strTitle = [NSString stringWithFormat:@"發(fā)送媒體消息的結(jié)果"];
}
NSString * wxPayResult;
//判斷是否是微信支付回調(diào) (注意是PayResp 而不是PayReq)
if ([resp isKindOfClass:[PayResp class]])
{
//支付返回的結(jié)果, 實際支付結(jié)果需要去微信服務(wù)器端查詢
strTitle = [NSString stringWithFormat:@"支付結(jié)果"];
switch (resp.errCode)
{
case WXSuccess:
{
strMsg = @"支付結(jié)果:";
NSLog(@"支付成功: %d",resp.errCode);
wxPayResult = @"success";
break;
}
case WXErrCodeUserCancel:
{
strMsg = @"用戶取消了支付";
NSLog(@"用戶取消支付: %d",resp.errCode);
wxPayResult = @"cancel";
break;
}
default:
{
strMsg = [NSString stringWithFormat:@"支付失敗! code: %d errorStr: %@",resp.errCode,resp.errStr];
NSLog(@":支付失敗: code: %d str: %@",resp.errCode,resp.errStr);
wxPayResult = @"faile";
break;
}
}
//發(fā)出通知 從微信回調(diào)回來之后,發(fā)一個通知,讓請求支付的頁面接收消息,并且展示出來,或者進行一些自定義的展示或者跳轉(zhuǎn)
NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}
3. 在ViewController.h (進行支付請求的類)
暫時沒有任何操作
4. 在ViewController.m中(進行支付的請求的類)
(1) 導(dǎo)入微信庫 #import @"WXApi.h"
(2) 監(jiān)聽APPDelegate.m中發(fā)送的通知
#pragma mark 監(jiān)聽通知
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
//檢測是否裝了微信軟件
if ([WXApi isWXAppInstalled])
{
//監(jiān)聽通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
}
}
#pragma mark - 事件
- (void)getOrderPayResult:(NSNotification *)notification
{
NSLog(@"userInfo: %@",notification.userInfo);
if ([notification.object isEqualToString:@"success"])
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertView show];
}
else{
[self alert:@"提示" msg:@"支付失敗"];
}
}
//客戶端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
(3) 移除通知
#pragma mark 移除通知
- (void)dealloc
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}