Skip to content

Commit

Permalink
Merge pull request #209 from blueshift-labs/fix/html-inapp-retain-cycle
Browse files Browse the repository at this point in the history
HTML in-app retain cycle issue
  • Loading branch information
ketanshikharebsft committed Jun 15, 2021
2 parents 3ee760e + 43664d3 commit dcc9a0e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 48 deletions.
7 changes: 3 additions & 4 deletions BlueShift-iOS-SDK/BlueShift.m
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,11 @@ - (void)performRequestQueue:(NSMutableDictionary *)parameters canBatchThisEvent:
if([self validateSDKTrackingRequirements] == false) {
return;
}

parameters[kBrowserPlatform] = [BlueShiftDeviceData currentDeviceData].operatingSystem;

if (parameters != nil) {
NSMutableDictionary* mutableParams = [parameters mutableCopy];
[mutableParams setValue:[BlueShiftDeviceData currentDeviceData].operatingSystem forKey:kBrowserPlatform];
NSString *url = [NSString stringWithFormat:@"%@%@", kBaseURL, kPushEventsUploadURL];
BlueShiftRequestOperation *requestOperation = [[BlueShiftRequestOperation alloc] initWithRequestURL:url andHttpMethod:BlueShiftHTTPMethodGET andParameters:[parameters copy] andRetryAttemptsCount:kRequestTryMaximumLimit andNextRetryTimeStamp:0 andIsBatchEvent:isBatchEvent];
BlueShiftRequestOperation *requestOperation = [[BlueShiftRequestOperation alloc] initWithRequestURL:url andHttpMethod:BlueShiftHTTPMethodGET andParameters:[mutableParams copy] andRetryAttemptsCount:kRequestTryMaximumLimit andNextRetryTimeStamp:0 andIsBatchEvent:isBatchEvent];
[BlueShiftRequestQueue addRequestOperation:requestOperation];
}
}
Expand Down
2 changes: 1 addition & 1 deletion BlueShift-iOS-SDK/BlueShiftNotificationConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define kNotificationCategoryViewCartIdentifier @"view_cart"
#define kNotificationActionOpenCartIdentifier @"open_cart"

#define kNotificationCategorySilentPushIdentifier @"silent_push"
#define kNotificationCategorySilentPushIdentifier @"silent push"
#define kNotificationCategoryOfferIdentifier @"promotion"
#define kNotificationProductIDIdenfierKey @"product_id"
#define kNotificationSelectedIndexKey @"selected_index"
Expand Down
30 changes: 15 additions & 15 deletions BlueShift-iOS-SDK/BlueShiftUserInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,48 @@

/// Set user email id.
/// Make sure you call BlueShiftUserInfo.sharedInstance()?.save() after setting/modifying any property of user info.
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString* _Nullable email;

/// Set user customer id.
/// Make sure you call BlueShiftUserInfo.sharedInstance()?.save() after setting/modifying any property of user info.
@property (nonatomic, strong) NSString *retailerCustomerID;
@property (nonatomic, strong) NSString* _Nullable retailerCustomerID;

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString* _Nullable name;
@property (nonatomic, strong) NSString* _Nullable firstName;
@property (nonatomic, strong) NSString* _Nullable lastName;

@property (nonatomic, strong) NSDate *dateOfBirth;
@property (nonatomic, strong) NSString *gender;
@property (nonatomic, strong) NSString *education;
@property (nonatomic, strong) NSDate* _Nullable dateOfBirth;
@property (nonatomic, strong) NSString* _Nullable gender;
@property (nonatomic, strong) NSString* _Nullable education;

@property (nonatomic, strong) NSDate *joinedAt;
@property (nonatomic, strong) NSDate* _Nullable joinedAt;

@property (nonatomic, strong) NSString *facebookID;
@property (nonatomic, strong) NSString* _Nullable facebookID;

/// Unsubscribe from the push notifications.
/// Set this flag to true if you want to stop receiving push notifications for that user.
@property NSNumber* unsubscribed;
@property NSNumber* _Nullable unsubscribed;

