-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstViewController.m
99 lines (76 loc) · 3.1 KB
/
FirstViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// FirstViewController.m
// HWHReader
//
// Created by huanwh on 2018/10/24.
// Copyright © 2018 hwh. All rights reserved.
//
#import "FirstViewController.h"
#import "HWHDiscoverIndexCell.h"
#import "HWHDiscoverCellHeaderView.h"
@import WebKit;
@interface FirstViewController () <
WKNavigationDelegate,
UICollectionViewDelegate,
UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) WKWebView *webview;
@property (strong, nonatomic) NSArray *datasource;
@end
static NSString * kHWHErrorDomain = @"[HWHReader][ERROR]";
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setWebView];
}
- (void)setWebView {
self.webview = [[WKWebView alloc] initWithFrame:CGRectZero];
self.webview.navigationDelegate = self;
NSURLRequest * request= [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://m.23us.so/"]];
[self.webview loadRequest:request];
}
- (IBAction)jsGetBooks:(id)sender {
[self.webview evaluateJavaScript:@"getRecBookTitls()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"%@",result);
if (error || ![result isKindOfClass:[NSString class]]) {
NSLog(@"%@ , %@",kHWHErrorDomain,error);
return ;
}
NSError * jsonError = nil;
NSData * data = [result dataUsingEncoding:NSUTF8StringEncoding];
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError || ![json isKindOfClass:[NSArray class]]) {
NSLog(@"%@ , %@",kHWHErrorDomain,jsonError);
return ;
}
self.datasource = [json copy];
[self.collectionView reloadData];
}];
}
#pragma mark - UICollectionView Delegate & Datasource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HWHDiscoverIndexCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.collectionView.contentOffset = CGPointZero;
NSDictionary * item = self.datasource[indexPath.row];
cell.items = item[@"books"];
HWHDiscoverCellHeaderView * headerView = [cell viewWithTag:100];
headerView.titleLabel.text = item[@"className"];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.datasource.count ?: 0;
}
#pragma mark - WKNavigationDelegate
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self injectionJs];
[self jsGetBooks:nil];
}
#pragma mark - js helper
- (void)injectionJs {
NSString * jsPath = [[NSBundle mainBundle] pathForResource:@"parser" ofType:@"js"];
NSString * js = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];
[self.webview evaluateJavaScript:js completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"%@",result);
}];
}
@end