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

fix: resolve promise on ios every time #969

Conversation

krystofcelba
Copy link
Contributor

@krystofcelba krystofcelba commented Feb 22, 2021

Overview

The Share.open promise doesn't resolve in some situations on iOS. For example when you cancel printing. It's well described in #865. This PR fixes that.

Fix #865 #857

Test Plan

Please check #865, there are steps for reproduce and it should work as expected with this fix.

@krystofcelba krystofcelba changed the title fix: resolve callback on ios every time on completion fix: resolve promise on ios every time Feb 22, 2021
@@ -266,7 +266,7 @@ - (NSDictionary *)constantsToExport
if (activityError) {
[controller dismissViewControllerAnimated:true completion:nil];
failureCallback(activityError);
} else if (completed || activityType == nil) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition here is unnecessary as the completion is checked here in JS code and in situations like canceled print it causes that the JS callback isn't called at all. So the bug is fixed just by removing the condition and calling the callback every time except there is activityError.

Copy link
Collaborator

@MateusAndrade MateusAndrade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!! Thanks, @krystofcelba! 🚀 🚀 🚀

@MateusAndrade MateusAndrade merged commit 8be5897 into react-native-share:master Feb 23, 2021
MateusAndrade pushed a commit that referenced this pull request Feb 23, 2021
## [5.1.2](v5.1.1...v5.1.2) (2021-02-23)

### Bug Fixes

* resolve callback on ios every time on completion ([#969](#969)) ([8be5897](8be5897))
@MateusAndrade
Copy link
Collaborator

🎉 This PR is included in version 5.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@cristianoccazinsp
Copy link
Contributor

This introduced the following bug: #972

I'm already testing a "fix", but it would be great to get @krystofcelba input.

@cristianoccazinsp
Copy link
Contributor

Proposed update:

__weak UIActivityViewController* weakShareController = shareController;
    shareController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
        
        // always dismiss since this may be called from cancelled shares
        // but the share menu would remain open, and our callback would fire again on close
        [controller dismissViewControllerAnimated:true completion:nil];
        
        if (activityError) {
            failureCallback(activityError);
        } else {
            successCallback(@[@(completed), RCTNullIfNil(activityType)]);
        }
        
        // clear the completion handler to prevent cycles
        if(weakShareController){
            weakShareController.completionWithItemsHandler = nil;
        }
    };

This has the side effect that the share UI is always closed if the selected app was "cancelled". However, it doesn't seem possible to detect if the share UI was closed automatically by the system or not

mobiledev7 added a commit to mobiledev7/react-native-share that referenced this pull request Dec 12, 2022
## [5.1.2](react-native-share/react-native-share@v5.1.1...v5.1.2) (2021-02-23)

### Bug Fixes

* resolve callback on ios every time on completion ([#969](react-native-share/react-native-share#969)) ([8be5897](react-native-share/react-native-share@8be5897))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Promise "open" is never resolved when canceling printing
3 participants