/// The data stored in the additionalUserInfo will be populated on server with `additional_user_info__` prefix to every key name.
/// If key is stored as `profession`, then server will popluate it as `additional_user_info__profession` in the events.
/// Make sure you call BlueShiftUserInfo.sharedInstance()?.save() after setting/modifying the user info.
@property NSDictionary *additionalUserInfo;
@property NSMutableDictionary* _Nullable additionalUserInfo;

/// The data stored in the extras will be sent to server as it is as part of every event.
/// If key is stored as `profession`, then server will populate it as `profession` in the events.
/// Make sure you call BlueShiftUserInfo.sharedInstance()?.save() after setting/modifying the user info.
@property NSDictionary *extras;
@property NSMutableDictionary* _Nullable extras;

/// Call save method after making any change in the BlueshiftUserInfo data to store the data.
- (void)save;

/// Use this method to erase the stored data.
+ (void)removeCurrentUserInfo;

+ (instancetype) sharedInstance;
+ (instancetype _Nullable) sharedInstance;

/// Returns saved User info as dictionary of key value pairs
- (NSDictionary *)toDictionary;
- (NSDictionary *_Nonnull)toDictionary;

@end

55 changes: 35 additions & 20 deletions BlueShift-iOS-SDK/InApps/BlueShiftInAppNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -654,33 +654,32 @@ -(void)processHTMLNotification:(BlueShiftInAppNotification*)notification display
[BlueshiftLog logInfo:@"Creating HTML in-app notification to display on screen name" withDetails:displayOnScreen methodName:nil];
BlueShiftNotificationViewController* notificationVC = [[BlueShiftNotificationWebViewController alloc] initWithNotification:notification];
notificationVC.displayOnScreen = displayOnScreen;
notificationVC.delegate = self;
self.currentNotificationController = notificationVC;

BlueShiftNotificationWebViewController *webViewController = (BlueShiftNotificationWebViewController*) notificationVC;
[webViewController setupWebView:^{
[self presentInAppViewController:notificationVC forNotification:notification];
}];
[webViewController setupWebView];
}

