Skip to content

Commit 0b5a718

Browse files
committed
Fixed image and file sharing.
1 parent d8e15ad commit 0b5a718

File tree

4 files changed

+135
-22
lines changed

4 files changed

+135
-22
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// © 2024-present https://github.com/cengiz-pz
3+
//
4+
5+
#import <Foundation/Foundation.h>
6+
#import <UIKit/UIKit.h>
7+
8+
@interface ActiveViewController : NSObject
9+
10+
+ (UIViewController*) getActiveViewController;
11+
12+
@end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// © 2024-present https://github.com/cengiz-pz
3+
//
4+
5+
#import "active_view_controller.h"
6+
7+
@implementation ActiveViewController : NSObject
8+
9+
+ (UIViewController*) getActiveViewController {
10+
UIWindow *keyWindow = nil;
11+
for (UIWindowScene *scene in UIApplication.sharedApplication.connectedScenes) {
12+
if (scene.activationState == UISceneActivationStateForegroundActive) {
13+
for (UIWindow *window in scene.windows) {
14+
if (window.isKeyWindow) {
15+
keyWindow = window;
16+
break;
17+
}
18+
}
19+
}
20+
}
21+
22+
if (!keyWindow) {
23+
keyWindow = UIApplication.sharedApplication.windows.firstObject;
24+
}
25+
26+
UIViewController *activeVC = keyWindow.rootViewController;
27+
while (activeVC.presentedViewController) {
28+
activeVC = activeVC.presentedViewController;
29+
}
30+
31+
if ([activeVC isKindOfClass:[UINavigationController class]]) {
32+
activeVC = [(UINavigationController *)activeVC topViewController];
33+
} else if ([activeVC isKindOfClass:[UITabBarController class]]) {
34+
activeVC = [(UITabBarController *)activeVC selectedViewController];
35+
if ([activeVC isKindOfClass:[UINavigationController class]]) {
36+
activeVC = [(UINavigationController *)activeVC topViewController];
37+
}
38+
}
39+
40+
return activeVC;
41+
}
42+
43+
@end

ios/SharePlugin/share_plugin_implementation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
extern String const DATA_KEY_TITLE;
1212
extern String const DATA_KEY_SUBJECT;
1313
extern String const DATA_KEY_CONTENT;
14-
extern String const DATA_KEY_IMAGE_PATH;
14+
extern String const DATA_KEY_FILE_PATH;
15+
extern String const DATA_KEY_MIME_TYPE;
1516

1617
extern String const SIGNAL_NAME_SHARE_COMPLETED;
1718

ios/SharePlugin/share_plugin_implementation.mm

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
#include "core/config/project_settings.h"
99

1010
#import "share_plugin_implementation.h"
11+
#import "active_view_controller.h"
1112

1213
String const DATA_KEY_TITLE = "title";
1314
String const DATA_KEY_SUBJECT = "subject";
1415
String const DATA_KEY_CONTENT = "content";
15-
String const DATA_KEY_IMAGE_PATH = "image_path";
16+
String const DATA_KEY_FILE_PATH = "file_path";
17+
String const DATA_KEY_MIME_TYPE = "mime_type";
18+
19+
String const MIME_TYPE_TEXT = "text/plain";
20+
String const MIME_TYPE_IMAGE = "image/*";
1621

1722
String const SIGNAL_NAME_SHARE_COMPLETED = "share_completed";
1823

