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

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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
 

Attachments

  • Screen Shot 2012-12-12 at 22.12.29.jpg
    Screen Shot 2012-12-12 at 22.12.29.jpg
    62.6 KB · Views: 98
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
What do you expect?
What have you tried?

A single method name isn't good enough. Show us your implementation.
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
What do you expect?
What have you tried?

A single method name isn't good enough. Show us your implementation.
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;
    }
}
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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.
Where should I put a real baseURL?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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?
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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?
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.