Explorar o código

Add WKWebView example (#1357)

Frank %!s(int64=6) %!d(string=hai) anos
pai
achega
3c7c7bab2e

+ 6 - 0
MJRefreshExample.xcodeproj/project.pbxproj

@@ -8,6 +8,7 @@
 
 /* Begin PBXBuildFile section */
 		01A2CE0C21ACE01500BEE365 /* MJRefreshConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 01A2CE0B21ACE01500BEE365 /* MJRefreshConfig.m */; };
+		01EB8272236846CE00813DAA /* MJWKWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 01EB8271236846CE00813DAA /* MJWKWebViewController.m */; };
 		2D4698861D0EE6A400CB8025 /* NSBundle+MJRefresh.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4698851D0EE6A400CB8025 /* NSBundle+MJRefresh.m */; };
 		2D9BEB091BB15F4A00AED473 /* UIViewController+Example.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D9BEAE01BB15F4A00AED473 /* UIViewController+Example.m */; };
 		2D9BEB0A1BB15F4A00AED473 /* MJChiBaoZiFooter.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D9BEAE31BB15F4A00AED473 /* MJChiBaoZiFooter.m */; };
@@ -122,6 +123,8 @@
 /* Begin PBXFileReference section */
 		01A2CE0A21ACE01500BEE365 /* MJRefreshConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJRefreshConfig.h; sourceTree = "<group>"; };
 		01A2CE0B21ACE01500BEE365 /* MJRefreshConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJRefreshConfig.m; sourceTree = "<group>"; };
+		01EB8270236846CE00813DAA /* MJWKWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJWKWebViewController.h; sourceTree = "<group>"; };
+		01EB8271236846CE00813DAA /* MJWKWebViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJWKWebViewController.m; sourceTree = "<group>"; };
 		2D4698841D0EE6A400CB8025 /* NSBundle+MJRefresh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSBundle+MJRefresh.h"; sourceTree = "<group>"; };
 		2D4698851D0EE6A400CB8025 /* NSBundle+MJRefresh.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSBundle+MJRefresh.m"; sourceTree = "<group>"; };
 		2D9BEADF1BB15F4A00AED473 /* UIViewController+Example.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+Example.h"; sourceTree = "<group>"; };
@@ -308,6 +311,8 @@
 				2D9BEB061BB15F4A00AED473 /* MJWebViewViewController.h */,
 				2D9BEB071BB15F4A00AED473 /* MJWebViewViewController.m */,
 				2D9BEB081BB15F4A00AED473 /* MJWebViewViewController.xib */,
+				01EB8270236846CE00813DAA /* MJWKWebViewController.h */,
+				01EB8271236846CE00813DAA /* MJWKWebViewController.m */,
 			);
 			path = Second;
 			sourceTree = "<group>";
@@ -714,6 +719,7 @@
 				2DB2EA1D1BECBE6700D58F6A /* MJRefreshAutoNormalFooter.m in Sources */,
 				2D9BEB0C1BB15F4A00AED473 /* MJChiBaoZiHeader.m in Sources */,
 				2DB2EA1B1BECBE6700D58F6A /* MJRefreshHeader.m in Sources */,
+				01EB8272236846CE00813DAA /* MJWKWebViewController.m in Sources */,
 				2D9BEB191BB15F4A00AED473 /* MJTestViewController.m in Sources */,
 				2DB2EA211BECBE6700D58F6A /* MJRefreshBackStateFooter.m in Sources */,
 				2DB2EA281BECBE6700D58F6A /* UIScrollView+MJRefresh.m in Sources */,

+ 10 - 1
MJRefreshExample/Classes/First/MJExampleViewController.m

@@ -14,11 +14,13 @@
 #import "MJExample.h"
 #import "UIViewController+Example.h"
 #import "MJRefresh.h"
+#import "MJWKWebViewController.h"
 
 static NSString *const MJExample00 = @"UITableView + 下拉刷新";
 static NSString *const MJExample10 = @"UITableView + 上拉刷新";
 static NSString *const MJExample20 = @"UICollectionView";
 static NSString *const MJExample30 = @"UIWebView";
+static NSString *const MJExample40 = @"WKWebView";
 
 @interface MJExampleViewController()
 @property (strong, nonatomic) NSArray *examples;
@@ -53,7 +55,14 @@ - (NSArray *)examples
         exam3.titles = @[@"下拉刷新"];
         exam3.methods = @[@"example31"];
         
-        self.examples = @[exam0, exam1, exam2, exam3];
+        MJExample *exam4 = [[MJExample alloc] init];
+        exam4.header = MJExample40;
+        exam4.vcClass = [MJWKWebViewController class];
+        exam4.titles = @[@"下拉刷新"];
+        exam4.methods = @[@"example41"];
+        
+        
+        self.examples = @[exam0, exam1, exam2, exam3, exam4];
     }
     return _examples;
 }

