我在哪里将此代码放在Xcode中

时间:2016-12-11 14:46:17

标签: php ios xcode

我正在尝试在回答此链接时实现代码

Upload File to FTP Server on iPhone

我知道这段代码会进入我服务器网站的php文件

<?php
$uploaddir = 'photos/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR";
}

?>

这个代码进入我的webview控制器,如果我错了,请纠正我

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{


    NSString *urlString = @"http://www.yourdomainName.com/yourPHPPage.php";

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

    return ([returnString isEqualToString:@"OK"]);
}

这段代码怎么样?

[self uploadImage:UIImageJPEGRepresentation(imageView.image, 1.0) filename:imageName];

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您的代码段位于Objective-C中,并使用旧的(如果不是已弃用的)API。您定位的最低iOS版本是多少?

你应该看看NSURLSession课程。关于这个主题有很多有用的tutorials