有的童鞋沒弄過京東支付跑來問我,我們項(xiàng)目里有用到京東支付疮蹦,于是自己寫了個(gè)demo诸迟,代碼貼在下面,希望能幫助到不會(huì)的童鞋愕乎。
1.新建一個(gè)繼承Viewcontroller的JDViewcontroller
2. 在JDViewcontroller.h文件中阵苇,定義一個(gè)url屬性,用來傳url(廢話)
@property (nonatomic, copy) NSString *urlStr;
3.在JDViewcontroller.m文件中感论,導(dǎo)入<JavaScriptCore/JavaScriptCore.h>框架绅项,并定義一個(gè)成員變量webView
{
UIWebView *_webView;
}
4.主要代碼
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"京東支付";
self.view.backgroundColor = [UIColor whiteColor];
[self initSubView];
}
- (void)initSubView {
//創(chuàng)建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:@"支付的結(jié)果"];
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中導(dǎo)入JDViewController.h
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//新建一個(gè)button比肄,用來做點(diǎn)擊事件
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(80, 180, 180, 30);
[btn setTitle:@"點(diǎn)擊進(jìn)行京東支付" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(ttt:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
}
//點(diǎn)擊事件
- (void)ttt:(UIButton *)btn {
NSLog(@"點(diǎn)擊進(jìn)行京東支付");
[self jdPay];
}
//支付
- (void)jdPay {
NSString *userid = @"你的ID";
NSString *money = @"0.15
![Simulator Screen Shot 2016年6月20日 14.32.54.png](http://upload-images.jianshu.io/upload_images/1903176-b2c81ed5e8412b3a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";
//具體根據(jù)你們后臺(tái)的參數(shù)來設(shè)置
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:userid,@"linkTel",@"付款",@"body",money,@"actualAmount",userid,@"userId",@"1",@"deviceType", nil];
//創(chuàng)建對(duì)象
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:@"%@%@", @"你們后臺(tái)接口地址message=",url];
//跳轉(zhuǎn)
[self.navigationController pushViewController:jd animated:YES];
}
結(jié)果圖如下
好啦快耿,如果你喜歡的話,請(qǐng)點(diǎn)個(gè)贊芳绩。