0

I am looking for anything like .NET WebClient: Where is DownloadString? on XCode.

Just one URL to send and one String to get. I have working solution on iPhone and one on Mac Project but on iPad it does not work.

I found https://github.com/pokeb/asi-http-request but it throws errors in XCode 4.1 about release is not supported.

Any suggestions to download website on iPad project with HighLevel access?

What class an be used in case of ASIHTTPRequest?

-(NSString*) DownloadString:(NSString *)UrlString;
{

    NSURL *url = [NSURL URLWithString:UrlString];

    // Create a request
    ASIHTTPRequest* myRequest = [ASIHTTPRequest requestWithURL:url];

    // Start the request
    [myRequest startSynchronous];

    // Request has now finished
    NSString *Result = [myRequest responseString];

    return Result;
}

* SOLUTION (thanks to Francesco) *

NSString *UrlTargetString = @"http://www.goldengel.ch";
NSURL *UrlTarget = [NSURL URLWithString: UrlTargetString];


NSError* error = nil;
NSString* text = [NSString stringWithContentsOfURL:UrlTarget encoding:NSASCIIStringEncoding error:&error];
if( text )
{
    NSLog(@"Text=%@", text);
}
else 
{
    NSLog(@"Error = %@", error);
}

Response / Log is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>....</html>
Community
  • 1
  • 1
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86
  • It seems ARC should be disabled to build that library. – michex Mar 26 '12 at 18:25
  • Is ASIHTTPRequest (http://allseeing-i.com/ASIHTTPRequest/How-to-use) the sort of thing you are after? – Paul Jowett Mar 27 '12 at 06:42
  • I've now checked for http://stackoverflow.com/questions/1140341/dylib-linked-in-debug-not-found-for-release-for-iphone-in-xcode because of header errors on HTTP Includes and also I've checked http://stackoverflow.com/questions/6692022/why-cant-i-release-an-object-anymore this item but I could not get it work because it seems to be older XCode 3. It is still horrible to just download an simple string from my website. On Microsoft SDK that takes 30 seconds, not on Apple SDK I am there for 3 days and it still does not work. – Nasenbaer Mar 29 '12 at 10:30
  • @jowierun: I've updated my question with iPad project as solution. Perhaps that is the problem? – Nasenbaer Mar 30 '12 at 12:08
  • @michex: what and where is ARC? – Nasenbaer Mar 30 '12 at 12:09

2 Answers2

2

ASIHTTPRequest is an outdated framework (the developer has stated that he will not work on it anymore), and does not support ARC. ARC (Automatic Reference Counting) is a new feature provided in SDK5 that automatically does all the retain/release work for you. This, of course, requires that you don't use retain/release yourself, which ASIHTTPRequest does. Therefore your errors.

To disable ARC in files, use the -fno-objc-arc flag, there are plenty of tutorials about this on the internet and on Stack Overflow. Using this, you can still use ASIHTTPRequest for your current projects, though this is not recommended. For example, see How can I disable ARC for a single file in a project? - and do this for all ASIHTTPRequest files.

New frameworks include AFNetworking and RestKit and MKNetworkKit.

Community
  • 1
  • 1
JiaYow
  • 5,207
  • 3
  • 32
  • 36
  • Hi JiaYow. I fully thank you for your very quick and good answer. Because I've tested the methode of Francesco and it works without installing and configuring anything, I love to see simple way. – Nasenbaer Mar 30 '12 at 12:43
2

You can also use the NSString stringWithContentsOfURL:encoding:error: method.

It is synchronous so you can use dispatch_async to use it from the main thread.

dispatch_queue_t httpQueue = dispatch_queue_create(httpQueueName, NULL);
        dispatch_async(httpQueue, ^{
            NSString *response = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 
//update UI
dispatch_async(dispatch_get_main_queue(), ^{
//code...
});
            });

EDIT: I updated the answer: if you need to update some UI component, remember to do it inside the main thread (so you can use dispatch_async on the main queue)

Francesco
  • 1,840
  • 19
  • 24