编译之后会报错
如果拉进去还是报一个错,试多几次,或者重启Xcode ,看见报30个错就成功了..........
这30个错误是因为没有导入支付宝依赖的库
方便大家copy
libc++.tbd
libz.tbd
Foundation.framework
UIKit.framework
CoreGraphics.framework
CoreText.framework
QuartzCore.framework
CoreTelephony.framework
CFNetwork.framework
CoreMotion.framework
SystemConfiguration.framework
在info里面的URL Types的配置,配置了才能让支付宝跳转回到你的APP,代码里面也要设置
下面是我的代码
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 200, 44)];
[button1 setTitle:@"0.01" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor redColor];
[button1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(20, 120, 200, 44)];
[button2 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"0.02" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:button2];
}
- (void)click1
{
[self pushAliPay:@"0.01"];
}
- (void)click2
{
[self pushAliPay:@"0.02"];
}
- (void)pushAliPay:(NSString *)str_price
{
/*============================================================================*/
/*=======================需要填写商户app申请的===================================*/
/*============================================================================*/
NSString *partner = @"";
NSString *seller = @"";
NSString *privateKey = @"";
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
//partner和seller获取失败,提示
if ([partner length] == 0 ||
[seller length] == 0 ||
[privateKey length] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"缺少partner或者seller或者私钥。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
/*
*生成订单信息及签名
*/
//将商品信息赋予AlixPayOrder的成员变量
Order *order = [[Order alloc] init];
order.partner = partner;
order.sellerID = seller;
order.outTradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
order.subject = @"商品标题"; //商品标题
order.body = @"我是商品描述"; //商品描述
order.totalFee = [NSString stringWithFormat:@"%.2f",[str_price doubleValue]]; //商品价格
order.notifyURL = @"http://www.xxx.com"; //回调URL.问后台要
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showURL = @"m.alipay.com";
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString *appScheme = @"alisdkdemo";//这里设置支付宝回调
//将商品信息拼接成字符串
NSString *orderSpec = [order description];
NSLog(@"orderSpec = %@",orderSpec);
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderSpec];
//将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString = nil;
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
}];
}
}
pragma mark -
pragma mark ==============产生随机订单号==============
- (NSString *)generateTradeNO
{
static int kNumber = 15;
NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
srand((unsigned)time(0));
for (int i = 0; i < kNumber; i++)
{
unsigned index = rand() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
[resultStr appendString:oneStr];
}
return resultStr;
}