Pete
Pete

Reputation: 613

Website Does Not Download Completely - Using stringWithContentsOfURL:

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?

Upvotes: 0

Views: 151

Answers (1)

lxt
lxt

Reputation: 31304

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.

Upvotes: 1

Related Questions