Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Rhalliwell1

macrumors 6502a
Original poster
May 22, 2008
588
1
...Maybe someone could check this for me.

Just trying to get the basics of a simple UIWebView working....

I got this:

Code:
@interface BookmarkMiniViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webPage;
}

@property(nonatomic, retain) UIWebView *webPage;

Code:
@synthesize webPage;
- (void)viewDidLoad {
//web view
	webPage = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 120)];
	[webPage setDelegate:self];
	NSURL *pageURL = [[NSURL alloc] initWithString:@"http://www.google.com/"];
	[webPage loadRequest:[NSURLRequest requestWithURL:pageURL]];
	webPage.scalesPageToFit = YES;
	webPage.userInteractionEnabled = YES;

[self.view addSubview:webPage];
}

Code:
-(void)webViewDidFinishLoad:(UIWebView *)wView {
	NSLog(@"Finished\n");
}

-(void)webViewDidStartLoad:(UIWebView *)wView {
	NSLog(@"Started\n");
}
-(void)webView:(UIWebView *)wView didFailLoadWithError:(NSError *)error {
	NSLog(@"Error\n");
}

Then when i run it the web view loads fine but i dont get any output in the console for webViewDidStartLoad, webViewDidFinishLoad or webView:didFailLoadWithError

Anyone know why? :S
 
That is pretty odd. Try instead to load the request after you add the webview as a subview and see. Don't know if that makes a difference though.
 
Unfortunately that did not work.

I have a view with a webview in, that one works fine. when a button is pressed that web view is removedFromSuperView and this view is added as a subview of the Window which in turn loads this web view.

Like i said the first webView works fine, and i cannot see any difference in the code. Totally confused by it!
 
btw, [super viewDidLoad] is missing at the beginning of your viewDidLoad.

I didn't include the complete code above although at the moment the rest of the code is commented out.

The call to super is at the bottom. I thought it was meant to go last.. Does it matter?
 
There is some omitted code.

I didn't want to paste code that had nothing to do with my query.
 
To my understanding weather or not it is declared as a property shouldn't effect the problem i am facing?
 
For example, this line:
Code:
webPage = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 120)];
It is not using the setter.

Doesn't make any difference. Just because you declare a setter property doesn't mean you have to use it. Just means you have to be a lot more careful about tracking your memory retain counts and releases/leaks.

But the OP's question is about delegates, not properties.
 
Which the OP isn't.

How do you know? From what i have posted there is no complex allocations or references so a simple autorelease or release in dealloc should suffice, especially considering the scale of the application and the memory i have available.

Do you have any ideas on how to solve my problem or are you just here to scrutinise my code?
 
an update:

Code:
[webPage.delegate webViewDidStartLoad:webPage];
	[webPage.delegate webViewDidFinishLoad:webPage];
	[webPage.delegate webView:webPage didFailLoadWithError:nil];

Does produce outputs in terminal.

I'm using SDK 4.0 beta... could it be a bug??
 
Do you have any ideas on how to solve my problem or are you just here to scrutinise my code?
And how do you know that your problems are being caused by the code I'm scrutinizing?

an update:

Code:
[webPage.delegate webViewDidStartLoad:webPage];
	[webPage.delegate webViewDidFinishLoad:webPage];
	[webPage.delegate webView:webPage didFailLoadWithError:nil];

Does produce outputs in terminal.

I'm using SDK 4.0 beta... could it be a bug??
So, the original concern was that you weren't getting outputs from these methods. Now you are. So, the problem now is what, exactly?
 
The original problem is still there. It is no good me calling them, i need them to be called by UIWebView, as they should do.
 
Ok, I've built a very simple project using the code you provided in your original post. Here's the output:

[Session started at 2010-05-21 09:09:18 -0600.]
2010-05-21 09:09:25.085 BookmarkMini[80153:207] Started
2010-05-21 09:09:26.010 BookmarkMini[80153:207] Finished


So, I would guess the issue lies somewhere in the code you have not provided.
 
Full code, everything other than what i posted commented out:

Code:
#import <UIKit/UIKit.h>

@interface BookmarkMiniViewController : UIViewController <UIWebViewDelegate> {
	UIWebView *webPage;
	
}
//@property(nonatomic, retain) UIWebView *webPage;

