[英]iOS how can I add a completion block to UIWebView loadRequest:?
I'm working with a UIWebView and am already using webViewDidFinishLoad: method with an optional block that gets executed after loading complete: 我正在使用UIWebView,并且我已经在使用带有可选块的webViewDidFinishLoad:方法,该块在加载完成后执行:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[super webViewDidFinishLoad:webView];
//... bunch of other code
if(self.webViewFinishLoadBlock != nil)
{
self.webViewFinishLoadBlock();
}
}
Now I'm working with an even more complicated sequence of loading pages and redirects that makes the logic above not sufficient. 现在我正在使用更复杂的加载页面和重定向序列,这使得上面的逻辑不够充分。 I don't want to register myself as a delegate of dummyWebView and have to juggle multiple completion blocks stored in my view controller's properties:
我不想将自己注册为dummyWebView的委托,并且必须处理存储在我的视图控制器属性中的多个完成块:
dummyWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
[dummyWebView loadRequest:[NSURLRequest requestWithURL:logoutURL]];
//Ideally here I would know when dummyWebView finishes loading, because there's some code I want to execute once it is done
My question is: 我的问题是:
Is there some kind of third party framework that would allow me to use loadRequest:withCompletion:
to simplify writing callback code? 是否有某种第三方框架允许我使用
loadRequest:withCompletion:
简化编写回调代码?
You can just: 你可以:
Subclass UIWebView
with a property to hold the webViewDidFinish
completion block; 具有属性的子类
UIWebView
用于保存webViewDidFinish
完成块;
Make sure it specifies its delegate; 确保它指定了其委托;
Implement the webViewDidFinish
much like you wrote it (though I'd suggest the block return both the web view as well as the NSError
object, if any); 实现
webViewDidFinish
就像你写的那样(尽管我建议块返回web视图以及NSError
对象,如果有的话); and 和
Implement the webView:didFailLoadWithError:
, too. 实现
webView:didFailLoadWithError:
.
Thus: 从而:
// MyWebView.h
#import <UIKit/UIKit.h>
typedef void(^WebViewFinishLoadBlock)(UIWebView *, NSError *);
@interface MyWebView : UIWebView
@property(nonatomic, copy) WebViewFinishLoadBlock webViewFinishLoadBlock;
- (void)loadRequest:(NSURLRequest *)request withCompletionHandler:(WebViewFinishLoadBlock)completionHandler;
@end
And 和
// MyWebView.m
#import "MyWebView.h"
@interface MyWebView () <UIWebViewDelegate>
@end
@implementation MyWebView
- (void)loadRequest:(NSURLRequest *)request withCompletionHandler:(WebViewFinishLoadBlock)completionHandler
{
self.delegate = self;
self.webViewFinishLoadBlock = completionHandler;
[self loadRequest:request];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (self.webViewFinishLoadBlock) {
self.webViewFinishLoadBlock(webView, nil);
self.webViewFinishLoadBlock = nil;
}
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if (self.webViewFinishLoadBlock) {
self.webViewFinishLoadBlock(webView, error);
self.webViewFinishLoadBlock = nil;
}
}
@end
And then: 接着:
MyWebView *webView = [[MyWebView alloc] init];
webView.frame = self.view.bounds;
[self.view addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request withCompletionHandler:^(UIWebView *webView, NSError *error) {
if (error) {
NSLog(@"failed: %@", error);
} else {
NSLog(@"succeeded");
}
}];
- (void)webViewDidFinishLoad:(UIWebView *)webView
is a delegate method. 是一种委托方法。 By convention delegate methods require the object pass itself back to the delegate:
按照惯例,委托方法要求对象将自身传递回委托:
(UIWebView*)webView
Through a parameter. 通过参数。
If we want to get last request parameter using property request:
that means webView.request.URL
如果我们想使用属性
request:
获取最后一个请求参数request:
这意味着webView.request.URL
The parent object can be the delegate for multiple objects, and it can identify which it is getting a response from though that parameter. 父对象可以是多个对象的委托,它可以识别从该参数获得响应的对象。 Either switch on what responds to you or build some infrastructure to handle it more elegantly.
要么打开响应你的东西,要么建立一些基础设施来更优雅地处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.