📄 downloader.m
字号:
#import <CoreFoundation/CoreFoundation.h>#import <Foundation/Foundation.h>#import <UIKit/CDStructures.h>#import <UIKit/UIWindow.h>#import <UIKit/UIView-Hierarchy.h>#import <UIKit/UIHardware.h>#import <UIKit/UIKit.h>#import <UIKit/UIApplication.h>#import <UIKit/UITextView.h>#import <UIKit/UIView.h>#import <UIKit/UIKeyboard.h>#import <UIKit/UIBox.h>#import <Celestial/AVItem.h>#import <Celestial/AVController.h>#import <Celestial/AVQueue.h>
#import "Downloader.h"
@implementation Downloader
+ (id) downloadURL: (NSString *)url to: (NSString *)filename app: (UIApplication *) app trigger: (NSString *) trig {
return [[Downloader alloc] initWithDownloadURL: url savingToFile: filename app: app trigger: trig];
}
- (id) initWithDownloadURL: (NSString *)u savingToFile: (NSString *)f app: (UIApplication *) a trigger: (NSString *) t {
NSLog(@"Downloader begins: downloading\n\t%@\nto:\n\t%@", u, f);
url = [u retain];
file = [f retain];
app = a;
trig = t;
nsurl = [[NSURL URLWithString: u] retain];
failed = finished = NO;
data = [[NSMutableData dataWithCapacity: 100000] retain];
expectedBytes = loadedBytes = 0;
[nsurl loadResourceDataNotifyingClient: self usingCache: YES];
return self;
}
- (float) getProgress {
if (expectedBytes == 0) return 0;
return (float)loadedBytes / (float)expectedBytes;
}
- (BOOL) finished {
return finished;
}
- (BOOL) failed {
return failed;
}
- (void)URL:(NSURL *)sender resourceDataDidBecomeAvailable:(NSData *)newBytes {
NSLog(@"Downloader got %d bytes", [newBytes length]);
[data appendData: newBytes];
loadedBytes = [data length];
}
- (void)URL:(NSURL *)sender resourceDidFailLoadingWithReason:(NSString *)reason{
failed = YES;
finished = YES;
[app triggerButton: trig];
}
- (void)URLResourceDidCancelLoading:(NSURL *)sender{
failed = YES;
finished = YES;
[app triggerButton: trig];
}
- (void)URLResourceDidFinishLoading:(NSURL *)sender{
NSLog(@"Downloader finished");
[data writeToFile: file atomically: NO];
finished = YES;
[app triggerButton: trig];
}
@end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -