UIWebView confusion.

Discussion in 'iOS Programming' started by Rhalliwell1, May 20, 2010.

  1. Rhalliwell1 macrumors 6502a

    Joined:
    May 22, 2008
    #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
     
  2. Compile 'em all macrumors 601

    Compile 'em all

    Joined:
    Apr 6, 2005
    #2
    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.
     
  3. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #3
    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!
     
  4. Compile 'em all macrumors 601

    Compile 'em all

    Joined:
    Apr 6, 2005
    #4
    btw, [super viewDidLoad] is missing at the beginning of your viewDidLoad.
     
  5. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #5
    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?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Also, you set up a property and accessors for webPage and then never use them. Why is that?
     
  7. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #7
    There is some omitted code.

    I didn't want to paste code that had nothing to do with my query.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Is this post a response to my query about why you're not using your accessors?
     
  9. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #9
    Yes it was. :)
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Then I'm not sure why you would talk about omitted code. I am talking about the code you included. For example, this line:
    Code:
    webPage = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 120)];
    It is not using the setter.
     
  11. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #11
    so you suggest i should remove the @property and @synthesize lines?
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Depends on how you plan on using webPage.
     
  13. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #13
    To my understanding weather or not it is declared as a property shouldn't effect the problem i am facing?
     
  14. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #14
    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.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Which the OP isn't.
     
  16. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #16
    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?
     
  17. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #17
    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??
     
  18. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    And how do you know that your problems are being caused by the code I'm scrutinizing?

    So, the original concern was that you weren't getting outputs from these methods. Now you are. So, the problem now is what, exactly?
     
  19. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #19
    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.
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    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.
     
  21. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #21
    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
    
     
  22. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #22
    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.
     
  23. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
  24. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #24
    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.
     
  25. Rhalliwell1 thread starter macrumors 6502a

    Joined:
    May 22, 2008
    #25
    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
     

Share This Page