Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(share_plus): Added excludedActivityTypes support for the iOS platform. #3376

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,59 @@
return viewController;
}

static NSDictionary *activityTypes;

static void initializeActivityTypeMapping(void) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSMutableDictionary *originalTypes =
[[NSMutableDictionary alloc] initWithDictionary:@{
@"postToFacebook" : UIActivityTypePostToFacebook,
@"postToTwitter" : UIActivityTypePostToTwitter,
@"postToWeibo" : UIActivityTypePostToWeibo,
@"message" : UIActivityTypeMessage,
@"mail" : UIActivityTypeMail,
@"print" : UIActivityTypePrint,
@"copyToPasteboard" : UIActivityTypeCopyToPasteboard,
@"assignToContact" : UIActivityTypeAssignToContact,
@"saveToCameraRoll" : UIActivityTypeSaveToCameraRoll,
@"addToReadingList" : UIActivityTypeAddToReadingList,
@"postToFlickr" : UIActivityTypePostToFlickr,
@"postToVimeo" : UIActivityTypePostToVimeo,
@"postToTencentWeibo" : UIActivityTypePostToTencentWeibo,
@"airDrop" : UIActivityTypeAirDrop,
@"openInIBooks" : UIActivityTypeOpenInIBooks,
@"markupAsPDF" : UIActivityTypeMarkupAsPDF,
}];

if (@available(iOS 15.4, *)) {
originalTypes[@"sharePlay"] = UIActivityTypeSharePlay;
}

if (@available(iOS 16.0, *)) {
originalTypes[@"collaborationInviteWithLink"] =
UIActivityTypeCollaborationInviteWithLink;
}

if (@available(iOS 16.0, *)) {
originalTypes[@"collaborationCopyLink"] =
UIActivityTypeCollaborationCopyLink;
}
if (@available(iOS 16.4, *)) {
originalTypes[@"addToHomeScreen"] = UIActivityTypeAddToHomeScreen;
}
activityTypes = originalTypes;
});
}

static UIActivityType activityTypeForString(NSString *activityTypeString) {
initializeActivityTypeMapping();
if ([activityTypes.allKeys containsObject:activityTypeString]) {
return activityTypes[activityTypeString];
}
return nil;
}

// We need the companion to avoid ARC deadlock
@interface UIActivityViewSuccessCompanion : NSObject

Expand Down Expand Up @@ -254,6 +307,7 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
NSNumber *originY = arguments[@"originY"];
NSNumber *originWidth = arguments[@"originWidth"];
NSNumber *originHeight = arguments[@"originHeight"];
NSArray *excludedActivityType = arguments[@"excludedActivityType"];

CGRect originRect = CGRectZero;
if (originX && originY && originWidth && originHeight) {
Expand Down Expand Up @@ -284,10 +338,11 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
TopViewControllerForViewController(rootViewController);

[self shareText:shareText
subject:shareSubject
withController:topViewController
atSource:originRect
toResult:result];
subject:shareSubject
excludedActivityType:excludedActivityType
withController:topViewController
atSource:originRect
toResult:result];
} else if ([@"shareFiles" isEqualToString:call.method]) {
NSArray *paths = arguments[@"paths"];
NSArray *mimeTypes = arguments[@"mimeTypes"];
Expand Down Expand Up @@ -320,12 +375,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
UIViewController *topViewController =
TopViewControllerForViewController(rootViewController);
[self shareFiles:paths
withMimeType:mimeTypes
withSubject:subject
withText:text
withController:topViewController
atSource:originRect
toResult:result];
withMimeType:mimeTypes
withSubject:subject
withText:text
excludedActivityType:excludedActivityType
withController:topViewController
atSource:originRect
toResult:result];
} else if ([@"shareUri" isEqualToString:call.method]) {
NSString *uri = arguments[@"uri"];

Expand All @@ -347,20 +403,22 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
TopViewControllerForViewController(rootViewController);

[self shareUri:uri
withController:topViewController
atSource:originRect
toResult:result];
excludedActivityType:excludedActivityType
withController:topViewController
atSource:originRect
toResult:result];
} else {
result(FlutterMethodNotImplemented);
}
}];
}

