• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Want advice on NSURLSession command line tool
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Want advice on NSURLSession command line tool


  • Subject: Want advice on NSURLSession command line tool
  • From: Daryle Walker <email@hidden>
  • Date: Wed, 18 Mar 2015 15:11:13 -0400

Here’s what I got so far:

> @import Foundation;
> #include <stdbool.h>
> #include <stdlib.h>
>
> int   returnCode = EXIT_SUCCESS;
> bool  shouldExit = false;
>
> int main(int argc, const char * argv[]) {
>     if (argc != 2) {
>         fprintf(stderr, "Usage: %s URL\n", argv[0]);
>         returnCode = EXIT_FAILURE;
>         goto finish;
>     }
>
>     @autoreleasepool {
>         NSRunLoop * const              runLoop = [NSRunLoop currentRunLoop];
>         NSURLSession * const           session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration] delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
>         NSURLSessionDownloadTask * const  task = [session downloadTaskWithURL:[NSURL URLWithString:[NSString stringWithUTF8String:argv[1]]] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
>             if (error) {
>                 fprintf(stderr, "Error, downloading: %s\n", error.localizedFailureReason.UTF8String ?: error.localizedDescription.UTF8String);
>                 returnCode = EXIT_FAILURE;
>             } else {
>                 NSURL * const  finalLocation = [NSURL fileURLWithPath:response.suggestedFilename isDirectory:NO];
>
>                 [[NSFileManager defaultManager] moveItemAtURL:location toURL:finalLocation error:&error];
>                 if (error) {
>                     fprintf(stderr, "Error, copying: %s\n", error.localizedFailureReason.UTF8String ?: error.localizedDescription.UTF8String);
>                     returnCode = EXIT_FAILURE;
>                 } else {
>                     fprintf(stdout, "%s\n", finalLocation.path.UTF8String);
>                 }
>             }
>             shouldExit = true;
>         }];
>
>         if (!task) {
>             returnCode = EXIT_FAILURE;
>             goto finish;
>         }
>         [task resume];
>         while (!shouldExit && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])
>             ;
>     }
>
> finish:
>     return returnCode;
> }

1. Is this an acceptable way to use a run loop in a command-line tool?
2. Is it OK to break an @autoreleasepool with a goto?
3. Is UTF-8 the standard to convert between NSString and whatever goes to/from the Terminal? I’ve seen it in various 3rd-party code.
4. When I run the tool with the same URL twice, the “Error, copying” line is triggered since the file already exists. However, the file-name within the error reason is the one in “location,” when it should be the one from “finalLocation”. (Especially funny since the file-name in “location” changes every run.) Has anyone else seen that before? Is this a bug?
5. Should I be using “localizedFailureReason” over “localizedDescription” when printing errors?
6. Is an ephemeral session the best option? What if I someday add options for proxies, cookies, etc. (like curl or wget)?
7. Is there a standard routine that moves files and renames them when necessary? (The API would need a NSURL** parameter to give the developer the file’s actual final location.) Moving files to the Trash is an example of Apple’s code renaming when needed during a move.

—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Prev by Date: Experiences using Chromium Embedded Framework?
  • Next by Date: windowDidLoad not getting called
  • Previous by thread: Experiences using Chromium Embedded Framework?
  • Next by thread: windowDidLoad not getting called
  • Index(es):
    • Date
    • Thread