一,文档共享
应用可以预览或打开文档,可以选择某一应用打开这个文档。
#import "ViewController.h"
@interface ViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *docController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn1 = [[UIButton alloc] init];
[btn1 setTitle:@"预览" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn1.titleLabel.font = [UIFont systemFontOfSize:17];
[btn1 sizeToFit];
btn1.center = CGPointMake(btn1.bounds.size.width/2+30, btn1.bounds.size.height/2+30);
[btn1 addTarget:self action:@selector(clickBtn1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [[UIButton alloc] init];
[btn2 setTitle:@"直接共享" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn2.titleLabel.font = [UIFont systemFontOfSize:17];
[btn2 sizeToFit];
btn2.center = CGPointMake(btn2.bounds.size.width/2+30, btn2.bounds.size.height/2+CGRectGetMaxY(btn1.frame)+20);
[btn2 addTarget:self action:@selector(clickBtn2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
}
- (void)clickBtn1:(UIButton *)sender{
//存储到document文件夹下
[self savePdf:@"sample.pdf"];
//sample.pdf的路径
//com.adobe.pdf需要在info中注册一下
NSURL *fileUrl = [self fileInDocsDirectory:@"sample.pdf"];
if (fileUrl) {
[self configureDIControllerWithURL:fileUrl uti:@"com.adobe.pdf"];
[self.docController presentPreviewAnimated:YES];
}
}
- (void)clickBtn2:(UIButton *)sender{
[self savePdf:@"sample.pdf"];
NSURL *fileUrl = [self fileInDocsDirectory:@"sample.pdf"];
if (fileUrl) {
[self configureDIControllerWithURL:fileUrl uti:@"com.adobe.pdf"];
[self.docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}
}
- (void)savePdf:(NSString *)filename{
//plist存储
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths firstObject];
NSString *fullPath = [docsDir stringByAppendingPathComponent:filename];
//要存储的数据
NSString *extras = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
NSData *data = [NSData dataWithContentsOfFile:extras];
[data writeToFile:fullPath atomically:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
- (NSURL *)fileInDocsDirectory:(NSString *)filename{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths firstObject];
NSString *fullPath = [docsDir stringByAppendingPathComponent:filename];
return [NSURL fileURLWithPath:fullPath];
}
- (void)configureDIControllerWithURL:(NSURL *)url uti:(NSString *)uti{
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:url];
controller.delegate = self;
controller.UTI = uti;
self.docController = controller;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
里面的com.adobe.pdf是自己定义的uti,需要在info中配置一下:
二,UIActivityViewController
它可以共享多种类型的数据,NSString,NSAttributedString,NSURL,UIImage,PHAsset。
- (void)shareSomeContent{
NSString *text = @"Text to share";
NSURL *url = [NSURL URLWithString:@"http://github.com"];
UIImage *image = [UIImage imageNamed:@"wuyifan"];
NSArray *items = @[text,url,image];
UIActivityViewController *ctrl = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
//排除不允许的活动类型,此处排除在Facebook上发帖子
ctrl.excludedActivityTypes = @[UIActivityTypePostToFacebook];
[self presentViewController:ctrl animated:YES completion:nil];
}