I saw the UIRefreshControl in iOS 6 and my question is if it is possible to refresh a WebView by pulling it down and than let it pop up like in mail? Code I used rabih is the We
This is how you can use pull down to refresh on UIWebview:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Make webView a delegate to itself
// I am going to add URL information
NSString *fullURL = @"http://www.umutcankoseali.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
_webView.delegate = (id)self;
[_webView loadRequest:requestObj];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[_webView.scrollView addSubview:refreshControl]; //<- this is point to use. Add "scrollView" property.
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
NSString *fullURL = @"http://www.umutcankoseali.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
[refresh endRefreshing];
}
I've just added this very quickly to my code:
[webserver.scrollView addSubview:refreshControl]
So it seems that the UIWebView
has itself a UIScrollView
you can just attach to. Hope it's useful for you.
I've actually tried that and got error following.
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIRefreshControl may only be managed by a UITableViewController'
I could use in UIScrollView and UICollectionView though.
In Swift use this,
Let's assume you wants to have pull to refresh in WebView,
So try this code:
override func viewDidLoad() {
super.viewDidLoad()
addPullToRefreshToWebView()
}
func addPullToRefreshToWebView(){
var refreshController:UIRefreshControl = UIRefreshControl()
refreshController.bounds = CGRectMake(0, 50, refreshController.bounds.size.width, refreshController.bounds.size.height) // Change position of refresh view
refreshController.addTarget(self, action: Selector("refreshWebView:"), forControlEvents: UIControlEvents.ValueChanged)
refreshController.attributedTitle = NSAttributedString(string: "Pull down to refresh...")
YourWebView.scrollView.addSubview(refreshController)
}
func refreshWebView(refresh:UIRefreshControl){
YourWebView.reload()
refresh.endRefreshing()
}
In Objective-C, I used this:
- (void)addPullToRefreshToWebView{
UIColor *whiteColor = [UIColor whiteColor];
UIRefreshControl *refreshController = [UIRefreshControl new];
NSString *string = @"Pull down to refresh...";
NSDictionary *attributes = @{ NSForegroundColorAttributeName : whiteColor };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
refreshController.bounds = CGRectMake(0, 0, refreshController.bounds.size.width, refreshController.bounds.size.height);
refreshController.attributedTitle = attributedString;
[refreshController addTarget:self action:@selector(refreshWebView:) forControlEvents:UIControlEventValueChanged];
[refreshController setTintColor:whiteColor];
[self.webView.scrollView addSubview:refreshController];
}
- (void)refreshWebView:(UIRefreshControl*)refreshController{
[self.webView reload];
[refreshController endRefreshing];
}
Right now I don't believe it is. You actually can't use it with just any UITableView. The tableView needs to be party of a UITableViewController to be used properly.
That said, its possible you might be able to get away with sticking one above your UIWebView and controlling its frame and values manually. Things that the UITableViewController has been updated to do on its own with its refreshControl property.
Extending @umut-can-köseali answer to have better control of the UIRefreshControl when refresh ends:
- (void)viewDidLoad {
[super viewDidLoad];
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(handleWebViewRefresh:) forControlEvents:UIControlEventValueChanged];
[self.webView.scrollView addSubview:_refreshControl]; //<- this is point to use. Add "scrollView" property.
}
- (void)webView:(UIWebView *)_webView didFailLoadWithError:(NSError *)error {
[_refreshControl endRefreshing];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
NSString *navigatorUrl = _webView.request.URL.absoluteString;
if( navigatorUrl && ![navigatorUrl isEqualToString:@""]) {
NSString *host=_webView.request.URL.host;
NSString *path=_webView.request.URL.path;
[_refreshControl endRefreshing];
}
}