集成支付宝支付,微信支付建议使用下面两个,方便快捷.SDK建议手动导入,别使用Cocoapods.
http://winann.blog.51cto.com/4424329/1601654
1.导入SDK:
上官网下载SDK,然后拷入工程即可
2.导入相应依赖库,选择"target"->"Link Binary With Libraries".
3.在AppDelegate.m中注册微信,如下所示:
1 2 3 4 5 6 7 8 9 10 11 | #import "AppDelegate.h" #import "WXApi.h" @interface AppDelegate () @end @implementation AppDelegate //测试时.真机测试 - ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions { // Override point for customization after application launch. [WXApi registerApp:@ "wxb4ba3c02aa476ea1" withDescription:@ "微信接口测试" ]; return YES ; } |
4.在点击controller中添加如下代码.
在点击按钮方法中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | //============================================================ // V3&V4支付流程实现 // 注意:参数配置请查看服务器端Demo // 更新时间:2015年11月20日 //============================================================ //微信测试接口,集成时换位自己的后台接口. NSString *urlString = @ "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios" ; //解析服务端返回json数据 NSError *error; //加载一个NSURL对象 NSURLRequest *request = [ NSURLRequest requestWithURL:[ NSURL URLWithString:urlString]]; //将请求的url数据放到NSData对象中 NSData *response = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ]; if ( response != nil ) { NSMutableDictionary *dict = NULL ; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 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" ]; NSLog (@ "%@" ,retcode); NSLog (@ "%@" ,stamp); //NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:dict[@""], nil] //调起微信支付 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" ]; // @"47B7A45B8DB4E7BB88B8F9F4E268CB5D"; //[dict objectForKey:@"sign"]; BOOL result = [WXApi sendReq:req]; NSLog (@ "-=-=-=-=- %d" , result); //日志输出 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 @ "服务器返回错误" ; } |
5.注册协议WXApiDelegate,需实现onResp方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //微信回调 -( void ) onResp:(BaseResp*)resp { NSString *strMsg = [ NSString stringWithFormat:@ "errcode:%d" , resp.errCode]; NSString *strTitle; if ([resp isKindOfClass:[SendMessageToWXResp class ]]) { strTitle = [ NSString stringWithFormat:@ "发送媒体消息结果" ]; } if ([resp isKindOfClass:[PayResp class ]]){ //支付返回结果,实际支付结果需要去微信服务器端查询 strTitle = [ NSString stringWithFormat:@ "支付结果" ]; 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 ; } } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate: self cancelButtonTitle:@ "OK" otherButtonTitles: nil , nil ]; [alert show]; } |
另:因为iOS9的原因,需要配置下项目,如:
https:
白名单:
更多白名单http://www.cnblogs.com/shaoting/p/5148323.html
URL types:
下面是朋友做的demo实现,本人亲测可用,需真机测试:
分类:
iOS-OC开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探