+ (void)share:(NSArray *)shareItems
withSubject:(NSString *)subject
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
withSubject:(NSString *)subject
excludedActivityType:(NSArray *)excludedActivityType
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
UIActivityViewSuccessController *activityViewController =
[[UIActivityViewSuccessController alloc] initWithActivityItems:shareItems
applicationActivities:nil];
Expand All @@ -369,7 +427,14 @@ + (void)share:(NSArray *)shareItems
if (![subject isKindOfClass:[NSNull class]]) {
[activityViewController setValue:subject forKey:@"subject"];
}

NSMutableArray *excludedActivityTypes = [[NSMutableArray alloc] init];
for (NSString *type in excludedActivityType) {
UIActivityType activityType = activityTypeForString(type);
if (activityType != nil) {
[excludedActivityTypes addObject:activityType];
}
}
activityViewController.excludedActivityTypes = excludedActivityTypes;
activityViewController.popoverPresentationController.sourceView =
controller.view;
BOOL isCoordinateSpaceOfSourceView =
Expand Down Expand Up @@ -413,38 +478,43 @@ + (void)share:(NSArray *)shareItems
}

+ (void)shareUri:(NSString *)uri
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
excludedActivityType:(NSArray *)excludedActivityType
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
NSURL *data = [NSURL URLWithString:uri];
[self share:@[ data ]
withSubject:nil
withController:controller
atSource:origin
toResult:result];
withSubject:nil
excludedActivityType:excludedActivityType
withController:controller
atSource:origin
toResult:result];
}

+ (void)shareText:(NSString *)shareText
subject:(NSString *)subject
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
subject:(NSString *)subject
excludedActivityType:(NSArray *)excludedActivityType
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
NSObject *data = [[SharePlusData alloc] initWithSubject:subject
text:shareText];
[self share:@[ data ]
withSubject:subject
withController:controller
atSource:origin
toResult:result];
withSubject:subject
excludedActivityType:excludedActivityType
withController:controller
atSource:origin
toResult:result];
}

+ (void)shareFiles:(NSArray *)paths
withMimeType:(NSArray *)mimeTypes
withSubject:(NSString *)subject
withText:(NSString *)text
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
withMimeType:(NSArray *)mimeTypes
withSubject:(NSString *)subject
withText:(NSString *)text
excludedActivityType:(NSArray *)excludedActivityType
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
NSMutableArray *items = [[NSMutableArray alloc] init];

for (int i = 0; i < [paths count]; i++) {
Expand All @@ -460,10 +530,11 @@ + (void)shareFiles:(NSArray *)paths
}

[self share:items
withSubject:subject
withController:controller
atSource:origin
toResult:result];
withSubject:subject
excludedActivityType:excludedActivityType
withController:controller
atSource:origin
toResult:result];
}

@end
20 changes: 19 additions & 1 deletion packages/share_plus/share_plus/lib/share_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:ui';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';

export 'package:share_plus_platform_interface/share_plus_platform_interface.dart'
show ShareResult, ShareResultStatus, XFile;
show ShareResult, ShareResultStatus, XFile, CupertinoActivityType;

