APP微信商戶申請APPID步驟地址
微信支付 SDK與 Demo地址下載
-
把微信支付 SDK 拖到工程上
-
添加微信支付依賴庫
微信支付 SDK 文件的read_me.txt 有版本更新說明與注意問題彤守,能避免不必要Xcode的報錯
- ** 蘋果在iOS9 系統(tǒng)把 Http 協(xié)議升級為 Https協(xié)議,Https比 Http更為安全性跷坝,對 Http協(xié)議訪問做限制砖茸,所以需要在 Info.plust 文件添加 URL Schemes 白名單 **
-
** 添加成功后隘擎,會在Info.plist顯示這兩個 Key (LSApplicationQueriesSchemes 與 App Transport Security Settings)**
-
設置微信 APPID 為 URL Schemes
-
在AppDelegate.m 導入頭文件
#import "WXApi.h"
在AppDelegate.m 填寫你的微信APPID
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WXApi registerApp:@"填寫你的微信APPID" withDescription:@"weixinDemo"];
return YES;
}
- 這個方法會收到來自微信回應的處理結果
/*! @brief 發(fā)送一個sendReq后,收到微信的回應
*
* 收到一個來自微信的處理結果凉夯。調用一次sendReq后會收到onResp货葬。
* 可能收到的處理結果有SendMessageToWXResp、SendAuthResp等劲够。
* @param resp具體的回應內容震桶,是自動釋放的
*/
-(void)onResp:(BaseResp *)resp
{
if ([resp isKindOfClass:[PayResp class]])
{
//返回支付結果,實際支付結果需要取微信服務端查詢
NSString *strMsg = @"支付結果";
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付成功";
NSLog(@"支付成功-PaySuccess,resp.errCode = %d",resp.errCode);
break;
default:
strMsg = @"支付失敗";
NSLog(@"支付失敗-PaySuccess,resp.errCode = %d,resp.errStr = %@",resp. errCode,resp.errStr);
break;
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%d",resp.errCode ]message:resp.errStr preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:sure];
[self.window.rootViewController presentViewController:alertController animated:YES completion:^{
}];
}
}
- 讓 AppDelegate.m 遵守 WXApiDelegate 代理協(xié)議
#pragma mark - 跳轉到微信應用(第三方)
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
// 處理微信通過URL啟動App時傳遞的數(shù)據(jù)
return [WXApi handleOpenURL:url delegate:self];
}
- **在點擊響應事件的類導入微信支付的頭文件 "WXApi.h",最后在你點擊事件的方法中調用下面這個方法 **
+ (NSString *)jumpToBizPay {
if (![WXApi isWXAppInstalled]) {
NSLog(@"該設備沒有安裝微信");
return @"該設備沒有安裝微信";
}
if (![WXApi isWXAppSupportApi]) {
NSLog(@"該設備不支持微信");
return @"該設備不支持微信";
}
//============================================================
// V3&V4支付流程實現(xiàn)
// 注意:參數(shù)配置請查看服務器端Demo
// 更新時間:2015年11月20日
//============================================================
NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
//解析服務端返回json數(shù)據(jù)
NSError *error;
//加載一個NSURL對象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//將請求的url數(shù)據(jù)放到NSData對象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if ( response != nil) {
NSMutableDictionary *dict = NULL;
//IOS5自帶解析類NSJSONSerialization從response中解析出數(shù)據(jù)放到字典中
dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"url:%@",urlString);
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//調起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepayid"];
req.nonceStr = [dict objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
[WXApi sendReq:req];
//日志輸出
NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
return @"";
}else{
return [dict objectForKey:@"retmsg"];
}
}else{
return @"服務器返回錯誤征绎,未獲取到json對象";
}
}else{
return @"服務器返回錯誤";
}
}