Objective C write to Remote URL

2019-08-27 17:29发布

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条回答
叛逆
2楼-- · 2019-08-27 18:02

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

查看更多
登录 后发表回答