Jelajahi Sumber

1.修改Apex Mobile Network Download,默认下载路径。

Pen Li 8 tahun lalu
induk
melakukan
46a74c011b

+ 7 - 0
common/NetworkUtils.m

@@ -496,6 +496,13 @@ repeat:
     RANetworkTaskDelegate *delegate = (RANetworkTaskDelegate *)sessoin.delegate;
     delegate.downloadTask = YES;
     delegate.fileCachePath = path;
+    if (progressHandler) {
+        delegate.p = progressHandler;
+    }
+    
+    if (result) {
+        delegate.r = result;
+    }
     
     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
     

+ 3 - 3
common/customUI/ResultViewController.m

@@ -736,15 +736,15 @@ const int delta = 25;
         return;
     }
     
-    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStylePlain target:self action:@selector(MenuClick:)];
+    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_menu"] style:UIBarButtonItemStylePlain target:self action:@selector(MenuClick:)];
     self.navigationItem.rightBarButtonItem = menuItem;
 }
 - (void)MenuClick:(UIBarButtonItem *)sender {
     
-    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Menu" message:nil preferredStyle:UIAlertControllerStyleAlert];
+    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"menu", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
     //block代码块取代了delegate
     
-        __weak typeof(self) weakSelf = self;
+    __weak typeof(self) weakSelf = self;
     for(int i=0;i<self.content_menu.count;i++)
     {
         NSMutableDictionary* json = self.content_menu[i];

+ 78 - 26
common/upload/RANetworkTaskDelegate.m

@@ -123,41 +123,82 @@
 //
     
     if (self.isDownloadTask) {
-        [self.fileStream jl_closeWriter];
-    }
-    
-
-    printf("net work complete\n");
-    if (self.r) {
         
+        [self.fileStream jl_closeWriter];
         
-        if(self.recvData==nil)
-        {
-            self.r(nil);
-            return;
+        if (error) {
+            if (error.code != -999) {
+                if (self.r) {
+                    
+                    NSMutableDictionary *resutlDic = [@{
+                                                        @"result" : @0,
+                                                        @"msg"    : error.localizedDescription
+                                                        } mutableCopy];
+                    self.r(resutlDic);
+                }
+            } else {
+                
+                NSLog(@"download task canceled");
+                
+            }
             
+            // 失败删除文件
+            BOOL isDir = NO;
+            if ([[NSFileManager defaultManager] fileExistsAtPath:self.fileCachePath isDirectory:&isDir]) {
+                
+                if (!isDir) {
+                    [[NSFileManager defaultManager] removeItemAtPath:self.fileCachePath error:nil];
+                }
+                
+            }
+            
+        } else {
+            if (self.r) {
+                
+                NSMutableDictionary *resutlDic = [@{
+                                                    @"result" : @2,
+                                                    @"path"   : self.fileCachePath,
+                                                    @"msg"    : @"download complete"
+                                                    } mutableCopy];
+                self.r(resutlDic);
+            }
         }
+    } else {
         
-        NSMutableString *str = [[NSMutableString alloc] initWithData:self.recvData encoding:NSUTF8StringEncoding];
-        
-        NSLog(@"data string: %@",str);
-        
-        NSError *error1=nil;
-        NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:self.recvData options:NSJSONReadingMutableLeaves error:&error1];
-        if(jsobj==nil)// 服务器返回不是json
-        {
+        if (self.r) {
             
-            jsobj=[NSMutableDictionary new];
-            [jsobj setValue:@"-30" forKey:@"result"];
-            [jsobj setValue:@"Can not upload to server, please contact administrator." forKey:@"msg"];
             
+            if(self.recvData==nil)
+            {
+                self.r(nil);
+                return;
+                
+            }
             
+            NSMutableString *str = [[NSMutableString alloc] initWithData:self.recvData encoding:NSUTF8StringEncoding];
+            
+            NSLog(@"data string: %@",str);
+            
+            NSError *error1=nil;
+            NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:self.recvData options:NSJSONReadingMutableLeaves error:&error1];
+            if(jsobj==nil)// 服务器返回不是json
+            {
+                
+                jsobj=[NSMutableDictionary new];
+                [jsobj setValue:@"-30" forKey:@"result"];
+                [jsobj setValue:@"Can not upload to server, please contact administrator." forKey:@"msg"];
+                
+                
+                
+            }
+            
+            self.r([jsobj mutableCopy]);
             
         }
-        
-        self.r([jsobj mutableCopy]);
-        
     }
+
+    printf("net work complete\n");
+    
 //
     [session invalidateAndCancel];
 }
@@ -179,8 +220,11 @@
         BOOL isDir = NO;
         [[NSFileManager defaultManager] fileExistsAtPath:desPath isDirectory:&isDir];
         if (isDir) {
-            
-            desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
+            if (response.suggestedFilename) {
+                desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
+            } else {
+                desPath = [self.fileCachePath stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
+            }
         }
         
         // 如果文件存在
@@ -209,6 +253,14 @@
         [self.recvData appendData:data];
     } else {
         
+        
+        if (self.p) {
+            int64_t totalExpectedRevc = [dataTask countOfBytesExpectedToReceive];
+            int64_t totalRevc = [dataTask countOfBytesReceived];
+            double progress = (double)totalRevc / totalExpectedRevc;
+            self.p(dataTask,progress);
+        }
+        
         [self.fileStream jl_writeData:data];
 
     }