新学到了WKWebView:
#import <WebKit/WebKit.h>
然后要为这个类的实例对象添加导航代理,因此要遵守WKNavigationDelegate协议,并且声明一个WKWebView的属性:
@interface neiRongViewController : UIViewController<UIScrollViewDelegate, WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *wkwebView;
在视图控制器中,我们首先创建一个这个类的实例对象,并且为其设置导航代理:
self.wkwebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.wkwebView.navigationDelegate = self;
然后获取对应网页的url,并使用loadRequest方法加载网页:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", today_stories.url]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wkwebView loadRequest:request];
WKNavigationDelegate
协议定义了一系列方法,允许你监控和响应 WKWebView
的导航事件。以下是一些常用的 WKNavigationDelegate
方法:
页面开始加载时调用:
webView:didStartProvisionalNavigation:
:在开始加载一个页面时调用,通常表示已经接收到页面的主要内容,但可能还有其他资源需要加载。页面导航发生错误时调用:
webView:didFailProvisionalNavigation:withError:
:在加载页面时发生错误时调用,例如无法连接到服务器或找不到页面。页面加载完成时调用:
webView:didFinishNavigation:
:在页面成功加载完成后调用,可用于执行与页面加载相关的操作。页面导航被重定向时调用:
webView:didReceiveServerRedirectForProvisionalNavigation:
:在页面导航被服务器重定向到其他位置时调用。页面加载失败时调用:
webView:didFailNavigation:withError:
:在页面导航失败时调用,例如因为网络问题或其他原因导致加载失败。页面加载过程中调用:
webView:didCommitNavigation:
:在页面加载过程中,当接收到更多数据时调用。页面导航操作完成时调用:
webView:didFinishNavigation:
:在页面导航操作完成后调用,此时页面已完全加载。页面加载发生错误时调用:
webView:didFailProvisionalNavigation:withError:
:在加载页面时发生错误时调用,可以用于处理加载失败的情况。处理页面身份验证时调用:
webView:didReceiveAuthenticationChallenge:completionHandler:
:在需要进行身份验证时调用,你可以在此方法中提供用户凭据,例如用户名和密码。页面内容进程终止时调用:
webViewWebContentProcessDidTerminate:
:在页面内容进程意外终止时调用,你可以在此方法中处理相应的逻辑。这些方法允许我们监控页面导航、处理导航错误、执行页面加载后的操作以及其他与 WKWebView
相关的事件。
在这里,我使用的是- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;即完成网页的获取后调用。
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
imageScrollTableViewCell *scrollCell = [self.mainview.tableView dequeueReusableCellWithIdentifier:@"idimage"];
scrollCell.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 7, 400);
scrollCell.scrollView.pagingEnabled = YES;
scrollCell.scrollView.showsHorizontalScrollIndicator = NO;
for (int i = 0; i < 6; i++) {
if (i == 0) {
top_storiesModel *top_stories = self.mainModel.top_stories[4];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", top_stories.image]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"geRen.jpg"]];
button.frame = CGRectMake(0, 0, self.view.bounds.size.width, 400);
button.tag = 4;
[button addTarget:self action:@selector(scrollButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UILabel *title = [[UILabel alloc] init];
title.font = [UIFont systemFontOfSize:24];
title.numberOfLines = 2;
title.textColor = [UIColor whiteColor];
[button addSubview:title];
[title mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(button).offset(24);
make.top.equalTo(button).offset(250);
make.width.equalTo(@374);
make.height.equalTo(@100);
}];
title.text = top_stories.title;
UILabel *zuoZheTitle = [[UILabel alloc] init];
zuoZheTitle.numberOfLines = 0;
zuoZheTitle.textColor = [UIColor whiteColor];
[button addSubview:zuoZheTitle];
[zuoZheTitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(button).offset(24);
make.top.equalTo(button).offset(330);
make.width.equalTo(@374);
make.height.equalTo(@50);
}];
zuoZheTitle.text = top_stories.hint;
[scrollCell.scrollView addSubview: button];
} else {
top_storiesModel *top_stories = self.mainModel.top_stories[i - 1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", top_stories.image]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"geRen.jpg"]];
button.frame = CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, 400);
button.tag = i - 1;
[button addTarget:self action:@selector(scrollButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UILabel *title = [[UILabel alloc] init];
title.font = [UIFont systemFontOfSize:24];
title.numberOfLines = 2;
title.textColor = [UIColor whiteColor];
[button addSubview:title];
[title mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(button).offset(24);
make.top.equalTo(button).offset(250);
make.width.equalTo(@374);
make.height.equalTo(@100);
}];
title.text = top_stories.title;
UILabel *zuoZheTitle = [[UILabel alloc] init];
zuoZheTitle.numberOfLines = 0;
zuoZheTitle.textColor = [UIColor whiteColor];
[button addSubview:zuoZheTitle];
[zuoZheTitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(button).offset(24);
make.top.equalTo(button).offset(330);
make.width.equalTo(@374);
make.height.equalTo(@50);
}];
zuoZheTitle.text = top_stories.hint;
[scrollCell.scrollView addSubview: button];
}
}
top_storiesModel *top_stories = self.mainModel.top_stories[0];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", top_stories.image]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"geRen.jpg"]];
button.frame = CGRectMake(self.view.bounds.size.width * 6, 0, self.view.bounds.size.width, 400);
button.tag = 0;
[button addTarget:self action:@selector(scrollButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UILabel *title = [[UILabel alloc] init];
title.font = [UIFont systemFontOfSize:24];
title.numberOfLines = 2;
title.textColor = [UIColor whiteColor];
[button addSubview:title];
[title mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(button).offset(24);
make.top.equalTo(button).offset(250);
make.width.equalTo(@374);
make.height.equalTo(@100);
}];
title.text = top_stories.title;
UILabel *zuoZheTitle = [[UILabel alloc] init];
zuoZheTitle.numberOfLines = 0;
zuoZheTitle.textColor = [UIColor whiteColor];
[button addSubview:zuoZheTitle];
[zuoZheTitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(button).offset(24);
make.top.equalTo(button).offset(330);
make.width.equalTo(@374);
make.height.equalTo(@50);
}];
zuoZheTitle.text = top_stories.hint;
[scrollCell.scrollView addSubview: button];
[scrollCell.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0)];
return scrollCell;
}
mainViewTableViewCell *cell = [self.mainview.tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
if (indexPath.section == 1) {
storiesModel *stories = self.mainModel.stories[indexPath.row];
cell.titleLabel.text = stories.title;
cell.zuoZheAndTimeLabel.text = stories.hint;
cell.picImageStr = stories.images[0];
} else {
for (int i = 0; i < numberOfCell; i++) {
if (indexPath.section == i + 2) {
if ([self.beforeArray count] == numberOfCell) {
beforeStoriesModel *before = self.beforeArray[i][indexPath.row];
cell.titleLabel.text = before.title;
cell.zuoZheAndTimeLabel.text = before.hint;
cell.picImageStr = before.images[0];
}
}
}
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0 || section == 1) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0)];
return view;
}
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
for (int i = 0; i < numberOfCell; i++) {
if (section == i + 2) {
int month = [[self.beforeTimeArray[i] substringWithRange:NSMakeRange(4, 2)] intValue];
int day = [[self.beforeTimeArray[i] substringWithRange:NSMakeRange(6, 2)] intValue];
UILabel *titleTimeLabel = [[UILabel alloc] init];
[view1 addSubview:titleTimeLabel];
titleTimeLabel.text = [NSString stringWithFormat:@"%d月%d日 ——————————————", month, day];
titleTimeLabel.textColor = [UIColor grayColor];
titleTimeLabel.font = [UIFont systemFontOfSize:21];
[titleTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(view1).offset(25);
make.top.equalTo(view1).offset(0);
make.width.equalTo(@(self.view.bounds.size.width));
make.height.equalTo(@30);
}];
}
}
return view1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == 0 || section == 1) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0)];
return view;
}
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
return view1;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (scrollView.contentOffset.y > self.mainview.tableView.contentSize.height - self.view.bounds.size.height + 50) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(self.view.bounds.size.width / 2 - 50, self.view.bounds.size.height - 100, 80, 80)] ;
[self.view addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
beforeManager *beforemanager = [beforeManager sharedBeforeManager];
NSInteger strInt = [self.mainModel.date longLongValue];
NSString *str = [NSString stringWithFormat:@"%ld", strInt - numberOfCell];
beforemanager.timeStr = str;
[beforemanager NetWorkWithBeforeData:^(beforeModel * _Nonnull beforeModel) {
[self.beforeArray addObject:beforeModel.stories];
[self.beforeTimeArray addObject:beforeModel.date];
numberOfCell++;
dispatch_async(dispatch_get_main_queue(), ^{
sleep(1);
[self.activityIndicator stopAnimating];
[self.mainview.tableView reloadData];
});
} error:^(NSError * _Nonnull error) {
NSLog(@"ERROE");
}];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *allNeiRongArr = [[NSArray alloc] init];
allNeiRongArr = [allNeiRongArr arrayByAddingObjectsFromArray:self.mainModel.stories];
for (int i = 0; i < numberOfCell; i++) {
allNeiRongArr = [allNeiRongArr arrayByAddingObjectsFromArray:self.beforeArray[i]];
}
if (indexPath.section != 0) {
neiRongViewController *neiRong = [[neiRongViewController alloc] init];
neiRong.theStories = allNeiRongArr;
neiRong.isButton = NO;
neiRong.allIndexNum = (1 + numberOfCell) * 5;
neiRong.section = ((int)indexPath.section - 1) * 5 + (int)indexPath.row;
[self.navigationController pushViewController:neiRong animated:YES];
}
}
- (void)webViewGet {
[self.set addObject:[NSString stringWithFormat:@"%d", self.section]];
self.wkwebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.wkwebView.navigationDelegate = self;
if (self.isButton == YES) {
top_storiesModel *top_stories = [[top_storiesModel alloc] init];
top_stories = self.theStories[self.section];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", top_stories.url]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wkwebView loadRequest:request];
} else if (self.section < 5){
storiesModel *today_stories = [[storiesModel alloc] init];
today_stories = self.theStories[self.section];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", today_stories.url]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wkwebView loadRequest:request];
} else if (self.section >= 5) {
beforeStoriesModel *before_stories = [[beforeStoriesModel alloc] init];
before_stories = self.theStories[self.section];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", before_stories.url]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.wkwebView loadRequest:request];
}
[self.activityIndicator stopAnimating];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int index = scrollView.contentOffset.x / self.view.bounds.size.width;
NSLog(@"index :%d", index);
self.section = index;
if (![self.set containsObject: [NSString stringWithFormat:@"%d", self.section]]) {
[self webViewGet];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(self.view.bounds.size.width / 2 - 50, self.view.bounds.size.height/2 - 50, 80, 80)] ;
[self.view addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
}
self.idStr = [self.theStories[self.section] ID];
[self getData];
}
首先要定义一个UIActivityIndicatorView属性,方便我们在任何地方可以关掉该控件
//初始化该控件并设置位置
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(self.view.bounds.size.width / 2 - 50, self.view.bounds.size.height - 100, 80, 80)] ;
[self.view addSubview:self.activityIndicator];
//开启加载控件
[self.activityIndicator startAnimating];
//结束该控件
[self.activityIndicator stopAnimating];