views:

108

answers:

2
Q: 

Connection

Hello,

I would like to ask you about NSURLConnection in objective-c for iPhone.

I have one app that needs to connect to one webservice to receive data (about YouTube videos), Then I have all the things that I need to connect (Similar to Hello_Soap sample code in the web).

But now, my problem is that I create a class (inherits from NSObject) named Connection and I have implemented the methods: didReceiveResponse, didReceiveData, didFailWithError and connectionDidFinishLoading. Also the method:

-(void)Connect:(NSString *) soapMessage{
    NSLog(soapMessage);

    NSURL *url = [NSURL URLWithString:@"http://....."];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];


    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
}

But when from my AppDelegate I create one Connection object:

Connection * connect = [[Connection alloc] Init:num]; //It's only one param to test.
[connect Connect:method.soapMessage];

And I call this method, when this finishes, it doesn't continue calling didReceiveResponse, didReceiveData, didFailWithError or connectionDidFinishLoading. I'm trying to do this but I can't for the moment.

The thing I would like to do is to be able to call this class "Connection" each time that I want to receive data (after that to be parsed and displayed in UITableViews).

Thank you.

A: 

A starting point I would explore is to not name your class "Connect". There are no name spaces in Objective-C, and with a name as generic as "Connect" it's possible other classes of the same name are causing you issues.

The convention is to prepend a few characters related to the project or your company, so for instance if your project is named Amazing Thing you might name the class "ATConnector" instead. You can highlight the class name in the .h file and use the "Refactor" menu command to change it easily while fixing other references too.

Also if you were releasing either the Connect class or the NSURLconnection after calling, that would have the effect you are seeing of no delegate methods getting called - but that's hard to determine from the code present.

Kendall Helmstetter Gelner
It's not necessary to use prefixes unless you're dealing with outside code. The only Apple classes without prefixes are the core Objective-C language classes like Object and Protocol.
Chuck
A: 

how do you manage to connect to youtube with iphone sdk ?

I want to retreive private video of a user and i try to use youtube api for objective C. and i doesn't manage to connect. how do you do ?

Sly33