ios – 无法使用UIActivityViewController以NSDATA的形式共享Docx

前端之家收集整理的这篇文章主要介绍了ios – 无法使用UIActivityViewController以NSDATA的形式共享Docx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用UIWebView打开一个Docx文件,该文件是从另一个控制器传递到webview的NSDATA形式.我正在使用UIAcvitityController在社交媒体上分享内容.它适用于图像类型和PDF类型,但它不适用于DOCX和XLSX.我需要做些什么特别的事吗?
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:self.downloadData applicationActivities:self.applicationActivities];

解决方法

您需要为activityItems使用自定义数据,并在协议UIActivityItemSource中实现方法.跟着它:

file:ShareData.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface ShareData : NSObject<UIActivityItemSource>
@property (nonatomic,strong) NSData *data;
@property (nonatomic,strong) NSURL *url;
@end

文件:ShareData.m

#import "ShareData.h"

@implementation ShareData
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return self.data;
}

-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:UIActivityTypeAirDrop]) {
        return self.data;
    }
    return self.url;
}

-(NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return @"share data";
}

-(NSString*)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:UIActivityTypeAirDrop]) {
        return @"test";
    }
    return nil;
}
@end

并且,在社交媒体上分享内容

ShareData *share = [ShareData new];
share.data = downloadData; // one of your downloadData
share.url = downloadURL; // url for the downloadData
 UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[share] applicationActivities:nil];

上面的代码已经过测试,它适用于DOCX和XLSX.

源演示在这里:https://github.com/ocarol/UIActivityViewControllerDemo

原文链接:https://www.f2er.com/iOS/333412.html

猜你在找的iOS相关文章