Downloading an Object Body

The following sample code shows how to download an object body with the transfer-at-once method.

Swift 3:

  • // Instantiate the target KiiObject.
    let object = KiiObject(uri: "Set the URI of an existing KiiObject here")!
    
    // Specify the file destination.
    let targetDirectory : NSString = (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as NSString
    let downloadFilePathStr = targetDirectory.appendingPathComponent("myImage.jpg")
    let downloadFilePath = URL(fileURLWithPath: downloadFilePathStr)
    
    do{
      // Start downloading.
      try object.downloadBodySynchronous(with: downloadFilePath)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate the target KiiObject.
    let object = KiiObject(uri: "Set the URI of an existing KiiObject here")!
    
    // Specify the file destination.
    let targetDirectory : NSString = (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as NSString
    let downloadFilePathStr = targetDirectory.appendingPathComponent("myImage.jpg")
    let downloadFilePath = URL(fileURLWithPath: downloadFilePathStr)
    
    // Create a progress block.
    let progress : KiiObjectBodyProgressBlock = { (transObj : KiiObject, completedSizeInBytes : Int, totalSizeInBytes : Int, error : Error?) in
      print("Progress : \(Float(completedSizeInBytes/totalSizeInBytes))")
    }
    
    // Start downloading.
    object!.downloadBody(with: downloadFilePath, andCompletion: { (object : KiiObject?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        print("Transfer error!")
        return
      }
    }, andProgress: progress)

Objective-C:

  • NSError *error = nil;
    
    // Instantiate the target KiiObject.
    KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"];
    
    // Specify the file destination.
    NSString *targetDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *downloadFilePathStr = [targetDirectory stringByAppendingPathComponent:@"myImage.jpg"];
    NSURL *downloadFilePath = [NSURL fileURLWithPath:downloadFilePathStr];
    
    // Start downloading.
    [object downloadBodySynchronousWithURL:downloadFilePath
                                  andError:&error];
    
    if (error != nil) {
      // Handle the error.
      NSLog(@"Transfer error!");
      return;
    }
  • // Instantiate the target KiiObject.
    KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"];
    
    // Specify the file destination.
    NSString *targetDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *downloadFilePathStr = [targetDirectory stringByAppendingPathComponent:@"myImage.jpg"];
    NSURL *downloadFilePath = [NSURL fileURLWithPath:downloadFilePathStr];
    
    // Create a progress block.
    KiiObjectBodyProgressBlock progress = ^(KiiObject *transObj, NSUInteger completedSizeInBytes, NSUInteger totalSizeInBytes, NSError *retError) {
      NSLog(@"Progress : %f", (float) completedSizeInBytes/ totalSizeInBytes);
    };
    
    // Start downloading.
    [object downloadBodyWithURL:downloadFilePath andCompletion:^(KiiObject *obj, NSError *error) {
      if (error != nil) {
        // Handle the error.
        NSLog(@"Transfer error!");
        return;
      }
    } andProgress:progress];

Here is what is happening in the sample code:

  • Create a KiiObject instance
  • Create a reference to the target file (myImage.jpg).
  • Start downloading by calling the downloadBody(withURL:andCompletion:_:) method.