欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发之实现微信支付,支付宝支付的开发教程

程序员文章站 2022-05-12 16:57:10
首先做如下设置 然后在appdelegate.m 中 #pragma 支付宝支付 - (bool)application:(uiapplication *)application...

首先做如下设置

iOS开发之实现微信支付,支付宝支付的开发教程

然后在appdelegate.m 中

#pragma 支付宝支付
- (bool)application:(uiapplication *)application
            openurl:(nsurl *)url
  sourceapplication:(nsstring *)sourceapplication
         annotation:(id)annotation {

    if ([url.host isequaltostring:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[alipaysdk defaultservice] processorderwithpaymentresult:url standbycallback:^(nsdictionary *resultdic) {
            nslog(@"result = %@",resultdic);
        }];
        return yes;
    }else{
        //微信
        return [wxapi handleopenurl:url delegate:self];
    }

}

// note: 9.0以后使用新api接口
- (bool)application:(uiapplication *)app openurl:(nsurl *)url options:(nsdictionary *)options
{
    // safepay 是支付宝, pay 是微信
    if ([url.host isequaltostring:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[alipaysdk defaultservice] processorderwithpaymentresult:url standbycallback:^(nsdictionary *resultdic) {
            nslog(@"result = %@",resultdic);
        }];
        return yes;
    }else{
        //跳回到app
        return [wxapi handleopenurl:url delegate:self];
    }
}

//以下微信独有
#pragma 微信
- (bool)application:(uiapplication *)application handleopenurl:(nsurl *)url
{
    return [wxapi handleopenurl:url delegate:self];
}


#pragma wxapidelegate
-(void) onreq:(basereq*)req{
    nslog(@"-========请求");
}

-(void) onresp:(baseresp*)resp{
    //支付结果回调
    if ([resp iskindofclass:[payresp class]]){
        payresp*response=(payresp*)resp;
        switch(response.errcode){
            case wxsuccess:
                //服务器端查询支付通知或查询api返回的结果再提示成功
                nslog(@"支付成功");
                [self tohomevc];
                break;
            default:
                nslog(@"支付失败,retcode=%d",resp.errcode);
                break;
        }
    }
}

//跳转到首页(自己定义的支付后跳转到指定页面)
- (void)tohomevc{
    uistoryboard *story = [uistoryboard storyboardwithname:@"main" bundle:[nsbundle mainbundle]];
    tabbarcontroller *tabvc = [story instantiateviewcontrollerwithidentifier:@"tabvc"];
    self.window.rootviewcontroller = tabvc;
}