Reputation: 931
I'm setting up a webview programmatically, and everything works until I actually set the delegate.
#import <UIKit/UIKit.h>
@interface MainPageViewController : UIViewController <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webViewForStories;
@end
- (void)viewDidLoad
{
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)];
NSString *urlAddress = @"http://myurl.com/mypage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
webViewForStories = [[UIWebView alloc] initWithFrame:CGRectMake(0, webView.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
NSString *urlAddressForStories = @"http://myurl.com/mysecondpage";
NSURL *urlForStories = [NSURL URLWithString:urlAddressForStories];
NSURLRequest *requestObjForStories = [NSURLRequest requestWithURL:urlForStories];
[webViewForStories loadRequest:requestObjForStories];
[self.view addSubview:webViewForStories];
[webViewForStories setDelegate:self];
NSLog(@"webviewForStories delegate is %@", webViewForStories.delegate);
NSLog(@"webviewForStories is %@", webViewForStories);
}
Here's what the page looks like when I set the delegate:
Here's what the page looks like when I DON'T set the delegate:
The only difference is this line of code:
[webViewForStories setDelegate:self];
Here is the relevant output from NSLog:
2012-12-26 14:18:40.720 WGGB for iPad[3144:907] webviewForStories delegate is <MainPageViewController: 0x1f878cc0>
2012-12-26 14:18:40.721 WGGB for iPad[3144:907] webviewForStories is <UIWebView: 0x1f8838c0; frame = (0 300; 768 1004); layer = <CALayer: 0x1f883920>>
This is a mystery, any help is appreciated. FYI, I tried commenting out the first webview, I thought they might be conflicting, but it didn't make a difference.
EDIT:
Here are my delegate methods:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"webview finished loading");
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webview started loading");
}
// This function allows me to intercept links clicked in a webview and open in
// a navigation window with a back button.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(@"urlstring is %@", urlString);
// Only catching links without "mobile" in the url (i.e. stories)
if ([urlString rangeOfString:@"/mobile"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound &&[urlString rangeOfString:@"twitter"].location == NSNotFound && [urlString rangeOfString:@"fb://"].location == NSNotFound && [urlString rangeOfString:@"liverail"].location == NSNotFound && [urlString rangeOfString:@"about:blank"].location == NSNotFound) {
//DetailViewController *detailView = [[DetailViewController alloc] init];
//[self.navigationController pushViewController:detailView animated:YES];
return NO;
} else {
return YES;
}
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"error is %@", [error localizedDescription]);
}
None of them seem to be getting called.
Upvotes: 0
Views: 1909
Reputation: 931
I figured it out... the problem was with this method:
// This function allows me to intercept links clicked in a webview and open in
// a navigation window with a back button.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(@"urlstring is %@", urlString);
// Only catching links without "mobile" in the url (i.e. stories)
if ([urlString rangeOfString:@"/mobile"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound && [urlString rangeOfString:@"facebook"].location == NSNotFound &&[urlString rangeOfString:@"twitter"].location == NSNotFound && [urlString rangeOfString:@"fb://"].location == NSNotFound && [urlString rangeOfString:@"liverail"].location == NSNotFound && [urlString rangeOfString:@"about:blank"].location == NSNotFound) {
//DetailViewController *detailView = [[DetailViewController alloc] init];
//[self.navigationController pushViewController:detailView animated:YES];
return NO;
} else {
return YES;
}
}
The if statement was being caught on the initial loading of the webview so it was returning NO, which basically gave me a blank screen. I had to check if this was the initial load or not before allowing that if statement to run.
Upvotes: 1
Reputation: 5812
try setting the delegate to the property instead of the ivar.
[self.webViewForStories setDelegate:self]
to get the webviews to call the delegate methods you have implemented.
Upvotes: 0