+ 17 - 0
MJRefreshExample/Classes/Second/MJWKWebViewController.h

@@ -0,0 +1,17 @@
+//
+//  MJWKWebViewController.h
+//  MJRefreshExample
+//
+//  Created by Frank on 2019/10/29.
+//  Copyright © 2019 小码哥. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface MJWKWebViewController : UIViewController
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 104 - 0
MJRefreshExample/Classes/Second/MJWKWebViewController.m

@@ -0,0 +1,104 @@
+//
+//  MJWKWebViewController.m
+//  MJRefreshExample
+//
+//  Created by Frank on 2019/10/29.
+//  Copyright © 2019 小码哥. All rights reserved.
+//
+
+#import "MJWKWebViewController.h"
+#import "UIViewController+Example.h"
+#import "MJChiBaoZiHeader.h"
+#import "MJRefresh.h"
+
+@import WebKit;
+
+@interface MJWKWebViewController ()<WKNavigationDelegate>
+@property (strong, nonatomic) WKWebView *webView;
+
+@end
+
+@implementation MJWKWebViewController
+
+#pragma mark - 示例
+- (void)example41 {
+    __weak WKWebView *webView = self.webView;
+    webView.navigationDelegate = self;
+    
+    __weak UIScrollView *scrollView = self.webView.scrollView;
+    
+    // 添加下拉刷新控件
+    scrollView.mj_header= [MJChiBaoZiHeader headerWithRefreshingBlock:^{
+        [webView reload];
+    }];
+    
+    // 如果是上拉刷新,就以此类推
+    [scrollView.mj_header beginRefreshing];
+}
+
+#pragma mark - webViewDelegate
+- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
+    [self.webView.scrollView.mj_header endRefreshing];
+}
+
+#pragma mark - 其他
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    
+    self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
+    self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+    [self.view addSubview:self.webView];
+    
+    CGSize size = self.view.frame.size;
+    
+    UILabel *warningLabel = [[UILabel alloc] initWithFrame:CGRectMake(size.width - 210, size.height - 120, 200, 50)];
+    warningLabel.text = @"注意,这不是原生界面,是个网页:http://weibo.com/excepptions";
+    warningLabel.textColor = UIColor.blackColor;
+    warningLabel.backgroundColor = [UIColor.lightGrayColor colorWithAlphaComponent:0.3];
+    warningLabel.numberOfLines = 0;
+    warningLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
+    [self.view addSubview:warningLabel];
+    
+    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(size.width - 210, size.height - 60, 200, 50)];
+    [backButton setTitle:@"回到上一页" forState:UIControlStateNormal];
+    [backButton setBackgroundColor:UIColor.redColor];
+    backButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
+    [self.view addSubview:backButton];
+    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
+    
+    // 加载页面
+    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://weibo.com/exceptions"]]];
+    
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
+    [self performSelector:NSSelectorFromString(self.method) withObject:nil];
+#pragma clang diagnostic pop
+}
+
+- (void)viewWillAppear:(BOOL)animated
+{
+    [super viewWillAppear:animated];
+    
+    [self.navigationController setNavigationBarHidden:YES animated:YES];
+    
+    [self setNeedsStatusBarAppearanceUpdate];
+}
+
+- (void)viewWillDisappear:(BOOL)animated
+{
+    [super viewWillDisappear:animated];
+    
+    [self.navigationController setNavigationBarHidden:NO animated:YES];
+}
+
+- (BOOL)prefersStatusBarHidden
+{
+    return YES;
+}
+
+- (IBAction)back {
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+
+@end

+ 3 - 1
MJRefreshExample/Classes/Second/MJWebViewViewController.m

@@ -9,6 +9,7 @@
 #import "MJWebViewViewController.h"
 #import "UIViewController+Example.h"
 #import "MJRefresh.h"
+#import "MJChiBaoZiHeader.h"
 
 @interface MJWebViewViewController () <UIWebViewDelegate>
 @property (weak, nonatomic) IBOutlet UIWebView *webView;
@@ -24,11 +25,12 @@ - (void)example31
     __weak UIScrollView *scrollView = self.webView.scrollView;
     
     // 添加下拉刷新控件
-    scrollView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
+    scrollView.mj_header= [MJChiBaoZiHeader headerWithRefreshingBlock:^{
         [webView reload];
     }];
     
     // 如果是上拉刷新,就以此类推
+    [scrollView.mj_header beginRefreshing];
 }
 
 #pragma mark - webViewDelegate