/**
下載檔案
*/
-
(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];
NSURLSessionDataTasktask=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) {
}];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/230753.html
標籤:其他
