时间:2022-03-24 11:36:05 | 栏目:iOS代码 | 点击:次
{ memo=""; result="partner=\"311811\"&seller_id=\"nse@gmail.com\"&out_trade_no=\"S005372\"&subject=\"\U522b\U5885\U8ba2\U5355\"&body=\"\U5885\"&total_fee=\"0.1\"¬ify_url=\"http://baidu.com\"&service=\"mobile.secy.pay\"&payment_type=\"1\"&_input_charset=\"UTF-8\"&it_b_pay=\"30m\"&success=\"true\"&sign_type=\"RSA\"&sign=\"dMiqkRZvnpTnJp9zbwGNAL3Bu43RMocNwOAsRYFnueZX8Uwy81zXHLg=\""; resultStatus=9000; }
如上所示,为支付宝的返回数据结果。可能有的同学就懵了,这个result是一个字符串怎么把它分解成字典用那。
我封装了一个方法可以用
/** * 支付宝返回字段解析 * * @param AllString 字段 * @param FirstSeparateString 第一个分离字段的词 * @param SecondSeparateString 第二个分离字段的词 * * @return 返回字典 */ +(NSDictionary *)VEComponentsStringToDic:(NSString*)AllString withSeparateString:(NSString *)FirstSeparateString AndSeparateString:(NSString *)SecondSeparateString; +(NSMutableDictionary *)VEComponentsStringToDic:(NSString*)AllString withSeparateString:(NSString *)FirstSeparateString AndSeparateString:(NSString*)SecondSeparateString{ NSMutableDictionary *dic=[NSMutableDictionary dictionary]; NSArray *FirstArr=[AllString componentsSeparatedByString:FirstSeparateString]; for (int i=0; i<FirstArr.count; i++) { NSString *Firststr=FirstArr[i]; NSArray *SecondArr=[Firststr componentsSeparatedByString:SecondSeparateString]; [dic setObject:SecondArr[1] forKey:SecondArr[0]]; } return dic; }
在AppDelegate用的时候代码如下
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSInteger orderState=[resultDic[@"resultStatus"]integerValue]; if (orderState==9000) { NSString *allString=resultDic[@"result"]; NSString * FirstSeparateString=@"\"&"; NSString * SecondSeparateString=@"=\""; NSMutableDictionary *dic=[CommonToolsVEComponentsStringToDic:allString withSeparateString:FirstSeparateString AndSeparateString:SecondSeparateString]; NSLog(@"ali=%@",dic); if ([dic[@"success"]isEqualToString:@"true"]) { [[NSNotificationCenter defaultCenter] postNotificationName:@"AliPaySucceed" object:nil userInfo:dic]; } }else{ NSString *returnStr; switch (orderState) { case 8000: returnStr=@"订单正在处理中"; break; case 4000: returnStr=@"订单支付失败"; break; case 6001: returnStr=@"订单取消"; break; case 6002: returnStr=@"网络连接出错"; break; default: break; } [HUDTooles showTextHUD:returnStr]; [HUDTooles removeHUD:4]; } }]; }