1.下载demo引入文件和第三方库,如下图:(只需引入图片中的)
2.AppDelegate.m中如下:(别忘了引头文件)
#import <AlipaySDK/AlipaySDK.h>
// 真机回调方法
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
// 如果极简 SDK 不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给 SDK
if ([url.host isEqualToString:@"safepay"]) {
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 通知中心监听:
}];
}
if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回 authCode
[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
#import <AlipaySDK/AlipaySDK.h>
- (void)zhiFu
{
// 当手机没有安装支付宝时 调用网页支付(如iOS模拟器)
若已安装支付宝,会优先走支付宝第三方
UIWindow *win = [[[UIApplicationsharedApplication]windows]objectAtIndex:0];
win.hidden =NO;
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
info - URL types - Url Scheme
NSString *appScheme =@"alisdkdemo";
_orderString 为后台返回的字符串(根据需支付的商品,价格,安全密钥已拼接好的)
*****可见有一个好的后台是多么的重要!!!!!!!!!!!!!!!!!!!********
[[AlipaySDKdefaultService]payOrder:_orderStringfromScheme:appSchemecallback:^(NSDictionary *resultDic) {
NSString *str =@"9000";
if ([strisEqualToString:[resultDicobjectForKey:@"resultStatus"]]) {
[selfshowHudWithCustomView:NSLocalizedString(@"hud.successMoney",@"支付成功~")];
}else {
[selfshowHudWithCustomView:NSLocalizedString(@"hud.faileMoney",@"支付失败~")];
}
//支付完成 隐藏win 否则页面无法托动
win.hidden =YES;
}];
// 通知中收众appdeleta传值过来@"Alipay"
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipay:) name:@"Alipay" object:nil];
}
// 调用方法 alipay: 判断支付成功与否
- (void)alipay:(NSNotification *)noti{
NSString *str = @"9000";
if ([str isEqualToString:[noti.userInfo objectForKey:@"resultStatus"]]) {
[myToast showWithText:@"支付成功"];
} else {
[myToast showWithText:@"支付失败~"];
}
}
4. 当走支付宝第三方时,回到支付页面,win没有隐藏,需在AppDelegate中隐藏,如下:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
UIWindow *win = [[[UIApplicationsharedApplication]windows]objectAtIndex:0];
win.hidden =YES;
}