UIWebView won't display at all


#1

This question is not about the Nerdfeed program but it is relevant in that I used this chapter as a guide for creating a UIWebView in my own program. I’m not sure it’s allowed to post questions about our own programs but I am growing desperate and am giving it a shot anyways. :frowning:

I have spent a week just trying to display an image from a URL (successfully pulled from an XML earlier in the code) by itself in a view, but no method of displaying images or setting a UIImage has worked for me, so I decided to try displaying it in a UIWebView. However, the web view is not displaying at all despite the fact that Xcode gives me no errors. I think the problem lies in the fact that I am trying to display the WebView within connectionDidFinishLoading because I cannot even successfully push a new view controller with the navigation controller in that else if statement, but everything I’ve tried works when I place them same code in viewDidLoad. I feel like this should be really simple and maybe I’m missing something so I would seriously appreciate help on this. Thank you in advance.

- (void)fetchDataInitial
{
    NSLog(@"BEGIN fetchDataInitial:");
    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc] init];

    NSURL *url = [NSURL URLWithString:@"http://giovanni.gsfc.nasa.gov/daac-bin/giovanni/service_manager.pl?location=GSFC&data=AERONET_AOD_L2.2:AOD0550intrp:mean[nval%20%3E=%202],MYD04_L2.051:AOD0550corr-l:mean[nval%20%3E=%205,QAavg-l%20=%203,None]&starttime=2003-01-01&endtime=2003-06-30T23:59:59Z&ardFlag=0&service=AEROSTAT_TIME_SERIES&portal=AEROSTAT"];

    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Create a connection that will exchange this request for data from the URL
    connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    NSLog(@"END fetchDataInitial");
}


- (void)fetchDataFinal 
{
    NSLog(@"SLEEP");
    [NSThread sleepForTimeInterval:5];
    NSLog(@"WAKE UP");

    NSLog(@"BEGIN fetchDataFinal");
    xmlData = [[NSMutableData alloc] init];

    NSString *urlString = [NSString stringWithFormat:@"http://giovanni.gsfc.nasa.gov/daac-bin/giovanni/service_manager.pl?session=%@&resultset=%@&result=%@", sessionID, resultSetID, resultID];
    NSLog(@"The URL is %@", urlString);

    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    NSLog(@"END fetchDataFinal");
}

// This method will be called several times as the data arrives
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
    NSLog(@"BEGIN connection:didReceiveData");
    // Add the incoming chunk of data to the container we are keeping
    // The data always comes in the correct order
    [xmlData appendData:data];
    NSLog(@"END connection:didReceiveData");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
    NSLog(@"BEGIN connectionDidFinishLoading");
    // Log XML to the console
    NSString *xmlCheck = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
    NSLog(@"xmlCheck = %@", xmlCheck);

    // Create the parser object with the data received from the web service
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];

    // Give it a delegate
    [parser setDelegate:self];

    // Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of its delegate messages sent to it before this line finishes execution - it is blocking
    [parser parse];

    // Get rid of the XML data as we no longer need it
    xmlData = nil;

    // and the connection
    connection = nil;

    if ([code isEqual:@"0"] && ![percentComplete isEqual:@"100"]) {
        [self fetchDataFinal];
    } else {
        if ([code isEqual:@"1"]) {
            UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error processing request" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [errorMessage show];
        } else if ([percentComplete isEqual:@"100"]) {

            WebViewController *wvc = [[WebViewController alloc] init];

            [[self navigationController] pushViewController:wvc animated:YES];

            NSURL *url = [NSURL URLWithString:@"http://www.earthlink.net/"];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            [[wvc webView] loadRequest:request];

            [[wvc navigationItem] setTitle:@"Back to Data Selection"];

            NSLog(@"Reached percentComplete");
        }
    }
    NSLog(@"END connectionDidFinishLoading");
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"BEGIN parser:didStartElement");
    NSLog(@"%@ found a %@ element", self, elementName);
    if ([elementName isEqual:@"session"]) {
        sessionID = [attributeDict objectForKey:@"id"];
        NSLog(@"SessionID: %@", sessionID);
    } else if ([elementName isEqual:@"resultset"]) {
        resultSetID = [attributeDict objectForKey:@"id"];
        NSLog(@"ResultSetID: %@", resultSetID);
    } else if ([elementName isEqual:@"result"]) {
        resultID = [attributeDict objectForKey:@"id"];
        NSLog(@"ResultID: %@", resultID);
    } else if ([elementName isEqual:@"code"]) {
        currentString = [[NSMutableString alloc] init];
        [self setCode:currentString];
    } else if ([elementName isEqual:@"percentComplete"]) {
        currentString = [[NSMutableString alloc] init];
        [self setPercentComplete:currentString];
    }

    if ([percentComplete isEqual:@"100"]) {
        if ([elementName isEqual:@"src"]) {
            currentString = [[NSMutableString alloc] init];
            [self setImgURL:currentString];
        }
    }
    NSLog(@"END parser:didStartElement");
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)str
{
    NSLog(@"BEGIN parser:foundCharacters");
    [currentString appendString:str];
    NSLog(@"END parser:foundCharacters");
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"BEGIN parser:didEndElement");
    currentString = nil;
    NSLog(@"END parser:didEndElement");
}

It may also be helpful to see my WebViewController method:

@implementation WebViewController


- (void)loadView 
{
    // Create an instance of UIWeb View as large as the screen
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:screenFrame];
    // Tell web view to scale web content to fit within bounds of webview
    [wv setScalesPageToFit:YES];
    
    [self setView:wv];
}

- (UIWebView *)webView
{
    return (UIWebView *)[self view];
}

@end