How to request HTML page with "Loading..."

Discussion in 'iOS Programming' started by mikezang, Dec 12, 2012.

  1. mikezang, Dec 12, 2012
    Last edited: Dec 12, 2012

    mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I am using NSURLCoeection to request HTML page, when I show it in UIWebView, the main contents can't be loaded and page is showing "読み込み中...", you can check attachment though there is Japanese for "Loading...". I checked source, there is somethings as below:
    Code:
    <div id="Tables"><div class="load_data"><span>読み込み中</span></div></div>
    
    I tried to use code as below to redirect, but it looks like no redirect happened, what can I do next?
    Code:
    - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
    
     

    Attached Files:

  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    What do you expect?
    What have you tried?

    A single method name isn't good enough. Show us your implementation.
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Well , I request as below
    Code:
    	NSURL *url = [NSURL URLWithString:[self prefixForURLString:_htmlUrl.text]];
    	NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
    	_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    Then I implemented delegation as below, that's all!
    Code:
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        _htmlData = [[NSMutableData alloc] initWithData:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        [_htmlData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        NSLog(@"");
        
        NSString *error_str = [error localizedDescription];
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"RequestError"
                                  message:error_str
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSString *htmlString = [[NSString alloc]
                        initWithData:_htmlData
                        encoding:NSUTF8StringEncoding;
        
        [_htmlSource loadHTMLString:htmlString baseURL:nil];
    }
    
    - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
    {
        if (redirectResponse) {
            NSMutableURLRequest *r = [request mutableCopy]; // original request
            [r setURL: [request URL]];
            return r;
        }
        else {
            return request;
        }
    }
    
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The web page probably has relative links inside it and you don't set the baseURL so the web view doesn't know where to look for those assets.

    Either just send the original link to the web view and let it load the page or you need to set the baseURL.
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Where should I put a real baseURL?
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Code:
    [_htmlSource loadHTMLString:htmlString baseURL:nil];
     
  7. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    That is no problems, my question is not HTML Web Viewer but HTML source view.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    You download a web page's html into a string and then ask the webview to load the html string. If there are relative paths in the html how will the web view know what web site to use to find the resources at those relative paths?

    For example if I look at the page source for the main page of this forum I see html code like this

    Code:
    <a href="subscription.php?do=addsubscription&f=135" rel="nofollow">Subscribe to This Forum</a>
    How will the web view know what web site is being referenced without the baseURL?
     
  9. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #9
    This is what I want to know, I am interesting in HTML source itself because I want parse it, so that I use NSURLConnection to get it, I hope that I can get all source like it show in browser, but I can only get part of source, I thought maybe there is redirect but it seems no redirect happened.

    My question is how can I get whole source including JavaScript, redirect and anything else, just like what I viewed in browser, how can I do? May I make sense?
     

Share This Page