compatible with
Objective-Candswift
- Support automatically select whether to use
UIWebVieworWKWebViewto load web pages based on system version - Use the method is simple, and
UIWebViewvery similar to the use of simplifiedWKWebViewuse - Dual delegate mode, support the use of
commonDelegate(commonDelegate class suggested using singleton mode), also supports the simultaneous set of customdelegateBy default, the custom
delegateimplementation of the method will take precedence over thecommonDelegateto achieve the same name method;In the custom
delegateto achieve a method in the case will not call thecommonDelegatein the same name method. (If necessary, you can use the method [webView.commonDelegate someFunc ..] to invoke thecommonDelegatemethod in the customdelegate) - supports
ProgressView,UIWebViewuses part of the code in NJKWebViewProgress to simulate the progress of the configuration, support the configuration ProgressView color - Support the configuration is allowed to slide back (
canScrollBack) - Support the configuration can zoom in and out of the page (
canScrollChangeSize) - Support configuration is masked long press the link to display ActionSheet & MenuController (
blockTouchCallout) - Support configuration for blocking links 3DTouch preview (
block3DTouch)
TWebViewController's back button for back web page, if back to first page, click it will pop TWebViewControllerTWebViewControllerclick the back button to back web page, will appear the close button in controllerTWebViewControllerin the Debug model contains the empty cache and enter the URL button (automatically save the last manually entered URL), in the Release model automatically blocked.
- All of the
@optionalproxy methods are easier to use
If your project support iOS 7 or older, please download all files in the Source directory and TWebKit.bundle, then put them in your project, No other configuration can be used.
If your project just support iOS 8+, it is recommended to use CocoaPods or Carthage.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsTo integrate TWebKit into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '6.0'
use_frameworks!
target '<Your Target Name>' do
pod 'TWebKit'
endThen, run the following command:
$ pod installCarthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthageTo integrate TWebKit into your Xcode project using Carthage, specify it in your Cartfile:
github "tobedefined/TWebKit" ~> 1.2.1Run carthage update to build the framework and drag the built TWebKit.framework into your Xcode project.
- swift
in swift, add following code in <Your Target Name>-Bridging-Header.h file:
#import <TWebKit/TWebKit.h>- Objective-C
#import <TWebKit/TWebKit.h>You can see the specific use of the demo, the following is a specific introduction
Most of the parameters and methods are same like UIWebView, the following describes some of the different parameters and methods.
- delegate:
id <TWebViewDelegate>, any object that confirm theTWebViewDelegateprotocol, if set, has a higher priority than the commonDelegate method. - commonDelegate:
id <TWebViewDelegate>, any object that obeys theTWebViewDelegateprotocol, is recommended to use a singleton object as a commonDelegate. - showProgress:
BOOL,getter=isShowProgress, whether to display the progress view. - progressTintColor:
UIColor, progress color, if setting, theshowProgressbecomeYES/truenow. - canSelectContent:
BOOL, set whether you can long press to select the contents of the page. - canScrollChangeSize:
BOOL, whether you can drag to change the page size. - blockTouchCallout:
BOOL, whether to block the long press link appears actionSheet and menuController. - canScrollBack:
BOOL, iOS8+ support, whether it can slide back to the previous page. - block3DTouch:
BOOL, iOS9+ support, whether to block 3DTouch preview links. - confirmText:
NSString, the confirm button text of web page pop-up box. - cancelText:
NSString, the cancel button text of web page pop-up box. - loadingDefaultTitle:
NSString, the title text returned by default in the page loading. - successDefaultTitle:
NSString, the title text returned by default in the page load succesed. - failedDefaultTitle:
NSString, the title text returned by default in the page load failed.
-
- (instancetype)initCreate a default
TWebViewConfigobject and call- (instancetype)initWithConfig:(TWebViewConfig *)config. -
- (instancetype)initWithConfig:(TWebViewConfig *)configUse the parameters in
configto initialize to createTWebView. -
- (void)clearCacheClear cache and cookies.
-
- (void)resetCookieForceOverride:(BOOL)forceOverrideGet cookies in
NSHTTPCookieStorageand set the cookie for TWebView, theforceOverrideparameter control use the cookie value inNSHTTPCookieStorageto reset the cookie of the same name that existed before TWebView, ifforceOverrideisNO/false, will don't reset the same name cookie. -
- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completionTake out the page's
titletocompletion(it was useJavaScriptto get the web page in thedocument.title) -
+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)dataClass method to get
JavaScript function,functionparameter to access the JavaScript method name (no need to add brackets),dataparameters can beJSON Objector ordinaryNSString, will automatically convert Stitching; returns a function call string after splicing. -
- (void)runJavascript:(NSString *)js completion:(void (^__nullable)(id obj, NSError *error))completionRun the JavaScript function, interact with the web page,
jsparameter for the running javascript code,completeparameter for the callback.
In order to make the configuration parameters more clear, so add the TWebViewConfig class, parameters corresponding to the TWebView parameters, you can use TWebViewConfig to create the configuration, and then use the configuration to create TWebView, assuredly, you can directly create TWebView object, and then assign the parameters of the created TWebView object.
| TWebViewConfig parameters | -> | TWebView parameters |
|---|---|---|
| webViewCommonDelegate | -> | commonDelegate |
| webViewDelegate | -> | delegate |
| forceOverrideCookie | -> | forceOverrideCookie |
| showProgressView | -> | showProgress |
| progressTintColor | -> | progressTintColor |
| canSelectContent | -> | canSelectContent |
| canScrollChangeSize | -> | canScrollChangeSize |
| blockTouchCallout | -> | blockTouchCallout |
| canScrollBack | -> | canScrollBack |
| block3DTouch | -> | block3DTouch |
| confirmText | -> | confirmText |
| cancelText | -> | cancelText |
| loadingDefaultTitle | -> | loadingDefaultTitle |
| successDefaultTitle | -> | successDefaultTitle |
| failedDefaultTitle | -> | failedDefaultTitle |
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
Whether to display to clear the cache and enter the URL for automatic control (Debug display, Release does not show), no configuration
- webView:
TWebView,TWebViewController'sTWebViewobject, you can modify some of the properties that match your configuration requirements. - navTitle:
NSString, default navgation title, if setting, priority of- (void)webView:(TWebView *)webView loadStatus:(TWebViewLoadStatus)status title:(NSString *)titlemethod, will always shownavTitle. - backImage:
UIImage, default useback.pnginTWebKit.bundle, you can customize set the back button image.
-
- (instancetype)initWithConfig:(TWebViewConfig *)configCreate
TWebViewControlleraccording to theTWebViewConfigconfiguration. -
- (void)loadURLFromString:(NSString *)urlStringload
urlString's web page. -
- (void)loadURLAndAutoConversionFromString:(NSString *)urlStringload
urlString's web page, will be on the urlString transcoding judgments and other operations, seeNSString *trueURLString(NSString *urlString)method. -
- (void)resetWebViewCookieForceOverride:(BOOL)forceOverridecall
TWebView's- (void)resetCookieForceOverride:(BOOL)forceOverridemethod, reset webView's cookie.