|
8 | 8 | #include "core/config/project_settings.h" |
9 | 9 |
|
10 | 10 | #import "share_plugin_implementation.h" |
| 11 | +#import "active_view_controller.h" |
11 | 12 |
|
12 | 13 | String const DATA_KEY_TITLE = "title"; |
13 | 14 | String const DATA_KEY_SUBJECT = "subject"; |
14 | 15 | 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/*"; |
16 | 21 |
|
17 | 22 | String const SIGNAL_NAME_SHARE_COMPLETED = "share_completed"; |
18 | 23 |
|
|
26 | 31 | Error SharePlugin::share(const Dictionary &sharedData) { |
27 | 32 | NSLog(@"SharePlugin::share"); |
28 | 33 |
|
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]; |
30 | 58 |
|
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 | + } |
34 | 63 |
|
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]; |
41 | 67 | } |
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]; |
52 | 72 | } |
53 | 73 |
|
| 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 | + |
54 | 111 | return OK; |
55 | 112 | } |
56 | 113 |
|
|
0 commit comments