微信SDK与 Demo
SDK
添加依赖库
版本更新说明与注意问题
Info.plist显示这两个 Key
设置微信 APPID 为 URL Schemes
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WXApi registerApp:@"填写你的微信APPID" withDescription:@"weixinDemo"];
return YES;
}
/*! @brief 发送一个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:^{
}];
}
}
#pragma mark - 跳转到微信应用(第三方)
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
// 处理微信通过URL启动App时传递的数据
return [WXApi handleOpenURL:url delegate:self];
}
+ (NSString *)jumpToBizPay {
if (![WXApi isWXAppInstalled]) {
NSLog(@"该设备没有安装微信");
return @"该设备没有安装微信";
}
if (![WXApi isWXAppSupportApi]) {
NSLog(@"该设备不支持微信");
return @"该设备不支持微信";
}
//============================================================
// 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"];
//调起微信支付
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 @"服务器返回错误";
}
}