compatible with
Objective-C
andswift
- Support automatically select whether to use
UIWebView
orWKWebView
to load web pages based on system version - Use the method is simple, and
UIWebView
very similar to the use of simplifiedWKWebView
use - Dual delegate mode, support the use of
commonDelegate
(commonDelegate class suggested using singleton mode), also supports the simultaneous set of customdelegate
By default, the custom
delegate
implementation of the method will take precedence over thecommonDelegate
to achieve the same name method;In the custom
delegate
to achieve a method in the case will not call thecommonDelegate
in the same name method. (If necessary, you can use the method [webView.commonDelegate someFunc ..] to invoke thecommonDelegate
method in the customdelegate
) - supports
ProgressView
,UIWebView
uses 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 (
scrollBackType
) - Support the configuration can zoom in and out of the page (
scrollChangeSizeType
) - Support configuration is masked long press the link to display ActionSheet & MenuController (
touchCalloutType
) - Support configuration for blocking links 3DTouch preview (
webView3DTouchType
)
TWebViewController
's back button for back web page, if back to first page, click it will pop TWebViewControllerTWebViewController
click the back button to back web page, will appear the close button in controllerTWebViewController
in 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
@optional
proxy 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 cocoapods
To 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'
end
Then, run the following command:
$ pod install
Carthage
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 carthage
To integrate TWebKit
into your Xcode project using Carthage, specify it in your Cartfile
:
github "tobedefined/TWebKit"
Run 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 theTWebViewDelegate
protocol, if set, has a higher priority than the commonDelegate method. - commonDelegate:
id <TWebViewDelegate>
, any object that obeys theTWebViewDelegate
protocol, is recommended to use a singleton object as a commonDelegate. - contentWebView:
UIView
, if< iOS 8.0
, returnTWebView
'sUIWebView
; if≥ iOS 8.0
, returnTWebView
'sWKWebView
. - uiWebView:
UIWebView
, if< iOS 8.0
, returnTWebView
'sUIWebView
; if≥ iOS 8.0
, returnnil
. - wkWebView:
WKWebView
, if< iOS 8.0
, returnnil
; if≥ iOS 8.0
, returnTWebView
'sWKWebView
. - scrollView:
UIScrollView
, web page'sscrollView
,readonly
, returnscrollView
ofUIWebView
orWKWebView
- showProgress:
BOOL
,getter=isShowProgress
, whether to display the progress view. - progressTintColor:
UIColor
, progress color. - progressViewHeight:
CGFloat
, set the progress view height. - selectContentType:
Enum
, set whether you can long press to select the contents of the page. - scrollChangeSizeType:
Enum
, whether you can drag to change the page size. - touchCalloutType:
Enum
, whether to block the long press link appears actionSheet and menuController. - scrollBackType:
Enum
, iOS8+ support, whether it can slide back to the previous page. - webView3DTouchType:
Enum
, 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)init
Create a default
TWebViewConfig
object and call- (instancetype)initWithConfig:(TWebViewConfig *)config
. -
- (instancetype)initWithConfig:(TWebViewConfig *)config
Use the parameters in
config
to initialize to createTWebView
. -
- (void)clearCache
Clear cache and cookies.
-
- (void)resetCookieForceOverride:(BOOL)forceOverride
Get cookies in
NSHTTPCookieStorage
and set the cookie for TWebView, theforceOverride
parameter control use the cookie value inNSHTTPCookieStorage
to reset the cookie of the same name that existed before TWebView, ifforceOverride
isNO/false
, will don't reset the same name cookie. -
- (void)getDocumentTitle:(void (^)(NSString * _Nullable))completion
Take out the page's
title
tocompletion
(it was useJavaScript
to get the web page in thedocument.title
) -
+ (nullable NSString *)getJavascriptStringWithFunctionName:(NSString *)function data:(id)data
Class method to get
JavaScript function
,function
parameter to access the JavaScript method name (no need to add brackets),data
parameters can beJSON Object
or ordinaryNSString
, will automatically convert Stitching; returns a function call string after splicing. -
- (void)runJavascript:(NSString *)js completion:(void (^__nullable)(id obj, NSError *error))completion
Run the JavaScript function, interact with the web page,
js
parameter for the running javascript code,complete
parameter 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 |
progressViewHeight | -> | progressViewHeight |
selectContentType | -> | selectContentType |
scrollChangeSizeType | -> | scrollChangeSizeType |
touchCalloutType | -> | touchCalloutType |
scrollBackType | -> | scrollBackType |
webView3DTouchType | -> | webView3DTouchType |
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
-
defaultCachePolicy:Class property, NSURLRequest cache policy
-
defaultTimeoutInterval:Class property, NSURLRequest timeout interval
-
webView:
TWebView
,TWebViewController
'sTWebView
object, 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 *)title
method, will always shownavTitle
. -
backImage:
UIImage
, default useback.png
inTWebKit.bundle
, you can customize set the back button image.
-
- (instancetype)initWithConfig:(TWebViewConfig *)config
Create
TWebViewController
according to theTWebViewConfig
configuration. -
- (void)loadURLFromString:(NSString *)urlString
load
urlString
's web page. -
- (void)loadURLFromString:(NSString *)urlString cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval
load
urlString
's web page, withcachePolicy
andtimeoutInterval
. -
- (void)loadURLAndAutoConversionFromString:(NSString *)urlString
load
urlString
's web page, will be on the urlString transcoding judgments and other operations, seeNSString *trueURLString(NSString *urlString)
method. -
- (void)resetWebViewCookieForceOverride:(BOOL)forceOverride
call
TWebView
's- (void)resetCookieForceOverride:(BOOL)forceOverride
method, reset webView's cookie.