我创建了一个php服务器,该服务器创建了一个文本文件,应将其返回给iOS应用,并与之通信,但我有一些未解决的问题:

  • 如何返回使用PHP在服务器端创建的文本文件?
  • 如何在ios应用程序端接收文件?

  • 我定义了以下代码:
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
     NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    

    这里的数据是应该从服务器返回的结果,但是如何实现呢?

    这是我的php文件代码:
    $za = new ZipArchive();
    $za->open('/Applications/XAMPP/xamppfiles/temp/test.zip');
    $fp = fopen ('/Applications/XAMPP/xamppfiles/temp/myText.txt',"wb");
    for( $i = 0; $i < $za->numFiles; $i++ ){
        $stat = $za->statIndex( $i );
        print_r( basename( $stat['name'] ) . PHP_EOL );
        $content = basename( $stat['name'] ) . PHP_EOL ;
    fwrite($fp,$content);
    }
    
    fclose($fp);
    

    最佳答案

    推荐

    简短的答案

    如何返回使用PHP在服务器端创建的文本文件?

    使用从URL提取文件的基础对象。 Php正在处理繁重的工作来生成文件。您只需要将其存放到可访问网络的位置即可。如果出于安全考虑,您将需要更深入地了解Secure Transport Reference中的各种选项(如果需要,也可以在堆栈中搜索有关ssl的问题)。

    如何在ios应用程序端接收文件?

    基本上选择一个基础对象,然后使用其writeToFile方法(示例显示在详细信息下)。扩展使用NSURLConnectionDelegate协议或NSURLSession类(请参阅“将下载任务添加到 session 中-需要iOS7 +”部分)。

    样本

    如果要继续使用字符串来移动数据,则可能需要查看《字符串编程指南》(link)上的示例:

    该示例包含创建字符串后要执行的关键任务:

    NSURL *URL = ...;
    NSString *string = ...;
    NSError *error;
    BOOL ok = [string writeToURL:URL atomically:YES
                      encoding:NSUnicodeStringEncoding error:&error];
    if (!ok) {
        // an error occurred
        NSLog(@"Error writing file at %@\n%@",
                  path, [error localizedFailureReason]);
        // implementation continues ...
    

    详细信息

    您可以使用Web服务器的内置功能来支持文件分发。因此,在这种情况下,Php可以将文本文件存储到可访问网络的文件夹中。在可通过网络访问的地址中创建文件后,您可以考虑使用... withContentsOfURL方法之一,该方法可以将文件转换为某种可用的数据对象...
    //transform file contents into array
    NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"https://www.somelocation.com/temp/test.text"]];
    

    要么
    //transform file contents into nsdata
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://www.somelocation.com/temp/test.text"]];
    

    要么
    //transform file contents into dictionary
    NSDictionary * dictionary = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"https://www.somelocation.com/temp/test.text"]]
    

    如果您希望将文件下载到系统中(然后在本地进行处理),则可以转换使用writeToFile方法创建的对象。在这种情况下,建议您使用NSData检索文件,然后可以使用以下命令将该文件保存到本地目录中:
    //use this instance method on the NSData object
        - (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr
    

    最后,如果您使用的是iOS7,还可以将整个过程包装到NSURLSessionDataTask中。这将允许您以异步方式执行任务(有关更多信息,请参见类参考)。
    - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
    

    甚至可以使用CIImage类上的此方法将内容转换为图像。
    + (CIImage *)imageWithContentsOfURL:(NSURL *)url options:(NSDictionary *)d
    

    需要考虑的其他因素

    下载文件后,您可能还想使用该文件,因此我建议您阅读NSFileHandle类参考,其中有一个有趣的部分,介绍如何使用块进行读写。

    其他注意事项是根据数据的敏感性可能需要保护文件。这可能会影响您选择实施的过程。

    关于php - 从php服务器接收文本文件到ios app,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20120130/

    10-14 23:04
    查看更多