ダウンロード

一括ダウンロードを行う例を以下に挙げます。

Swift:

  • // 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];

ここでは以下の処理を実施しています。

  • KiiObject インスタンスを作成。
  • ダウンロード対象ファイル(myImage.jpg)のリファレンスを作成。
  • downloadBody(with:andCompletion:_:) メソッドを実行して、ダウンロードを開始。