I am using UIWebView
to open a Docx file which is in the form of NSDATA passed from another controller to webview. I am using UIAcvitityController
to share content on social media as well. It works fine for Image types and PDF types but it does not work for DOCX and XLSX. Anything special I need to do here?
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:self.downloadData applicationActivities:self.applicationActivities];
You need use custom data for activityItems, and implement methods in protocol UIActivityItemSource
. follow it:
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
file: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
And, share content on social media:
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];
The above code has been tested , it does work for DOCX and XLSX.
The source demo is here: https://github.com/ocarol/UIActivityViewControllerDemo
Supported Data Types by UIActivityViewController
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With