WebShell is an iOS application that uses WKWebView to load and display web content, with custom URL protocol handling functionality. The app allows loading local HTML resources through custom protocols (such as QuantumLink://), providing a more flexible way to load web content in iOS applications.
- Support for loading local HTML resources via custom URL protocol (QuantumLink://)
- Support for loading online web content (http/https)
- Support for loading local HTML files via File protocol
- Progress bar showing webpage loading progress
- Automatic handling of various resource types (HTML, CSS, JavaScript, images, etc.)
The core of the project is a custom URL protocol handler (SchemeHandler) that implements the WKURLSchemeHandler protocol, allowing WKWebView to handle non-standard URL protocols.
Main components:
- SchemeHandler: Implements the WKURLSchemeHandler protocol to handle custom URL requests
- WKWebView+SchemeHandler: WKWebView category extension for handling URL protocols
- Register the custom URL protocol handler in WKWebViewConfiguration
- When WKWebView encounters a custom protocol URL (e.g., QuantumLink://), it calls the SchemeHandler to process it
- SchemeHandler looks up local resources (HTML, CSS, JS, images, etc.) based on the URL path
- Converts local resources to the appropriate response format and returns them to WKWebView
// Configure WKWebView to use custom protocol in ViewController
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
if (@available(iOS 11.0, *)) {
[wkWebConfig setURLSchemeHandler:SchemeHandler.new forURLScheme:@"QuantumLink"];
}
// Create WKWebView and load custom protocol URL
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebConfig];
// Use custom protocol to load webpage
NSURL *url = [NSURL URLWithString:@"QuantumLink://www.ledgerheath.com/index.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];- WebShell/: Main project directory
- SchemeHandler/: Custom URL protocol handling related files
- SchemeHandler.h/m: Implements WKURLSchemeHandler protocol
- WKWebView+SchemeHandler.h/m: WKWebView category extension
- JL/: Sample HTML resource directory
- index.html: Home page
- blog_*.html: Blog pages
- static/: Static resources (CSS, JS, images, etc.)
- ViewController.h/m: Main view controller
- AppDelegate.h/m: Application delegate
- SceneDelegate.h/m: Scene delegate
- SchemeHandler/: Custom URL protocol handling related files
- iOS 11.0+ (requires WKURLSchemeHandler support)
- Xcode 12.0+
- Clone or download the project code
- Open WebShell.xcodeproj with Xcode
- Build and run the project
- The app will load the sample page (Justice League theme)
To use this project as a foundation for your own application:
- Replace the HTML resources in the JL directory with your own web content
- Modify the URL loading method in ViewController.m as needed
- If you need to change the custom protocol name, modify all occurrences of "QuantumLink"
- The custom protocol handler is only available in iOS 11.0 and above
- Local resources must be included in the application bundle to be loaded correctly
- Ensure that relative paths in HTML resources correctly reference other resources (CSS, JS, images, etc.)