iOS中京东支付

作者: 哈哈大p孩 | 来源:发表于2016-06-20 14:40 被阅读879次

    有的童鞋没弄过京东支付跑来问我,我们项目里有用到京东支付,于是自己写了个demo,代码贴在下面,希望能帮助到不会的童鞋。
    1.新建一个继承Viewcontroller的JDViewcontroller

    2. 在JDViewcontroller.h文件中,定义一个url属性,用来传url(废话)
    @property (nonatomic, copy) NSString *urlStr;
    

    3.在JDViewcontroller.m文件中,导入<JavaScriptCore/JavaScriptCore.h>框架,并定义一个成员变量webView

    {
        UIWebView *_webView;
    }
    
    4.主要代码
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.title = @"京东支付";
        self.view.backgroundColor = [UIColor whiteColor];
        [self initSubView];
    }
    
    - (void)initSubView {
    //创建webView
        _webView = [UIWebView new];
        _webView.scrollView.bounces = NO;
        _webView.delegate = self;
        [self.view addSubview:_webView];
        _webView.frame = self.view.frame;
        
        NSURL *url = [NSURL URLWithString:[_urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        
    }
    
    //delegate
    - (void)webViewDidStartLoad:(UIWebView *)webView {
    
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        NSLog(@"加载中,,");
        NSString *currentURL = webView.request.URL.absoluteString;
        NSString *theTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        NSLog(@"%@==", theTitle);
        
        NSRange range = [theTitle rangeOfString:@"支付的结果"];
        
        if (range.length > 0) {
            _webView = [UIWebView new];
            NSURL *url = [NSURL URLWithString:[currentURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            [_webView loadRequest:request];
            //固定格式,js与iOS交互
            JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
            context[@"jakilllog"] = ^() {
                NSString *type;
                NSLog(@"begin +++++++++++++");
                NSArray *args = [JSContext currentArguments];
                
                for (JSValue *jsVal in args) {
                    NSLog(@"%@", jsVal);
                    type = [NSString stringWithFormat:@"%@",jsVal];
                }
                
                JSValue *this = [JSContext currentThis];
                NSLog(@"this : %@", this);
                NSLog(@" end --------------");
                
                if ([type isEqualToString:@"success"]) {
                    NSLog(@"success");
                }
            };
        }
    }
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        return YES;
    }
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    }
    

    在主Viewcontroller中导入JDViewController.h

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        //新建一个button,用来做点击事件
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(80, 180, 180, 30);
        [btn setTitle:@"点击进行京东支付" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(ttt:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor redColor];
        [self.view addSubview:btn];
     }
    
    //点击事件
    - (void)ttt:(UIButton *)btn {
        NSLog(@"点击进行京东支付");
        [self jdPay];
    }
    

    //支付

    - (void)jdPay {
        NSString *userid = @"你的ID";
        NSString *money = @"0.15
    ![Simulator Screen Shot 2016年6月20日 14.32.54.png](https://img.haomeiwen.com/i1903176/b2c81ed5e8412b3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";
    //具体根据你们后台的参数来设置
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:userid,@"linkTel",@"付款",@"body",money,@"actualAmount",userid,@"userId",@"1",@"deviceType", nil];
    
    //创建对象
      JDViewController *jd  = [JDViewController new];
    
    //拼接url
      NSString *url = [NSString stringWithFormat:@"{\"linkTel\":\"%@\",\"body\":\"支付名称\",\"actualAmount\":\"%@\",\"deviceType\":\"1\",\"userId\":\"%@\"}", dic[@"linkTel"], dic[@"actualAmount"], dic[@"userId"]];
    
       jd.urlStr = [NSString stringWithFormat:@"%@%@", @"你们后台接口地址message=",url];
    
       //跳转
       [self.navigationController pushViewController:jd animated:YES];
    }
    

    结果图如下

    Simulator Screen Shot 2016年6月20日 14.32.54.png 59724001-2E9A-4D96-884B-462ED953BA32.png

    好啦,如果你喜欢的话,请点个赞。

    相关文章

      网友评论

        本文标题:iOS中京东支付

        本文链接:https://www.haomeiwen.com/subject/couwdttx.html