// Present ViewController
- (void)presentInAppViewController:(BlueShiftNotificationViewController*)notificationController forNotification:(BlueShiftInAppNotification*)notification {
if (notificationController && [BlueshiftEventAnalyticsHelper isNotNilAndNotEmpty:notificationController.displayOnScreen]) {
if(self.inAppNotificationDisplayOnPage == nil || ![self.inAppNotificationDisplayOnPage isEqualToString:notificationController.displayOnScreen]) {
self.currentNotificationController = nil;
[BlueshiftLog logInfo:@"Skipping preseting in-app notification as current screen is different than in-app notification display on screen" withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
return;
}
}
void(^ presentInAppBlock)(void) = ^{
if (notificationController && [self inAppNotificationDisplayOnPage]) {
[BlueshiftLog logInfo:@"Presenting in-app notification on the screen name" withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
notificationController.delegate = self;
notificationController.inAppNotificationDelegate = self.inAppNotificationDelegate;
[notificationController setTouchesPassThroughWindow: notification.templateStyle.enableBackgroundAction];
[notificationController show:YES];
} else {
self.currentNotificationController = nil;
[BlueshiftLog logInfo:@"Skipping preseting in-app notification as screen is not registered to receive in-app notification" withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
@try {
if (notificationController && [self shouldDisplayInAppNotification:notificationController.displayOnScreen] == YES) {
[BlueshiftLog logInfo:@"Presenting in-app notification on the screen name" withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
if(notificationController.delegate == nil) {
notificationController.delegate = self;
}
notificationController.inAppNotificationDelegate = self.inAppNotificationDelegate;
[notificationController setTouchesPassThroughWindow: notification.templateStyle.enableBackgroundAction];
[notificationController show:YES];
} else {
self.currentNotificationController = nil;
[BlueshiftLog logInfo:@"Skipped preseting in-app notification as screen is not registered to receive in-app notification or current screen is different than in-app notification display on screen." withDetails:[self inAppNotificationDisplayOnPage] methodName:nil];
}

} @catch (NSException *exception) {
[BlueshiftLog logException:exception withDescription:nil methodName:[NSString stringWithUTF8String:__PRETTY_FUNCTION__]];
}
};

Expand All @@ -693,6 +692,22 @@ - (void)presentInAppViewController:(BlueShiftNotificationViewController*)notific
}
}


/// Check if the notification is eligible to display on the current screen.
/// @param displayOnScreen Name of screen where notification should be displayed
- (BOOL)shouldDisplayInAppNotification:(NSString*)displayOnScreen {
if ([self inAppNotificationDisplayOnPage] == nil) {
return false;
} else if ([BlueshiftEventAnalyticsHelper isNotNilAndNotEmpty:displayOnScreen]) {
if(![[self inAppNotificationDisplayOnPage] isEqualToString:displayOnScreen]) {
return false;
} else {
return true;
}
}
return true;
}

#pragma mark - In App events
// Notification Click Callbacks
-(void)inAppDidDismiss:(NSDictionary *)notificationPayload fromViewController:(BlueShiftNotificationViewController *)controller {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ - (CGSize)getAutoImageSizeForNotificationView {

NSData* imageData = [self loadAndCacheImageForURLString:self.notification.templateStyle.backgroundImage];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[BlueshiftLog logInfo:@"Downloaded Image size is" withDetails:[NSString stringWithFormat:@"H:%f, W:%f",image.size.height,image.size.width] methodName:nil];
[BlueshiftLog logInfo:@"Image size is" withDetails:[NSString stringWithFormat:@"H:%f, W:%f",image.size.height,image.size.width] methodName:nil];

// If auto height and auto width is set for image modal
// and image resolution is less than the device height and width, use the image dimention.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ NS_ASSUME_NONNULL_BEGIN
@protocol BlueShiftNotificationDelegate <NSObject>
@optional
- (void)inAppDidDismiss:(NSDictionary *)notificationPayload fromViewController:(BlueShiftNotificationViewController*)controller;
- (void)inAppActionDidTapped:(NSDictionary *)notificationActionButtonPayload fromViewController:(BlueShiftNotificationViewController *)
controller;
- (void)inAppActionDidTapped:(NSDictionary *)notificationActionButtonPayload fromViewController:(BlueShiftNotificationViewController *)controller;
- (void)inAppDidShow:(NSDictionary *)notification fromViewController:(BlueShiftNotificationViewController*)controller;
- (void)presentInAppViewController:(BlueShiftNotificationViewController*)notificationController forNotification:(BlueShiftInAppNotification*)notification;
@end

@interface BlueShiftNotificationViewController : UIViewController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
NS_ASSUME_NONNULL_BEGIN

@interface BlueShiftNotificationWebViewController : BlueShiftNotificationViewController
- (void)setupWebView:(void (^)(void))block;
- (void)setupWebView;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ @interface BlueShiftNotificationWebViewController ()<WKNavigationDelegate, UIGes
WKWebView *webView;
BOOL isAutoHeight;
BOOL isAutoWidth;
void (^ didLoadWebView)(void);
}

@property(nonatomic, retain) UIPanGestureRecognizer *panGesture;
Expand All @@ -42,8 +41,7 @@ - (void)viewDidLoad {
[super viewDidLoad];
}

- (void)setupWebView:(void (^)(void))block {
didLoadWebView = block;
- (void)setupWebView {
[self setAutomaticScale];
[self configureBackground];
[self presentWebViewNotification];
Expand Down Expand Up @@ -293,7 +291,9 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat
if (complete) {
[BlueshiftLog logInfo:@"Webview loaded the content successfully." withDetails:nil methodName:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
self->didLoadWebView();
if ([self delegate] && [[self delegate] respondsToSelector:@selector(presentInAppViewController:forNotification:)]) {
[[self delegate] presentInAppViewController:self forNotification:self.notification];
}
[self resizeWebViewAsPerContent:webView];
});
}
Expand Down

0 comments on commit dcc9a0e

Please sign in to comment.