Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[In_app_purchase]fix requesthandler crash (#1199)
Browse files Browse the repository at this point in the history
The FIAPRequestHandler is destroyed when exiting the function. Since FIAPRequestHandler is the delegate, when SKProductRequest tries to execute the delegate method, it will not be able to find the delegate. Hence the crash.

Adding a set to hold strong reference of the FIAPRequestHandler until it is complete fix the crash.
  • Loading branch information
Chris Yang committed Feb 14, 2019
1 parent d2c12d6 commit 0e72ca5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/in_app_purchase/ios/Classes/InAppPurchasePlugin.m
Expand Up @@ -7,6 +7,14 @@
#import "FIAObjectTranslator.h"
#import "FIAPRequestHandler.h"

@interface InAppPurchasePlugin ()

// Holding strong references to FIAPRequestHandlers. Remove the handlers from the set after
// the request is finished.
@property(strong, nonatomic) NSMutableSet *requestHandlers;

@end

@implementation InAppPurchasePlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
Expand Down Expand Up @@ -42,6 +50,8 @@ - (void)handleProductRequestMethodCall:(FlutterMethodCall *)call result:(Flutter
SKProductsRequest *request =
[self getProductRequestWithIdentifiers:[NSSet setWithArray:productIdentifiers]];
FIAPRequestHandler *handler = [[FIAPRequestHandler alloc] initWithRequest:request];
[self.requestHandlers addObject:handler];
__weak typeof(self) weakSelf = self;
[handler startProductRequestWithCompletionHandler:^(SKProductsResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
Expand All @@ -61,11 +71,21 @@ - (void)handleProductRequestMethodCall:(FlutterMethodCall *)call result:(Flutter
return;
}
result([response toMap]);
[weakSelf.requestHandlers removeObject:handler];
}];
}

- (SKProductsRequest *)getProductRequestWithIdentifiers:(NSSet *)identifiers {
return [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers];
}

#pragma mark - getter

- (NSSet *)requestHandlers {
if (!_requestHandlers) {
_requestHandlers = [NSMutableSet new];
}
return _requestHandlers;
}

@end

0 comments on commit 0e72ca5

Please sign in to comment.