问题
Ok, so I have the correct username / pass for a remote host, and I have a NSURL
Object referring to the file on the server.
How can I possibly write data to that server via Objective-C? I cannot find anyone with this problem, so you help would be greatly appreciated!
回答1:
I do it all the time with the program I am writing. The answer depends on what server you have installed and what languages it supports. If, as I think, you have PHP, just send a GET request to your server and instruct the PHP to write the data it receives.
This is a function of mine:
function checkInfo() {
$username = $_GET['username'];
$password = $_GET['password'];
connectToDataBase();
return returnSuccessObjectOn(isLoginInfoValid($username, $password));
}
On the client side:
NSURL *url = [NSURL URLWithString:kRequestLink];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:TIMEOUT];
OARequestParameter *reqCodeParameter = [[OARequestParameter alloc] initWithName:kParamReqCode value:[NSString stringWithFormat:@"%d",reqCode]];
[par addObject:reqCodeParameter];
[data enumerateKeysAndObjectsUsingBlock:composeRequestBlock];
[req setParameters:par];
NSLog(@"Sendind GET request: %@", [req description]);
activeConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
As you can see I use blocks
来源:https://stackoverflow.com/questions/3935560/objective-c-write-to-remote-url