我正在使用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