Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jshmrsn

macrumors member
Original poster
Jul 26, 2008
43
0
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. http://www.chalkboardapp.com/site/code_download/code_download.php
 

DenNukem

macrumors member
Jul 12, 2008
31
0
Seattle
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];
 

jshmrsn

macrumors member
Original poster
Jul 26, 2008
43
0
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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

rafaelfaria

macrumors newbie
Aug 12, 2008
1
0
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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?

Not yet. Once 2.1 is released and the push framework is opened up to third part developers there will be...
 

jsnuff1

macrumors 6502a
Oct 4, 2003
730
340
NY
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.
 

jsnuff1

macrumors 6502a
Oct 4, 2003
730
340
NY
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.
 

jsnuff1

macrumors 6502a
Oct 4, 2003
730
340
NY
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.