TWebKit 1.3.0

TWebKit 1.3.0

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2020年1月

ToBeDefined维护。



TWebKit 1.3.0

TWebKit

platform  Carthage compatible  CocoaPods  Build Status  License MIT

功能

兼容 Objective-Cswift

TWebView

  • 支持自动根据系统版本选择是否使用UIWebViewWKWebView加载网页
  • 使用方法简单,UIWebView的用法与简化版的WKWebView非常相似
  • 双代理模式,支持使用commonDelegate(建议使用单例模式),也支持同时设置自定义delegate

    默认情况下,自定义delegate实现的同名方法将优于commonDelegate

    在自定义delegate中实现同名方法的情况下,不会调用commonDelegate中的同名方法。(如果需要,可以使用方法[webView(commonDelegate someFunc ..])在自定义delegate中调用commonDelegate的方法)

  • 支持ProgressViewUIWebView使用部分代码在NJKWebViewProgress中模拟进度配置,支持配置ProgressView颜色
  • 支持配置是否允许滑动返回(scrollBackType
  • 支持配置是否可以缩放页面(scrollChangeSizeType
  • 支持配置是否屏蔽链接3DTouch预览(webView3DTouchType
  • 支持配置是否屏蔽分享按钮(webViewShareButtonType

TWebViewController

  • TWebViewController的返回按钮用于返回网页,如果返回到第一页,点击它将弹出TWebViewController
  • 点击TWebViewController的返回按钮返回网页,将出现控制器中的关闭按钮
  • 在Debug模式下,TWebViewController包含清空缓存和输入URL按钮(自动保存手动输入的最后URL),在Release模式下自动遮挡。

TWebViewDelegate

  • 所有使用@optional代理方法都更容易使用

安装

源文件

如果你的项目支持 iOS 7 及更早版本,请下载 Source 目录中的所有文件和 TWebKit.bundle,然后将它们放入你的项目中,其他配置无效。

如果你的项目仅支持 iOS 8+,建议使用 CocoaPodsCarthage

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理工具。你可以使用以下命令安装它:

$ gem install cocoapods

使用 CocoaPods 将 TWebKit 集成到你的 Xcode 项目中,请在你的 Podfile 中指定它:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '6.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'TWebKit'
end

然后,运行以下命令:

$ pod install

Carthage

Carthage 是一个去中心化的依赖管理工具,它可以构建你的依赖关系并提供二进制框架。

你可以使用以下命令通过 Homebrew 安装 Carthage:

$ brew update
$ brew install carthage

使用 Carthage 将 TWebKit 集成到你的 Xcode 项目中,请在你的 Cartfile 中指定它。

github "tobedefined/TWebKit"

运行 carthage update 命令来构建框架,然后将构建好的 TWebKit.framework 拖拽到你的 Xcode 项目中。

如何使用

  • swift

在 swift 中,请在 <Your Target Name>-Bridging-Header.h 文件中添加以下代码:

#import <TWebKit/TWebKit.h>
  • Objective-C
#import <TWebKit/TWebKit.h>

您可以在下面的演示中查看具体用法,以下是一些具体介绍:

TWebView

大部分参数和方法与 UIWebView 相似,以下描述了一些不同的参数和方法。

property
  • delegate: id <TWebViewDelegate>,确认遵循 TWebViewDelegate 协议的任何对象,如果已设置,则比 commonDelegate 方法具有更高的优先级。
  • commonDelegate: id <TWebViewDelegate>,遵循 TWebViewDelegate 协议的任何对象,建议使用单例对象作为 commonDelegate。
  • contentWebView: UIView,如果 < iOS 8.0,则返回 TWebViewUIWebView;如果 ≥ iOS 8.0,则返回 TWebViewWKWebView
  • uiWebView: UIWebView,如果 < iOS 8.0,则返回 TWebViewUIWebView;如果 ≥ iOS 8.0,则返回 nil
  • wkWebView: WKWebView,如果 < iOS 8.0,则返回 nil;如果 ≥ iOS 8.0,则返回 TWebViewWKWebView
  • scrollView: UIScrollView,网页的 scrollView,只读,返回 UIWebViewWKWebViewscrollView
  • showProgress: BOOLgetter=isShowProgress,是否显示进度视图。
  • progressTintColor: UIColor,进度颜色。
  • progressViewHeight: CGFloat,设置进度视图高度。
  • selectContentType: Enum,设置是否可以长按选择页面内容。
  • scrollChangeSizeType: Enum,是否可以通过拖动来更改页面大小。
  • touchCalloutType: Enum,是否要阻止长按链接出现的操作表和菜单控制器。
  • scrollBackType: Enum,iOS8+ 支持,是否可以滑动回上一页。
  • webView3DTouchType: Enum,iOS9+ 支持,是否要阻止 3DTouch 预览链接。
  • confirmText: NSString,网页弹窗的确认按钮文本。
  • cancelText: NSString,网页弹窗的取消按钮文本。
  • loadingDefaultTitle: NSString,页面加载时默认返回的标题文本。
  • successDefaultTitle: NSString,页面加载成功时默认返回的标题文本。
  • failedDefaultTitle: NSString,页面加载失败时默认返回的标题文本。
function
  • - (instancetype)init

    创建一个默认的 TWebViewConfig 对象,并调用 - (instancetype)initWithConfig:(TWebViewConfig *)config

  • - (instancetype)initWithConfig:(TWebViewConfig *)config

    使用 config 中的参数初始化创建 TWebView

  • - (void)clearCache

    清除缓存和 cookie。

  • - (void)resetCookieForceOverride:(BOOL)forceOverride

    NSHTTPCookieStorage 获取 cookie,并将其设置为 TWebView 的 cookie,forceOverride 参数控制是否使用 NSHTTPCookieStorage 中的 cookie 值来重置 TWebView 中已存在的同名字符的 cookie,如果 forceOverrideNO/false,则不会重置同名字符的 cookie。

  • - (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion

    获取网页的 titlecompletion(使用 JavaScript 获取网页中的 document.title)

  • + (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data

    获取JavaScript函数的类方法,function参数用于访问JavaScript方法名(无需添加括号),data参数可以是JSON对象或普通NSString,将自动转义;返回拼接后函数调用字符串。

  • - (void)runJavascript:(NSString *)js completion:(void (^__nullable)(id obj, NSError *error))completion

    运行JavaScript函数,与网页交互,js参数为要运行的JavaScript代码,complete参数为回调函数。

TWebViewConfig

为了使配置参数更加清晰,因此加入TWebViewConfig类,与TWebView参数对应,可以使用TWebViewConfig来创建配置,然后使用配置创建TWebView,当然,您可以直接创建TWebView对象,然后分配已创建TWebView对象的参数。

TWebViewConfig参数 -> TWebView参数
webViewCommonDelegate -> commonDelegate
webViewDelegate -> delegate
forceOverrideCookie -> forceOverrideCookie
showProgressView -> showProgress
progressTintColor -> progressTintColor
progressViewHeight -> progressViewHeight
selectContentType -> selectContentType
scrollChangeSizeType -> scrollChangeSizeType
touchCalloutType -> touchCalloutType
scrollBackType -> scrollBackType
webView3DTouchType -> webView3DTouchType
confirmText -> confirmText
cancelText -> cancelText
loadingDefaultTitle -> loadingDefaultTitle
successDefaultTitle -> successDefaultTitle
failedDefaultTitle -> failedDefaultTitle

TWebViewDelegate

typedef NS_ENUM(NSUInteger, TWebViewLoadStatus) {
    TWebViewLoadStatusIsLoading = 1,
    TWebViewLoadStatusSuccess   = 2,
    TWebViewLoadStatusFailed    = 3,
};

@protocol TWebViewDelegate <NSObject>

@optional

// Whether you can load web pages
- (BOOL)webView:(TWebView *)webView shouldStartLoadRequest:(NSURLRequest *)request;

// Start loading page
- (void)webView:(TWebView *)webView didStartLoadRequest:(NSURLRequest *)request;

// Load page successfully
- (void)webView:(TWebView *)webView didFinishLoadRequest:(NSURLRequest *)request;

// Loading page failed
- (void)webView:(TWebView *)webView didFailedLoadRequest:(NSURLRequest *)request withError:(NSError *)error;

// Current status: status, current default use of the title, 
// You can determination title to the ViewController based on the status. Or you can set the title parameter to the title of the ViewController.
// TWebViewLoadStatusIsLoading  => return TWebView's loadingDefaultTitle
// TWebViewLoadStatusSuccess    => get web page's title, return it if not empty; if empty, return TWebView's successDefaultTitle
// TWebViewLoadStatusFailed     => return TWebView's failedDefaultTitle
- (void)webView:(TWebView *)webView loadStatus:(TWebViewLoadStatus)status title:(NSString *)title;

#pragma mark - 3D Touch Peek & Pop; iOS 10+ available
// Set whether to allow preview url;
// If you return to NO, the following two methods will not run;
// If you return to YES, The following two methods will be run when hard pressed.
- (BOOL)webView:(TWebView *)webView shouldPreviewURL:(nullable NSURL *)url API_AVAILABLE(ios(10.0));

// If you return to nil, the preview link will be made in Safari
// If you do not want to preview the url, please return NO at method "- webView:shouldPreviewURL:"
// param "actions" is the iOS default support actions
- (nullable UIViewController *)webView:(TWebView *)webView previewingViewControllerForURL:(nullable NSURL *)url defaultActions:(NSArray<id <WKPreviewActionItem>> *)actions API_AVAILABLE(ios(10.0));

// Pop the previewing ViewController and then run this method
- (void)webView:(TWebView *)webView commitPreviewingURL:(nullable NSURL *)url controller:(UIViewController *)controller API_AVAILABLE(ios(10.0));

@end

TWebViewController

是否显示清除缓存并自动控制进入URL(调试显示,发布时不显示),无需配置

属性
  • defaultCachePolicy:类属性,NSURLRequest缓存策略

  • defaultTimeoutInterval:类属性,NSURLRequest超时时间间隔

  • webView: TWebViewTWebViewControllerTWebView对象,可以修改某些与您的配置要求匹配的属性。

  • navTitle: NSString,默认导航标题,如果设置,会一直显示navTitle

  • backImage: UIImage,默认使用TWebKit.bundle中的back.png,可以自定义设置后退按钮图像。

函数
  • - (instancetype)initWithConfig:(TWebViewConfig *)config

    根据 TWebViewConfig 配置创建 TWebViewController

  • - (void)loadURLFromString:(NSString *)urlString

    加载 urlString 的网页。

  • - (void)loadURLFromString:(NSString *)urlString cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval

    加载 urlString 的网页,带有 cachePolicytimeoutInterval

  • - (void)loadURLAndAutoConversionFromString:(NSString *)urlString

    加载 urlString 的网页,将进行对 urlString 的转码判断和其他操作,请参阅 NSString *trueURLString(NSString *urlString) 方法。

  • - (void)resetWebViewCookieForceOverride:(BOOL)forceOverride

    调用 TWebView- (void)resetCookieForceOverride:(BOOL)forceOverride 方法,重置 webView 的 cookie。