Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to share Docx in form of NSDATA using UIActivityViewController

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];
like image 284
Ajay Beniwal Avatar asked Jul 27 '16 10:07

Ajay Beniwal


2 Answers

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

like image 92
ocarol Avatar answered Oct 07 '22 04:10

ocarol


Supported Data Types by UIActivityViewController

enter image description here

like image 32
PinkeshGjr Avatar answered Oct 07 '22 02:10

PinkeshGjr