@@ -26,31 +31,83 @@
2631
Error SharePlugin::share(const Dictionary &sharedData) {
2732
NSLog(@"SharePlugin::share");
2833

29-
UIViewController* rootController = [[UIApplication sharedApplication] delegate].window.rootViewController;
34+
UIViewController *viewController = [ActiveViewController getActiveViewController];
35+
if (!viewController) {
36+
NSLog(@"No active view controller found");
37+
return OK;
38+
}
39+
40+
// Items to share
41+
NSString *textToShare = toNsString(sharedData[DATA_KEY_CONTENT]);
42+
43+
UIImage *imageToShare;
44+
NSURL *fileURL;
45+
46+
if (sharedData.has(DATA_KEY_FILE_PATH) && sharedData.has(DATA_KEY_MIME_TYPE)) {
47+
NSString *mimeType = toNsString(sharedData[DATA_KEY_MIME_TYPE]);
48+
if ([mimeType isEqualToString:toNsString(MIME_TYPE_IMAGE)]) {
49+
imageToShare = [UIImage imageWithContentsOfFile: toNsString(sharedData[DATA_KEY_FILE_PATH])];
50+
}
51+
else {
52+
fileURL = [NSURL fileURLWithPath: toNsString(sharedData[DATA_KEY_FILE_PATH])];
53+
}
54+
}
55+
56+
// Array of items to share
57+
NSMutableArray *itemsToShare = [NSMutableArray array];
3058

31-
NSArray* sharedItems = sharedData.has(DATA_KEY_IMAGE_PATH) ?
32-
@[toNsString(sharedData[DATA_KEY_CONTENT]), [UIImage imageWithContentsOfFile: toNsString(sharedData[DATA_KEY_IMAGE_PATH])]] :
33-
@[toNsString(sharedData[DATA_KEY_CONTENT])];
59+
// Add text
60+
if (textToShare) {
61+
[itemsToShare addObject:textToShare];
62+
}
3463

35-
UIActivityViewController* avc = [[UIActivityViewController alloc] initWithActivityItems: sharedItems applicationActivities: nil];
36-
// if iPhone
37-
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
38-
[rootController presentViewController: avc animated: YES completion: ^() {
39-
emit_signal(SIGNAL_NAME_SHARE_COMPLETED);
40-
}];
64+
// Add image
65+
if (imageToShare) {
66+
[itemsToShare addObject:imageToShare];
4167
}
42-
// if iPad
43-
else {
44-
// Change Rect to position Popover
45-
avc.modalPresentationStyle = UIModalPresentationPopover;
46-
avc.popoverPresentationController.sourceView = rootController.view;
47-
avc.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(rootController.view.bounds), CGRectGetMidY(rootController.view.bounds),0,0);
48-
avc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection(0);
49-
[rootController presentViewController: avc animated: YES completion: ^() {
50-
emit_signal(SIGNAL_NAME_SHARE_COMPLETED);
51-
}];
68+
69+
// Add file
70+
if (fileURL) {
71+
[itemsToShare addObject:fileURL];
5272
}
5373

74+
// Check if there are items to share
75+
if (itemsToShare.count == 0) {
76+
NSLog(@"No items to share");
77+
return OK;
78+
}
79+
80+
// Initialize UIActivityViewController
81+
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
82+
83+
// Exclude specific activity types (all available in iOS 14.3)
84+
activityVC.excludedActivityTypes = @[
85+
UIActivityTypePrint,
86+
UIActivityTypeAssignToContact,
87+
UIActivityTypeAddToReadingList,
88+
UIActivityTypeMarkupAsPDF
89+
];
90+
91+
// For iPad: Configure popover presentation
92+
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
93+
activityVC.popoverPresentationController.sourceView = viewController.view;
94+
activityVC.popoverPresentationController.sourceRect = CGRectMake(viewController.view.bounds.size.width / 2.0, viewController.view.bounds.size.height / 2.0, 1.0, 1.0);
95+
}
96+
97+
// Present the share sheet on the main thread
98+
dispatch_async(dispatch_get_main_queue(), ^{
99+
[viewController presentViewController:activityVC animated:YES completion:nil];
100+
});
101+
102+
// Handle completion (optional)
103+
activityVC.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
104+
if (completed) {
105+
NSLog(@"Share completed via %@", activityType);
106+
} else {
107+
NSLog(@"Share cancelled or failed with error: %@", activityError.localizedDescription);
108+
}
109+
};
110+
54111
return OK;
55112
}
56113

0 commit comments

Comments
 (0)