export 'src/share_plus_linux.dart';
export 'src/share_plus_windows.dart'
Expand All @@ -30,17 +30,23 @@ class Share {
/// origin rect for the share sheet to popover from on iPads and Macs. It has no effect
/// on other devices.
///
/// The optional [excludedActivityType] parameter is used to exclude services on iOS and macOS that
/// you feel are not suitable for your content.
/// It has no effect on other platforms.
///
/// May throw [PlatformException]
/// from [MethodChannel].
///
/// See documentation about [ShareResult] on [share] method.
static Future<ShareResult> shareUri(
Uri uri, {
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
}) async {
return _platform.shareUri(
uri,
sharePositionOrigin: sharePositionOrigin,
excludedActivityType: excludedActivityType,
);
}

Expand All @@ -57,6 +63,10 @@ class Share {
/// origin rect for the share sheet to popover from on iPads and Macs. It has no effect
/// on other devices.
///
/// The optional [excludedActivityType] parameter is used to exclude services on iOS and macOS that
/// you feel are not suitable for your content.
/// It has no effect on other platforms.
///
/// May throw [PlatformException] or [FormatException]
/// from [MethodChannel].
///
Expand All @@ -83,12 +93,14 @@ class Share {
String text, {
String? subject,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
}) async {
assert(text.isNotEmpty);
return _platform.share(
text,
subject: subject,
sharePositionOrigin: sharePositionOrigin,
excludedActivityType: excludedActivityType,
);
}

Expand All @@ -111,6 +123,10 @@ class Share {
/// origin rect for the share sheet to popover from on iPads and Macs. It has no effect
/// on other devices.
///
/// The optional [excludedActivityType] parameter is used to exclude services on iOS and macOS that
/// you feel are not suitable for your content.
/// It has no effect on other platforms.
///
/// The optional parameter [fileNameOverrides] can be used to override the names of shared files
/// When set, the list length must match the number of [files] to share.
/// This is useful when sharing files that were created by [`XFile.fromData`](https://github.com/flutter/packages/blob/754de1918a339270b70971b6841cf1e04dd71050/packages/cross_file/lib/src/types/io.dart#L43),
Expand All @@ -125,6 +141,7 @@ class Share {
String? subject,
String? text,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
List<String>? fileNameOverrides,
}) async {
assert(files.isNotEmpty);
Expand All @@ -133,6 +150,7 @@ class Share {
subject: subject,
text: text,
sharePositionOrigin: sharePositionOrigin,
excludedActivityType: excludedActivityType,
fileNameOverrides: fileNameOverrides,
);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/share_plus/share_plus/lib/src/share_plus_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SharePlusLinuxPlugin extends SharePlatform {
String? subject,
String? text,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
}) async {
throw UnimplementedError(
'shareUri() has not been implemented on Linux. Use share().');
Expand All @@ -35,6 +36,7 @@ class SharePlusLinuxPlugin extends SharePlatform {
String text, {
String? subject,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
}) async {
final queryParameters = {
if (subject != null) 'subject': subject,
Expand Down Expand Up @@ -68,6 +70,7 @@ class SharePlusLinuxPlugin extends SharePlatform {
String? subject,
String? text,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
List<String>? fileNameOverrides,
}) {
throw UnimplementedError(
Expand Down
3 changes: 3 additions & 0 deletions packages/share_plus/share_plus/lib/src/share_plus_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SharePlusWebPlugin extends SharePlatform {
Future<ShareResult> shareUri(
Uri uri, {
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
}) async {
final data = ShareData(
url: uri.toString(),
Expand Down Expand Up @@ -76,6 +77,7 @@ class SharePlusWebPlugin extends SharePlatform {
String text, {
String? subject,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
}) async {
final ShareData data;
if (subject != null && subject.isNotEmpty) {
Expand Down Expand Up @@ -160,6 +162,7 @@ class SharePlusWebPlugin extends SharePlatform {
String? text,
Rect? sharePositionOrigin,
List<String>? fileNameOverrides,
List<CupertinoActivityType>? excludedActivityType,
}) async {
assert(
fileNameOverrides == null || files.length == fileNameOverrides.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SharePlusWindowsPlugin extends SharePlatform {
Uri uri, {
String? subject,
String? text,
List<CupertinoActivityType>? excludedActivityType,
Rect? sharePositionOrigin,
}) async {
throw UnimplementedError(
Expand All @@ -39,6 +40,7 @@ class SharePlusWindowsPlugin extends SharePlatform {
Future<ShareResult> share(
String text, {
String? subject,
List<CupertinoActivityType>? excludedActivityType,
Rect? sharePositionOrigin,
}) async {
final queryParameters = {
Expand Down Expand Up @@ -73,6 +75,7 @@ class SharePlusWindowsPlugin extends SharePlatform {
String? subject,
String? text,
Rect? sharePositionOrigin,
List<CupertinoActivityType>? excludedActivityType,
List<String>? fileNameOverrides,
}) {
throw UnimplementedError(
Expand Down
Loading
Loading