关注
iOS文档下载功能

/**
下载文档
*/

  • (IBAction)downLoadDoc:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths lastObject];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",@“文件名字”]]; //docPath为文件名

    if ([fileManager fileExistsAtPath:filePath]) {
    //文件已经存在,直接打开
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@“是否打开文件?”.localized message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction =[UIAlertAction actionWithTitle:@“取消”.localized style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:[UIAlertAction actionWithTitle:@“确定”.localized style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    [self openDocxWithPath:filePath];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];

    }else {
    //文件不存在,要下载
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@“是否下载并打开文件?”.localized message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction =[UIAlertAction actionWithTitle:@“取消”.localized style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:[UIAlertAction actionWithTitle:@“确定”.localized style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    [self downloadDocxWithDocPath:documentsDirectory fileName:[NSString stringWithFormat:@"%@",@“文件名字”]];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];

    }
    }

#pragma mark - 下载文件
-(void)downloadDocxWithDocPath:(NSString *)docPath fileName:(NSString *)fileName{

// fileStr即接口返回的链接
[SVProgressHUD show];
//创建 Request
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@“url”]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
[SVProgressHUD dismiss];
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
[SVProgressHUD dismiss];
NSString *path = [docPath stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
[SVProgressHUD dismiss];
NSString *name = [filePath path];
[self openDocxWithPath:name];
}];
[task resume];//开始下载 要不然不会进行下载的
}

-(void)openDocxWithPath:(NSString *)filePath {
[self pushSafariWithTitle:@“报价书”.localized urlString:self.model.offer];//用网页打开
// UIDocumentInteractionController *doc= [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
// doc.delegate = self;
// [doc presentPreviewAnimated:YES];

}

  • (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{

    return self;
    }

  • (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {

    return self.view;
    }

  • (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {

    return CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    /**
    网络请求
    */

  • (nullable NSURLSessionDataTask*)POSTWithURL:(NSString )urlString parameters:(id)parameters success:(nonnull RequestCompletionBlock)success failure:(nonnull RequestFailureBlock)failure{
    [SVProgressHUD show];
    NSURLSessionDataTask
    task=nil;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer = responseSerializer;
    //创建你得请求url、设置请求头
    NSString *URL = urlString;
    NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@“post” URLString:URL parameters:parameters error:nil];

    [[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
    NSLog(@"%@",uploadProgress);
    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@"%@",downloadProgress);
    } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
    [SVProgressHUD dismiss];
    if (!error) {
    NSDictionary*Dic;
    if ([responseObject isKindOfClass:[NSDictionary class]]) {
    Dic=responseObject;
    }else if ([responseObject isKindOfClass:[NSData class]]){
    NSString *result=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
    result = [result stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    NSError *error2=nil;
    Dic = [NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error2];
    }
    if (success) {
    success(Dic);
    }
    }else{//网络出错
    if (failure) {
    failure(error);
    }
    }
    }]resume];

    return task;
    }

  • (void)requestWithUrl:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success {
    [RequestViewController POSTWithURL:url parameters:params success:^(NSDictionary * _Nullable request) {
    if (success) {
    success(request);
    }
    } failure:^(NSError * _Nullable error) {
    NSLog(@“错误信息%@”,error);
    }];
    }
  • (void)test1{
    [self requestWithUrl:@“http://tankaquan.host5.liuniukeji.net/index.php/Api/IndexApi/homePage” params:@{@“can”:@2} success:^(id _Nonnull json) {
    }];
    }

转载自CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/weixin_48567147/article/details/110679590

文章分类iOS

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--