OTMWebView is a UIWebView subclass that attempts to bring some of missing features of the UIWebView to iOS.
OTMWebView is no longer being maintained, most of it's features can be found in WKWebView
- Progress tracking
- Setting user agent for all requests from the web view
- Response handling
- Document title change detection
Set up OTMWebView instance
#import <OTMWebView/OTMWebView.h>
OTMWebView *webView = [[OTMWebView alloc]initWithFrame:CGRectZero];
webView.delegate = ....;
webView.userAgent = @"My User Agent String";
OTMWebViewDelegate
#import <OTMWebView/OTMWebView.h>
-(void)webView:(OTMWebView *)webView didReceiveResponse:(NSURLResponse *)response forRequest:(NSURLRequest *)request {
if ([response.MIMEType isEqualToString:@"video/mp4"]) {
[webView stopLoading];
// Do something else...
}
}
-(void)webView:(OTMWebView *)webView documentTitleDidChange:(NSString *)title {
//Do something with new title...
}
-(void)webViewProgressDidStart:(OTMWebView *)webView {
// web view's main frame started loading a document
}
-(void)webViewProgressDidFinish:(OTMWebView *)webView {
// web view's document finished loading, as did all of its subresources
}
-(void)webView:(OTMWebView *)webView progressDidChange:(double)progress {
// Update some sort of progress indicator
}Cocoapods is the recommended way to install OTMWebView. Just add pod 'OTMWebView' to your Podfile.
Steps
- In
OTMWebView'sdelegate method:- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType- Set a property in the request with
NSURLProtocol's+ (id)propertyForKey:(NSString *)key inRequest:(NSURLRequest *)requestto tell ourNSURLProtocolsubclass to handle the request. - Attach identifier string to the request's url fragment component so we identify the
OTMWebViewinstance
- Set a property in the request with
- In the
NSURLProtocolsubclass's class method+ (BOOL)canInitWithRequest:(NSURLRequest *)requestmethod- Check if ignore property is set with
NSURLProtocol's+ (id)propertyForKey:(NSString *)key inRequest:(NSURLRequest *)request, if it isreturn NO - Check if the handle request property is set with
NSURLProtocol's+ (id)propertyForKey:(NSString *)key inRequest:(NSURLRequest *)request, if it isreturn YES - Check the
mainDocumentURLproperty of the request to see if it contains the identifier of aOTMWebViewinstance. If it isreturn YES- The
mainDocumentURLproperty of requests for subresources is set the to web view's main request url. This allows us to identify theOTMWebViewinstance.
- The
- Check if ignore property is set with
Since OTMWebView is able to intercept the web view's main requests and the the requests for its subresources, it can track the download progress for all those resources, enabling it to be able to use a similar algorithm to Webkit's to estimate the loading progress of an entire document.
OTMWebView is available under the MIT license. See the LICENSE file for more info.