@end

Code:
#import "BookmarkMiniViewController.h"
#import "QuartzCore/CALayer.h"

@implementation BookmarkMiniViewController
//@synthesize webPage;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	/*[self.view setBackgroundColor:[UIColor clearColor]];
	//name label
	UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 120, 80, 20)];
	nameLabel.backgroundColor = [UIColor clearColor];
	nameLabel.textColor = [UIColor grayColor];
	nameLabel.textAlignment = UITextAlignmentCenter;
	nameLabel.text = @"Google";
	
	//image
	NSString *imageLocation = [[NSString alloc] initWithString:@"http://www.google.com/favicon.ico"];
	UIWebView *imageView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 120, 16, 16)];
	NSURL *imageURL = [[NSURL alloc] initWithString:imageLocation];
	[imageView loadRequest:[NSURLRequest requestWithURL:imageURL]];
	imageView.userInteractionEnabled = NO;*/
	
	//web view
	webPage = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 120)];
	[webPage setDelegate:self];
	NSURL *pageURL = [[NSURL alloc] initWithString:@"http://www.google.com/"];
	[webPage loadRequest:[NSURLRequest requestWithURL:pageURL]];
	[webPage.delegate webViewDidStartLoad:webPage];
	[webPage.delegate webViewDidFinishLoad:webPage];
	[webPage.delegate webView:webPage didFailLoadWithError:nil];
	webPage.scalesPageToFit = YES;
	webPage.userInteractionEnabled = YES;
	
	/*
	//convert to image
	UIGraphicsBeginImageContext(webPage.bounds.size);
	[webPage.layer drawInContext:UIGraphicsGetCurrentContext()];
	UIImage *webPageImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	UIImageView *webPageImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 130)];
	webPageImageView.image = webPageImage;
	webPageImageView.contentMode =  UIViewContentModeScaleToFill;
	*/
	
	[self.view addSubview:webPage];

	[webPage.delegate webViewDidStartLoad:webPage];
	[webPage.delegate webViewDidFinishLoad:webPage];
	[webPage.delegate webView:webPage didFailLoadWithError:nil];
/*
	//[self.view addSubview:imageView];
	[self.view addSubview:nameLabel];
	
	[nameLabel release];
	//[webPage release];
	//[webPageImage release];
	//[webPageImageView release];
	[imageURL release];
	[pageURL release];
	[imageLocation release];
	[imageView release];	
	*/
	
	[super viewDidLoad];
}



-(void)webViewDidFinishLoad:(UIWebView *)wView {
	NSLog(@"Finished\n");
}

-(void)webViewDidStartLoad:(UIWebView *)wView {
	NSLog(@"Started\n");
}
-(void)webView:(UIWebView *)wView didFailLoadWithError:(NSError *)error {
	NSLog(@"Error\n");
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

/*
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
*/

/*
- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
*/

- (void)dealloc {
	webPage.delegate = nil;
	[webPage release];
    [super dealloc];
}


@end
 
Latest output based on latest code:

[Session started at 2010-05-21 09:25:52 -0600.]
2010-05-21 09:25:54.544 BookmarkMini[80305:207] Started
2010-05-21 09:25:54.555 BookmarkMini[80305:207] Finished
2010-05-21 09:25:54.556 BookmarkMini[80305:207] Error
2010-05-21 09:25:54.557 BookmarkMini[80305:207] Started
2010-05-21 09:25:54.565 BookmarkMini[80305:207] Finished
2010-05-21 09:25:54.566 BookmarkMini[80305:207] Error
2010-05-21 09:25:54.573 BookmarkMini[80305:207] Started
2010-05-21 09:25:55.163 BookmarkMini[80305:207] Finished


First two sets of Started, Finished, Error is from your explicit calls to those delegate methods. Last set of Started, Finished is from the actual webView calling those delegate methods.
 
3.2. Maybe it is a 4.0 bug. Perhaps put together a very simple project to test under 4.0 and, if it's still an issue, submit a bug report to Apple.

Well i am still on beta 1 so been downloading beta 4 all day... nearly finished. Will see if that solves it. I'm glad i'm not going mad... I had a feeling this was going to be something simple i had overseen. :p
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.