Skip to content

Commit

Permalink
[In_app_purchase]fix requesthandler crash (flutter#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 authored and Altercode IT Solutions committed Feb 19, 2019
1 parent f6f60d5 commit 803fee9
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 803fee9

Please sign in to comment.