iOS中的共享内容

一,文档共享

应用可以预览或打开文档,可以选择某一应用打开这个文档。

#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];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值