简单封装一个下载单例类

作者: 静花寒 | 来源:发表于2016-01-13 21:18 被阅读483次

在简友的建议下,我今天试着重新写了一个下载单例类,实际用下来,可以说省去了很多麻烦事,果然单例还是很有必要的,废话不多说,下面上干货
@Realank

我写的尽量详细些,这样大家也好理解

#import <Foundation/Foundation.h>
#import "DailySelected.h"
@interface DownloadHandle : NSObject<NSURLConnectionDelegate>

+ (DownloadHandle *)shareDownloadHandle;
//传递的是model,因为等会model里面其他一些东西有用。
- (void)createDownloadWithDailySelected:(DailySelected *)dailySelected;
//第一个参数我声明是为了做进度条的接口数据,外界通过接收这个数据,可以做可视的运动的进度条
@property (nonatomic,strong) NSString *percent;
@property (nonatomic,strong) DailySelected *dailySelected;
@property (nonatomic,assign) CGFloat currentLength;
@property (nonatomic,assign) CGFloat totalLength;
@property (nonatomic,retain) NSMutableData *recvivedData;
@property (nonatomic,retain) NSString *fileName;

@end
#import "DownloadHandle.h"
static DownloadHandle *downloadHandle;
@implementation DownloadHandle

+ (DownloadHandle *)shareDownloadHandle
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        downloadHandle = [[DownloadHandle alloc] init];
    });
    return downloadHandle;
}

- (void)createDownloadWithDailySelected:(DailySelected *)dailySelected
{
    self.recvivedData = [[NSMutableData alloc] initWithLength:0];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:dailySelected.playUrl] cachePolicy:(NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:5.0];
    self.dailySelected = dailySelected;
    self.fileName = [[[[NSURL URLWithString:dailySelected.playUrl] absoluteString]lastPathComponent]copy];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    if (connection == nil) {
        
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.recvivedData appendData:data];
    self.currentLength += data.length;
    //MB
    //百分比
    self.percent = [NSString stringWithFormat:@"%.02f%%",(self.currentLength / self.totalLength) * 100];
    //NSLog(@"%@",self.percent);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.totalLength = [response expectedContentLength];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *filePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]stringByAppendingPathComponent:self.fileName];
    NSData *data = self.recvivedData;
    [data writeToFile:filePath atomically:NO];//将数据写入Documents目录。
    //NSLog(@"%@",filePath);
    self.fileName = filePath;
    //NSLog(@"%@",self.fileName);
    [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected];
    [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected fileName:self.fileName];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"fileName" object:nil];
}

这样,一个简单的单例类就可以使用了。

相关文章

  • 简单封装一个下载单例类

    在简友的建议下,我今天试着重新写了一个下载单例类,实际用下来,可以说省去了很多麻烦事,果然单例还是很有必要的,废话...

  • 单例模式

    单例模式是封装的一种形式,依靠单例模式调用被封装的属性和方法,因为使用单例模式的类无法生成实例(只能引用单例模式所...

  • iOS单例模式

    单例模式 解决“应用中只有一个单例”的一类问题。 Objecttive-C实现原理 单例模式一般会封装一个静态属性...

  • 设计模式之单例模式

    单例设计模式理解起来非常简单。一个类只允许创建一个对象(或者实例),那这个类就是一个单例类,这种设计模式就叫单例模...

  • OkHttp的简单封装使用

    封装单例工具类,包含get和post请求 调用方式

  • java设计模式01(单例模式)

    单例模式是结构最简单的设计模式,核心结构只包含一个特殊类即单例类。通过单例模式可以确保系统中的一个类只有一个实例而...

  • OkHttp单例封装类

    //导okhttp依赖compile 'com.squareup.okhttp3:okhttp:3.8.1' pu...

  • 2020-08-17 Unity使用旧版WWW下载图片,音频等

    简单封装了一个下载类,记录一下

  • python09-单例设计模式

    单例设计模式 如果每个对象封装了不同的数据,则不适用单例模式 如果一个类封装的数据一样,调用方法得到的结果一样,且...

  • AFNetworking简单封装

    用AFNetworking框架做了简单的网络请求的封装,直接贴代码了 1、 .h文件中声明单例类方法、成员变量 ...

网友评论

    本文标题:简单封装一个下载单例类

    本文链接:https://www.haomeiwen.com/subject/atwckttx.html