DWAppDelegate.m 7.05 KB
Newer Older
Han Ruisong's avatar
Han Ruisong committed
1
2
3
4
5
6
#import "DWAppDelegate.h"
#import "DWDownloadSessionManager.h"
#import "DWMainViewController.h"
#import "DWNavigationViewController.h"
#import "DWOfflineModel.h"
#import "MJExtension.h"
Han Ruisong's avatar
Han Ruisong committed
7
#import <Photos/Photos.h>
Han Ruisong's avatar
Han Ruisong committed
8
9
10
11
12
13
14
15
16
17
18
19

#define DWUploadItemPlistFilename @"uploadItems.plist"

@interface DWAppDelegate ()

@end

@implementation DWAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [DWLog setIsDebugHttpLog:YES];
Han Ruisong's avatar
Han Ruisong committed
20

Han Ruisong's avatar
Han Ruisong committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    //设置AVAudioSession
    NSError *categoryError = nil;
    BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&categoryError];
    if (!success)
    {
        NSLog(@"Error setting audio session category: %@", categoryError);
    }
    
    NSError *activeError = nil;
    success = [[AVAudioSession sharedInstance] setActive:YES error:&activeError];
    if (!success)
    {
        NSLog(@"Error setting audio session active: %@", activeError);
    }
Han Ruisong's avatar
Han Ruisong committed
35
        
Han Ruisong's avatar
Han Ruisong committed
36
37
    //后台下载设置
    [[DWDownloadSessionManager manager] configureBackroundSession];
Han Ruisong's avatar
Han Ruisong committed
38
39
40
    //设置最大下载并发数,默认不设置
//    [DWDownloadSessionManager manager].maxDownloadCount = 2;
//    [DWDownloadSessionManager manager].isBatchDownload = NO;
Han Ruisong's avatar
Han Ruisong committed
41
42
43
44
45
46
47
48
49
50
51
52
53
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    DWMainViewController * mainVC = [[DWMainViewController alloc]initWithCollectionViewLayout:[self getLayout]];
    DWNavigationViewController * nc = [[DWNavigationViewController alloc]initWithRootViewController:mainVC];
    nc.navigationBar.translucent = NO;
    self.window.rootViewController = nc;
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    //根据自己项目原业务逻辑,自行斟酌调用即可。
    [self migrateOldDownloadTaskToNewVersion];
Han Ruisong's avatar
Han Ruisong committed
54
        
Han Ruisong's avatar
Han Ruisong committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    [self.window makeKeyAndVisible];
    
    return YES;
}

//下载任务过渡到新版
-(void)migrateOldDownloadTaskToNewVersion
{
    //!!!仅供参考
    //对于旧版本转移逻辑,请参照自己项目中的业务逻辑来处理,此处只是针对旧版Demo中的下载任务,转移到新版的示例。
    
    //1。首先,需要获取到自己项目中的下载队列。
    NSArray * downingArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"downingArray"];
    
    NSArray * finishDicArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"finishDicArray"];
    
    //2。通过[DWDownloadSessionManager manager]的 migrateDownloadTask:。。。方法,将旧版的下载任务过渡到新版。
    [downingArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DWOfflineModel * model = [DWOfflineModel mj_objectWithKeyValues:obj];
        //未下载完成的视频
        //本地路径请务必传nil
        DWDownloadModel * downloadModel = [[DWDownloadSessionManager manager]
                                           migrateDownloadTask:nil
                                           DownloadUrl:model.playurl
                                           MediaType:model.mediatype
                                           Quality:model.definition Desp:model.desp
                                           VRMode:NO
                                           OthersInfo:@{@"title":model.videoId,@"imageUrl":@"icon_placeholder.png"}
                                           UserId:[DWConfigurationManager sharedInstance].DWAccount_userId
                                           VideoId:model.videoId
                                           TotalBytesWritten:0
                                           TotalBytesExpectedToWrite:0];
        
        //如不开始下载,页面显示的进度根据totalBytesWritten,totalBytesExpectedToWrite参数决定。
        //如果以前没有存储此字段,继续开始下载即可获取到正确的数据。
//        if (downloadModel) {
//            //证明过渡成功,否则返回nil
//            [[DWDownloadSessionManager manager] resumeWithDownloadModel:downloadModel];
//        }
    }];
    
    [finishDicArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DWOfflineModel * model = [DWOfflineModel mj_objectWithKeyValues:obj];
        
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentDirectory = [paths objectAtIndex:0];
        NSString * localPath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[model.videoPath lastPathComponent]];
        
        DWDownloadModel * downloadModel = [[DWDownloadSessionManager manager]
                                           migrateDownloadTask:localPath
                                           DownloadUrl:model.playurl
                                           MediaType:model.mediatype
                                           Quality:model.definition
                                           Desp:model.desp
                                           VRMode:NO
                                           OthersInfo:@{@"title":model.videoId,@"imageUrl":@"icon_placeholder.png"}
                                           UserId:[DWConfigurationManager sharedInstance].DWAccount_userId
                                           VideoId:model.videoId
                                           TotalBytesWritten:0
                                           TotalBytesExpectedToWrite:0];
    }];
    
    //3.下载任务过渡完成后,将原本地保存的下载数据删除掉即可。
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"downingArray"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"finishDicArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
      [application beginReceivingRemoteControlEvents];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{
    
}

-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
Han Ruisong's avatar
Han Ruisong committed
150
151
152
153
154
{
    //下载的应该也得 搞个id传进去
    [[DWDownloadSessionManager manager] setBackgroundSession:identifier CompletionHandler:completionHandler];
 
    [[DWUploadSessionManager manager] setUploadSession:identifier CompletionHandler:completionHandler];
Han Ruisong's avatar
Han Ruisong committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
}

-(UICollectionViewFlowLayout *)getLayout
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((ScreenWidth - 5) / 2.0, ScaleHeight((ScreenWidth - 20 - 5) / 2.0, 175 / 98.0) + 10 + 14);
    layout.headerReferenceSize = CGSizeMake(ScreenWidth, ScaleHeight((ScreenWidth - 20),355 / 200.0) + 10);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 15;
    return layout;
}


@end