0

I am attempting to download a website but get only about a quarter of the entire site.

    NSString *tagiString = @"http://www.tagesanzeiger.ch";
    NSURL *tagiURL = [NSURL URLWithString:tagiString];
    NSError *error;
    NSString *text =[NSString stringWithContentsOfURL:tagiURL
                                             encoding:NSASCIIStringEncoding
                                                error:&error];

I don't receive any errors indicating the download did not complete. Any ideas?

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Pete
  • 613
  • 1
  • 6
  • 18
  • have u tried to log the text string and checked if you are getting required html string or not?? – Divyu May 31 '13 at 10:02
  • Can you elaborate on what you expect vs what you get? – Carl Veazey May 31 '13 at 10:13
  • 1
    I checked your code. I was able to fetch all data from the url. Print your text in console and check. – Nishant Tyagi May 31 '13 at 10:15
  • I logged the text string and found out that it cuts off at about 50'000 characters. The entire site is probably close to 200'000 characters. Nishant, how many characters do you get? – Pete May 31 '13 at 10:17

1 Answers1

1

I wouldn't recommend using stringWithContentsOfURL for this. Instead, you should use iOS's built-in networking classes which provide much greater control over download operations. For example, with stringWithContentsOfURL you have no way to adjust or set the timeout, which means on a slow connection your download could easily fail (that may be what's happening here).

By default stringWithContentsOfURL also isn't asynchronous, so unless you're running that code on a secondary thread you're going to halt your UI (which is bad).

This existing question - Making stringWithContentsOfURL asynchronous - Is it safe? - has some sample code showing you how to download a website as a string using the more suitable NSURLConnection class. It will make error handling and debugging much easier for you.

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83