iOS使用原生分享做PDF存储及分享

NSData *date = [NSData dataWithContentsOfURL:[NSURL URLWithString:[data objectForKey:@"imgurl"]]];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

NSFileManager *fileManger = [NSFileManager defaultManager];

    NSString*testDirectory = [documentsDirectorystringByAppendingString:@"/"];

    //  创建目录

    [fileMangercreateDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];

    //  创建文件

    NSString *name =[NSString stringWithFormat:@"%@.pdf",[data objectForKey:@"filename"]];

    NSString*testPath = [testDirectorystringByAppendingPathComponent:name];

    //  写入文件

    [fileMangercreateFileAtPath:testPathcontents:dateattributes:nil];

    NSURL*  file = [NSURLfileURLWithPath:testPath];

UIActivityViewController * activity = [[UIActivityViewController alloc]initWithActivityItems:@[file]

                                                                           applicationActivities:nil];

    activity.completionWithItemsHandler = ^(UIActivityType  _Nullable  activityType,

                                            BOOLcompleted,

                                            NSArray*_NullablereturnedItems,

                                            NSError*_NullableactivityError) {

        if(completed ==YES) {

            if ([activityType isEqualToString:@"com.apple.CloudDocsUI.AddToiCloudDrive"]) {


                [SVProgressHUDshowSuccessWithStatus:@"保存成功,请到系统“文件”中查看"duration:5];

            }

            else if ([activityType isEqualToString:@"com.tencent.xin.sharetimeline"]||[activityType isEqualToString:@"com.tencent.mqq.ShareExtension"]) {

                [SVProgressHUDshowSuccessWithStatus:@"分享成功"];

            }else{

                [SVProgressHUDshowSuccessWithStatus:@"成功"];

            }

        }

        NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError);

    };


    [[UIApplication sharedApplication].keyWindow.rootViewController presentModalViewController:activity animated:NO];

你可能感兴趣的:(iOS使用原生分享做PDF存储及分享)