单击按钮从捆绑包下载文件

时间:2017-07-25 03:26:59

标签: ios objective-c

我正在开发一个应用程序,其中我在应用程序包的目录中有9个pdf文件,我想在按钮点击下载文件。简要信息我想要实现的目标 - 如果我在文本字段中输入任意2位数字并单击提交逻辑,如果2位数字的输出为1,则通过将其添加到单个值(1-9)来打破2位数值该数字用于切换案例并在textview上显示输出pdf文件,在textview下面有一个保存按钮,我如何在保存按钮点击下载该PDF文件。 “

(IBAction)btnc:(id)sender { if([_textfield.text intValue]>31 &&[_textfield.text intValue]>0)

{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"WARNING" message:@"Please Enter Correct Details." preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    UIAlertAction* OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                         {
                         }];
    [alert addAction:OK];

}
else{

    {
        int sum, i ,j ;
        sum=0;
        sum= [_textfield.text intValue];
        for(i=0;i<=10;i++)
        {
            j=sum%10;
            sum=sum/10;
            sum=sum+j;
        }

        for(i=0;i<=10;i++)
        {
            j=sum%10;
            sum=sum/10;
            sum=sum+j;
        }
        NSLog(@"%i",sum);
        NSString *str = [NSString stringWithFormat:@"%d", sum];

        self.lb.text = str;

        switch(sum)
        {
            case 1 :

                NSLog(@"1\n" );
                break;
            case 2 :

                NSLog(@"2\n" );
                break;
            case 3 :

                NSLog(@"3\n" );
                break;
            case 4 :

                NSLog(@"4\n" );
                break;
            case 5 :

                NSLog(@"5\n" );
                break;
            case 6 :

                NSLog(@"6\n" );
                break;
            case 7 :

                NSLog(@"7\n" );
                break;
            case 8 :

                NSLog(@"8" );
                break;
            case 9 :

                NSLog(@"9 \n" );
                break;
            default :

                NSLog(@"incorrect\n" );
        }

        DescriptionViewController *face = [self.storyboard instantiateViewControllerWithIdentifier:@"Description"];
        [face setStr:[self descriptionForSum:sum]];
        [self.navigationController pushViewController:face animated:YES];
    }
}}

    - (NSString *)descriptionForSum:(int)sum{
NSString * returnedDescription = @"";
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"me%d", sum] ofType:@"pdf" inDirectory:@"Text"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];

if (myData) {
    returnedDescription = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
    return returnedDescription;
}
return returnedDescription; }

如果用户点击输出1的保存按钮,如图所示,如果输出为2,那么带有me1.pdf的pdf文件将开始下载,然后文件me2.pdf将开始下载。

1 个答案:

答案 0 :(得分:0)

您在QuickLook Frame工作中显示PDF: 在保存按钮上:

QLPreviewController *previewVC = [[QLPreviewController alloc] init];
        previewVC.delegate = self;
        previewVC.dataSource = self;
        [self presentViewController:previewVC animated:YES completion:nil];

Quicklook代表:

-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}

-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    @try {
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        return fileUrl;
    } @catch (NSException *exception) {
        NSLog(@"Error In Func:%s and line:%d with reason:%@",__func__,__LINE__,exception.reason);
    }
}

在快速查看视图控制器中,您将显示用户可以共享PDF的共享图标。 有关更多参考:https://www.appcoda.com/quick-look-framework/http://kratinmobile.com/blog/index.php/document-preview-in-ios-with-quick-look-framework/