trying to get started with iPhone to server communication.

Discussion in 'iPhone/iPad Programming' started by jshmrsn, Jul 26, 2008.

  1. macrumors member

    Joined:
    Jul 26, 2008
    #1
    Hi I posted this on Apple's forums also, but I never get responses there so I'd like to try here :)


    I'd like my app to be able to communicate with my server, as many apps do.

    Basically I have some PHP code that I'd like to run and get the output of.

    I'm guessing I can execute the code with a hidden UIWebView's loadRequest, but I don't see how you would get the output.

    I've never done this type of thing and would just like something I can start with.

    Anything will help :)

    Thanks,
    Josh


    Edit:
    e.g. www.chalkboardapp.com/site/code_download/code_download.php
     
  2. macrumors member

    DenNukem

    Joined:
    Jul 12, 2008
    Location:
    Seattle
    #2
    Start with this snippet and read up documentation on used classes:

    Code:
    	NSMutableURLRequest *rq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:nssurl]];
    	[rq setHTTPMethod: @"POST"];
    	[rq setHTTPBody: nsdataInput];
        [rq setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
    	NSError *err;
    	NSHTTPURLResponse *response;
    	NSData * nsdataReply = [NSURLConnection sendSynchronousRequest:rq returningResponse:&response error:&err];
    	if(err!=Nil)
    	{
    		NSString * str = [err localizedDescription];
    
     
  3. thread starter macrumors member

    Joined:
    Jul 26, 2008
    #3
    Thanks a bunch for the help.

    I also found this snippet which was very useful. (I happen to need GET more than POST, should have specified that)

    Code:
    
    NSString *FeedURL=@"http://www.google.com";
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FeedURL]];
    NSURLResponse *resp = nil;
    NSError *err = nil;
    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
    NSString * theString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
    
    NSLog(@"response: %@", theString);
    editorTextView.text = theString;
    
    

    Thanks again,
    Josh
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Don't use sendSynchronousRequest as it blocks the current thread. Use initWithRequest:delegate: (or similar methods) to start an asynchronous connection which runs in a separate thread.
     
  5. macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #5
    Also, sendSynchronousRequest leaks like crazy. Difficult to believe, but true.
     
  6. thread starter macrumors member

    Joined:
    Jul 26, 2008
    #6
    Oh okay, thanks.

    I was wondering how to go about async.
     
  7. macrumors newbie

    Joined:
    Aug 12, 2008
    #7
    Server to Iphone communication

    Is there a way to do the other way around? Like... the server send some message to the iphone?

    I still cant give any details what i'm thinking b to develop but it would be something like a emial client.

    I will send an email to a server, the server find my iphone and notify me that i have a message.

    Is there any way to do that?
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Not yet. Once 2.1 is released and the push framework is opened up to third part developers there will be...
     
  9. macrumors 6502

    Joined:
    Oct 4, 2003
    Location:
    NY
    #9
    I was trying to do the same thing...thanks for the info.

    So i figured out how to get the data from the server, and have the data in a String

    The data itself is a xml document that contains about 50 of the fallowing

    <item>
    <title>Data1 = (3.167963)</title>
    <link></link>
    <description></description>
    <pubDate>Tue, 12 Aug 2008 13:20:01 CST</pubDate>
    </item>
    <item>
    <title>Data2 = (2.115675)</title>
    <link></link>
    <description></description>
    <pubDate>Tue, 12 Aug 2008 13:20:01 CST</pubDate>
    </item>

    and so on

    so how do i go about retrieving the number in the parenthesis? What i will end up doing is creating an array with all 50 of these numbers, but I have no idea how to actually access a specific number in that string.
     
  10. macrumors 6502

    Joined:
    Oct 4, 2003
    Location:
    NY
    #10
    ok i have half answered my own question

    after I get the String I do this to find the specific number i need


    NSRange range = [theString rangeOfString:mad:"DATA1"];
    unichar *buffer;

    range.length = range.length + 3;
    range.location = range.location + 7;
    [theString getCharacters:buffer range:range];

    NSString *subString = [[[NSString alloc] initWithCharacters:buffer length:range.length] autorelease];


    NSLog(@"response: %@", subString);


    I see that the exact number i need is correctly stored in subString from the NSLog message, but after this the program crashes. I think im not doing something correctly with the buffer and getCharacter method.
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
  12. macrumors 6502

    Joined:
    Oct 4, 2003
    Location:
    NY
    #12
    thanks...that was easy.
     
  13. macrumors 6502

    Joined:
    Oct 4, 2003
    Location:
    NY
    #13
    Now that I got everything working ive encountered another little problem. The program works fine when there is a good internet connection. Once the internet gets spotty (especially with bad cell reception) the program crashes after trying to load data from the server.

    I was hopeing NSURLConnection would return a nil object if there was a problem with the internet connection and not bring down the whole program, but I guess it throws some sort of exception?

    It works fine when there is NO internet connection (and I have handled for this case in my program) but how do I handle a spotty internet connection. I dont even know what NSURLConnection returns in this case. I know the program crashes about 10 seconds after initilizing the connection, so maybe its some sort of timeout error?
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    You need to pass an address via the (NSError **) error parameter and check it after the call.
     

Share This Page