iOS 开发微信支付

首先我们到微信开放平台,下载相应的SDK。微信的官方文档感觉说的很简单,没有支付宝那么详细,在这里说下集成SDK到我们的工程中。

下载好demol后(最新版本SDKSample_v2.0.2_V3pay),看到有个SDKExport的文件;

这里写图片描述

你可以直接将这个文件夹添加到你的工程中,或者你自己新建一个文件夹,将里面那三个文件粘贴到你新建的文件夹中,并添加到你的工程中;

接下来就是添加相应地库文件;

这里写图片描述

我们看到demol中有个WXApiManager.h和WXApiManager.m文件,因为他里面会用到,所以这里我们直接将demol中的这两个文件也拖进我们工程中;

这里写图片描述

库文件添加好之后,就是实现我们的微信支付功能;

点击支付的button,完成支付;

引入头文件:
#import "ViewController.h"
#import "WXApiObject.h"
#import "WXApi.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =CGRectMake(100, 100, 100, 50);
    [btn setTitle:@"点击支付" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor magentaColor];
    [btn addTarget:self action:@selector(weixinPay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}
//在这里我们要判断用户是否安装微信,如果未安装是不能够支付,因为微信不支持网页支付,不像支付宝那样,如果用户未安装支付宝,它会自动跳转网页支付,如果安装,则会跳转到支付宝客户端进行支付;微信则不可以,所以要判断下;
- (void)weixinPay {
//判断用户是否安装微信
  if ([WXApi isWXAppInstalled]) {
         NSString *res = [self doClickZhiFu];
         if( ![@"" isEqual:res] ){
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"支付失败" message:res delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alter show];
         }
      }els
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#import "RespForWeChatViewController.h" #import "WXApiManager.h" #import "WXApiResponseHandler.h" #import "Constant.h" @implementation RespForWeChatViewController #pragma mark - View Lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self setupHeadView]; [self setupLinesView]; [self setupFootView]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; } } - (void)dealloc { [super dealloc]; } #pragma mark - User Actions - (void)sendTextContent { [WXApiResponseHandler respText:kTextMessage]; [self dismissModalViewControllerAnimated:YES]; } - (void)sendImageContent { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res1" ofType:@"jpg"]; NSData *imageData = [NSData dataWithContentsOfFile:filePath]; UIImage *thumbImage = [UIImage imageNamed:@"res1thumb.png"]; [WXApiResponseHandler respImageData:imageData MessageExt:nil Action:nil ThumbImage:thumbImage]; [self dismissModalViewControllerAnimated:YES]; } - (void)sendLinkContent { UIImage *thumbImage = [UIImage imageNamed:@"res2.png"]; [WXApiResponseHandler respLinkURL:kLinkURL Title:kLinkTitle Description:kLinkDescription ThumbImage:thumbImage]; [self dismissModalViewControllerAnimated:YES]; } - (void)sendMusicContent { UIImage *thumbImage = [UIImage imageNamed:@"res3.jpg"]; [WXApiResponseHandler respMusicURL:kMusicURL dataURL:kMusicDataURL Title:kMusicTitle Description:kMusicDescription ThumbImage:thumbImage]; [self dismissModalViewControllerAnimated:YES]; } - (void)sendVideoContent { UIImage *thumbImage = [UIImage imageNamed:@"res4.jpg"]; [WXApiResponseHandler respVideoURL:kVideoURL Title:kVideoTitle Description:kVideoDescription ThumbImage:thumbImage]; [self dismissModalViewControllerAnimated:YES]; } - (void)sendAppContent { Byte* pBuffer = (Byte *)malloc(BUFFER_SIZE); memset(pBuffer, 0, BUFFER_SIZE); NSData* data = [NSData dataWithBytes:pBuffer length:BUFFER_SIZE]; free(pBuffer); UIImage *thumbImage = [UIImage imageNamed:@"res2.jpg"]; [WXApiResponseHandler respAppContentData:data ExtInfo:kAppContentExInfo ExtURL:kAppContnetExURL Title:kAPPContentTitle Description:kAPPContentDescription MessageExt:kAppMessageExt MessageAction:kAppMessageAction ThumbImage:thumbImage]; } - (void)sendNonGifContent { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5" ofType:@"jpg"]; NSData *emoticonData = [NSData dataWithContentsOfFile:filePath]; UIImage *thumbImage = [UIImage imageNamed:@"res5thumb.png"]; [WXApiResponseHandler respEmotionData:emoticonData ThumbImage:thumbImage]; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值