How can I send an NSString
from the iPhone to a remote php script?
Asked
Active
Viewed 169 times
-1

jscs
- 63,694
- 13
- 151
- 195

Rory Lester
- 2,858
- 11
- 49
- 66
1 Answers
2
NSString *urlStr = @"http://host.com/script.php?text=This+is+some+text";
NSURL *url = [NSURL URLWithString:urlStr];
NSString *response = [NSString stringWithContentsOfURL:url];
NSLog(@"The server responded: %@", response);
-
Be sure to encode the parameter, otherwise any invalid-char in your string will cause an invalid URL. See http://stackoverflow.com/questions/8088473/url-encode-a-nsstring – Jelle De Laender Aug 19 '12 at 14:08
-
Also note that an URL got a limited length accepted by servers, so be sure your parameter/string is not to long. – Jelle De Laender Aug 19 '12 at 14:09
-
it's a better solution to send data/strings as a POST-request. More info at http://stackoverflow.com/questions/5537297/ios-how-to-perform-a-http-post-request . At server-side, you can access the objects with $_POST['key'] – Jelle De Laender Aug 19 '12 at 14:10
-
@JelleDeLaender it may be better (less prone to hit GET-parameter limitations) but then again, it is far more complicated. – Till Aug 19 '12 at 14:46