2

I have created a php server that creates a text file that should be returned to an iOS app, that communicates with it but I have some unsolved questions:

  1. how could I return the text file the was created on the server side using the PHP?
  2. how to receive the file on the ios app side?

I have defined this code:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
 NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

Here data is the result that should be returned from the server but how could I implement that?

Here is my php file code:

$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);
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
user2960510
  • 1,123
  • 2
  • 8
  • 9

2 Answers2

2

Recommendation

Short answers

How could I return the text file the was created on the server side using the PHP?

Using a foundation object that pulls the file from the URL. The heavy lifting is being done by Php to produce the file. You just need to deposit it into a web accessible location. If security is a concern you will want to take a deeper look at the various options in the Secure Transport Reference (also if needed, search stack overflow for questions on ssl).

How to receive the file on the ios app side?

Basically choose a foundation object and then use its writeToFile methods (examples shown under details). Extended use of the NSURLConnectionDelegate protocol or the NSURLSession class (see section on Adding Download Tasks to a Session - iOS7+ required).

Samples

If you want to continue using strings to move your data around you might want to take a look at the samples found on the String Programming Guide (link):

The sample includes the key task you'd want to do after creating your string:

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 ...

Details

You can use the built-in capability of your web server to support file distribution. So, in this case, Php could just deposit the text file into a web accessible folder. Once your file is created in a web accessible address you could consider using one of the ...withContentsOfURL methods that can transform the file into a usable data object of some kind...

//transform file contents into array
NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"https://www.somelocation.com/temp/test.text"]];

or

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

or

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

If you'd prefer to download the file to your system (and then work on it locally) you can transform the object created using the writeToFile methods. In that case, I'd suggest that you use the NSData to retrieve the file and then you can save that file into your local directory using:

//use this instance method on the NSData object 
    - (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr

Finally, you can also wrap the entire process into a NSURLSessionDataTask if you are using iOS7. This would allow you to perform the task in an asynchronous manner (see class reference for more info).

- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler

There is even a way to turn your content into an image using this method on the CIImage Class.

+ (CIImage *)imageWithContentsOfURL:(NSURL *)url options:(NSDictionary *)d

Other factors to consider

You may also want to work with the file after you've downloaded it and I would recommend a review of the NSFileHandle Class Reference that has an interesting section on reading and writing using blocks.

Other considerations are the possible need to secure the file depending on sensitivity of data. This might affect the process you choose to implement.

Community
  • 1
  • 1
Tommie C.
  • 12,895
  • 5
  • 82
  • 100
0

You can simply create and host the textfile from php. In your web service response just send it's URL

Now you can take the contents from that text file

NSString *strFileContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.puzzlers.org/pub/wordlists/pocket.txt"] encoding:NSUTF8StringEncoding error:nil ];

or you can download file using ASIHTTPRequest

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/path/to/my_file.txt"];
[request startSynchronous];
manujmv
  • 6,450
  • 1
  • 22
  • 35
  • thanks for your reply, but how can I use the respond defined in the HTTPREQUEST without using this way you have wrote above? – user2960510 Nov 21 '13 at 13:08