Skip to content

Instantly share code, notes, and snippets.

@buranmert
Created November 4, 2013 15:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buranmert/7304047 to your computer and use it in GitHub Desktop.
Save buranmert/7304047 to your computer and use it in GitHub Desktop.
In-page navigation detection UIWebView
//
// WVViewController.m
// WebViewTrial
//
// Created by Mert Buran on 11/4/13.
// Copyright (c) 2013 Mert Buran. All rights reserved.
//
#import "WVViewController.h"
@interface WVViewController () <UIWebViewDelegate, NSURLConnectionDataDelegate>
@property (nonatomic, retain) NSURLResponse *response;
@property (nonatomic, retain) NSData *data;
@end
@implementation WVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.webView setDelegate:self];
NSString *url = @"http://en.wikipedia.org/wiki/Takeru_Kobayashi";
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
self.data = [NSData data];
[connection start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked && request.URL.fragment != nil)
{
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
return NO;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.response = response;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableData *oldData = [NSMutableData dataWithData:self.data];
[oldData appendData:data];
self.data = oldData;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.webView loadData:self.data MIMEType:self.response.MIMEType textEncodingName:@"utf-8" baseURL:self.response.URL];
self.data = [NSData data];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment