Sending string via ftp : getting error

2019-08-23 13:44发布

问题:

I'm trying to send a string via ftp using the code below but I'm getting an error:

{

    //miscellaneous lines of code.....
    //Convert contents of shopping cart into a property list
    [Cart serializeCart];

    //now need to transport the propertyList to the webserver
    //first step is get the serialized propertylist from the documents folder
    NSString *pathToSerializedCart = [rootPath stringByAppendingPathComponent:@"serializedCart.plist"];

    NSString *shoppingCartString;

   if (![fileManager fileExistsAtPath:pathToSerializedCart])
   {
       NSLog(@"ERROR:\nCouldnt find serialized cart in documents folder.");
   }
   else 
   {
       NSData *serializedData = [NSData            dataWithContentsOfFile:pathToSerializedCart];
       shoppingCartString = [[NSString alloc] initWithData:serializedData encoding:NSUTF8StringEncoding];
   }

   NSLog(@"%@", shoppingCartString);
   //Now that the cart is converted into a string. it is ready for transport
   NSURL *url = [NSURL URLWithString:@"ftp://username:password@domainName.com/folder/serializedCart.xml"];

   BOOL OK =[shoppingCartString writeToURL:url atomically:NO encoding:NSUTF8StringEncoding error:&error];   
   if(!OK) {
       NSLog(@"Error writing to file %@ , error = %@", url, [error localizedFailureReason]);
   }

I'm getting the following console output for this code:

Error writing to file ftp://username:password@domainName.com/folder/serializedCart.xml , error = (null)

One of the variables: _domain in this error object in the last line when, I mouse over it during debugging says NSCocoaErrorDomain

I'm not sure how to debug this. Could someone give any suggestions?

回答1:

The writeToURL:.. methods don't support the FTP protocol. You'll have to use other mechanisms to write the string content onto an ftp server. You can look at tools such as ios-ftp-server to handle such uploads. You can also look at this sample code from Apple.