ダウンロード
中断/再開が可能な Object Body のダウンロードをする例を以下に挙げます。
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 downloadFilePath = targetDirectory.appendingPathComponent("sample.mp4") // Create a downloader. let downloader = object.downloader(downloadFilePath) // Create a progress block. let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in let info = transferObject.info() print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))") } do{ // Start downloading. try downloader.transfer(progressBlock: progress) } catch let error as NSError { print("transfer 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 downloadFilePath = targetDirectory.appendingPathComponent("sample.mp4") // Create a downloader. let downloader = object!.downloader(downloadFilePath) // Create a progress block. let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in let info = transferObject.info() print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))") } // Start downloading. downloader.transfer(progressBlock: progress, andCompletionBlock: { (transferObject : KiiRTransfer, error ) in if error != nil { // Handle the error. return } })
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 *downloadFilePath = [targetDirectory stringByAppendingPathComponent:@"sample.mp4"]; // Create a downloader. KiiDownloader *downloader = [object downloader:downloadFilePath]; // Create a progress block. KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) { KiiRTransferInfo *info = [transferObject info]; NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]); }; // Start downloading. [downloader transferWithProgressBlock:progress 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 *downloadFilePath = [targetDirectory stringByAppendingPathComponent:@"sample.mp4"]; // Create a downloader. KiiDownloader *downloader = [object downloader:downloadFilePath]; // Create a progress block. KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) { KiiRTransferInfo *info = [transferObject info]; NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]); }; // Start downloading. [downloader transferWithProgressBlock:progress andCompletionBlock:^(id<KiiRTransfer> transferObject, NSError *error) { if (error != nil) { // Handle the error. NSLog(@"Transfer error!"); return; } }];
ここでは以下の処理を実施しています。
KiiObject
インスタンスを作成。- ダウンロード対象ファイルのリファレンスを指定して
downloader(_:)
メソッドを実行し、KiiDownloader
インスタンスを作成。 - Progress block を定義。
transfer(progressBlock:andCompletionBlock:_:)
` メソッドを実行して、ダウンロードを開始。
ブロッキング API の transfer(progressBlock:_:)
メソッドをメインスレッドから呼び出した場合、Progress block は呼び出されません。
Progress block は、転送の進捗状況に応じて呼び出されます。転送サイズが小さい場合は、1 回目の呼び出しで 100% の進捗を示すことがあります。
何らかの理由によりダウンロードが中断した場合はエラーが発生します。この場合、中断箇所よりダウンロードの再開を行うことができます。再開方法については ダウンロードの再開 を参照してください。