-
Notifications
You must be signed in to change notification settings - Fork 73
feat(rokt): add setSession/getSession with optional sessionToken #816
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c31356a
feat(rokt): add setSession/getSession with optional sessionToken
jaissica12 b053dcd
ci: Add Live Test to Example App
BrandonStalnaker bd05c1a
Updatre for Rokt-Widget 5.4.0 release
BrandonStalnaker fea736b
Merge branch 'main' into feature/rokt-session-token-handoff
BrandonStalnaker 159df3a
fix(rokt): enqueue setSession on the message queue with selectPlacements
BrandonStalnaker 9a35bbf
Merge branch 'main' into feature/rokt-session-token-handoff
BrandonStalnaker 993af9b
Merge remote-tracking branch 'refs/remotes/public/main' into feature/…
BrandonStalnaker d7ba2d8
fix(rokt): preserve id-only session handoff
BrandonStalnaker afad498
test(rokt): cover session handoff branches
BrandonStalnaker 2088e32
fix(rokt): require complete token sessions
BrandonStalnaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #import <UIKit/UIKit.h> | ||
|
|
||
| @class MPRoktSession; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /** | ||
| * Loads a bundled mParticle Web SDK page and seeds Rokt launcherOptions from a native session. | ||
| */ | ||
| @interface RoktHybridWebViewController : UIViewController | ||
|
|
||
| - (instancetype)initWithSession:(MPRoktSession *)session NS_DESIGNATED_INITIALIZER; | ||
|
|
||
| - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil | ||
| bundle:(nullable NSBundle *)nibBundleOrNil NS_UNAVAILABLE; | ||
| - (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE; | ||
| - (instancetype)init NS_UNAVAILABLE; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| #import "RoktHybridWebViewController.h" | ||
|
|
||
| #import <WebKit/WebKit.h> | ||
| #import <mParticle_Apple_SDK_ObjC/mParticle.h> | ||
|
|
||
| static NSString *const kRoktHybridMessageHandlerName = @"roktSession"; | ||
| static NSString *const kRoktHybridHTMLResource = @"rokt-hybrid"; | ||
|
|
||
| @interface MPEWeakScriptMessageHandler : NSObject <WKScriptMessageHandler> | ||
| @property (nonatomic, weak) id<WKScriptMessageHandler> target; | ||
| @end | ||
|
|
||
| @implementation MPEWeakScriptMessageHandler | ||
| - (void)userContentController:(WKUserContentController *)userContentController | ||
| didReceiveScriptMessage:(WKScriptMessage *)message { | ||
| [self.target userContentController:userContentController didReceiveScriptMessage:message]; | ||
| } | ||
| @end | ||
|
|
||
| @interface RoktHybridWebViewController () <WKScriptMessageHandler> | ||
|
|
||
| @property (nonatomic, strong) MPRoktSession *session; | ||
| @property (nonatomic, strong) WKWebView *webView; | ||
| @property (nonatomic, strong) MPEWeakScriptMessageHandler *messageHandler; | ||
|
|
||
| @end | ||
|
|
||
| @implementation RoktHybridWebViewController | ||
|
|
||
| - (instancetype)initWithSession:(MPRoktSession *)session { | ||
| self = [super initWithNibName:nil bundle:nil]; | ||
| if (self) { | ||
| _session = session; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)dealloc { | ||
| [_webView.configuration.userContentController removeScriptMessageHandlerForName:kRoktHybridMessageHandlerName]; | ||
| } | ||
|
|
||
| - (void)viewDidLoad { | ||
| [super viewDidLoad]; | ||
|
|
||
| self.title = @"Rokt WebView"; | ||
| self.view.backgroundColor = [UIColor systemBackgroundColor]; | ||
| self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone | ||
| target:self | ||
| action:@selector(dismissHybrid)]; | ||
|
|
||
| WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; | ||
| WKUserScript *handoffScript = [[WKUserScript alloc] initWithSource:[self handoffJavaScript] | ||
| injectionTime:WKUserScriptInjectionTimeAtDocumentStart | ||
| forMainFrameOnly:YES]; | ||
| [configuration.userContentController addUserScript:handoffScript]; | ||
| self.messageHandler = [[MPEWeakScriptMessageHandler alloc] init]; | ||
| self.messageHandler.target = self; | ||
| [configuration.userContentController addScriptMessageHandler:self.messageHandler name:kRoktHybridMessageHandlerName]; | ||
|
|
||
| self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; | ||
| self.webView.translatesAutoresizingMaskIntoConstraints = NO; | ||
| [self.view addSubview:self.webView]; | ||
|
|
||
| [NSLayoutConstraint activateConstraints:@[ | ||
| [self.webView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor], | ||
| [self.webView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | ||
| [self.webView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | ||
| [self.webView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor] | ||
| ]]; | ||
|
|
||
| [[MParticle sharedInstance] initializeWKWebView:self.webView]; | ||
|
|
||
| NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:kRoktHybridHTMLResource withExtension:@"html"]; | ||
| if (!htmlURL) { | ||
| NSLog(@"Rokt hybrid: bundled rokt-hybrid.html is missing"); | ||
| return; | ||
| } | ||
| [self.webView loadFileURL:htmlURL allowingReadAccessToURL:htmlURL.URLByDeletingLastPathComponent]; | ||
| } | ||
|
|
||
| - (void)dismissHybrid { | ||
| [self dismissViewControllerAnimated:YES completion:nil]; | ||
| } | ||
|
|
||
| - (NSString *)handoffJavaScript { | ||
| NSMutableDictionary *handoff = [NSMutableDictionary dictionary]; | ||
| if (self.session.sessionId.length > 0) { | ||
| handoff[@"sessionId"] = self.session.sessionId; | ||
| } | ||
| if (self.session.sessionToken.length > 0) { | ||
| handoff[@"sessionToken"] = self.session.sessionToken; | ||
| } | ||
| if (self.session.expiresAt != nil) { | ||
| handoff[@"expiresAt"] = self.session.expiresAt; | ||
| } | ||
|
|
||
| NSError *error = nil; | ||
| NSData *jsonData = [NSJSONSerialization dataWithJSONObject:handoff options:0 error:&error]; | ||
| if (!jsonData || error) { | ||
| NSLog(@"Rokt hybrid: failed to serialize session handoff: %@", error); | ||
| return @"window.__ROKT_HANDOFF = {};"; | ||
| } | ||
|
|
||
| NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; | ||
| return [NSString stringWithFormat:@"window.__ROKT_HANDOFF = %@;", json]; | ||
| } | ||
|
|
||
| - (void)userContentController:(WKUserContentController *)userContentController | ||
| didReceiveScriptMessage:(WKScriptMessage *)message { | ||
| if (![message.name isEqualToString:kRoktHybridMessageHandlerName]) { | ||
| return; | ||
| } | ||
| if (![message.body isKindOfClass:[NSDictionary class]]) { | ||
| NSLog(@"Rokt hybrid: expected dictionary from webview, got %@", [message.body class]); | ||
| return; | ||
| } | ||
|
|
||
| NSDictionary *body = (NSDictionary *)message.body; | ||
| NSString *sessionId = [self stringValue:body[@"sessionId"]]; | ||
| if (sessionId.length == 0) { | ||
| NSLog(@"Rokt hybrid: webview posted an empty sessionId"); | ||
| return; | ||
| } | ||
|
|
||
| NSString *sessionToken = [self stringValue:body[@"sessionToken"]]; | ||
| NSNumber *expiresAt = [self numberValue:body[@"expiresAt"]]; | ||
| if (sessionToken.length == 0 || expiresAt == nil) { | ||
| NSLog(@"Rokt hybrid: webview posted an incomplete token-bearing session"); | ||
| return; | ||
| } | ||
| MPRoktSession *session = [[MPRoktSession alloc] initWithSessionId:sessionId | ||
| sessionToken:sessionToken | ||
| expiresAt:expiresAt]; | ||
|
|
||
| NSLog(@"Rokt hybrid: applying web session to native (id=%@, token=%@)", | ||
| session.sessionId, | ||
| session.sessionToken.length > 0 ? @"present" : @"nil"); | ||
|
|
||
| [[MParticle sharedInstance].rokt setSession:session]; | ||
| NSDictionary<NSString *, NSString *> *attributes = @{ | ||
| @"email": @"j.smit@example.com", | ||
| @"firstname": @"Jenny", | ||
| @"lastname": @"Smith", | ||
| @"sandbox": @"true", | ||
| @"mobile": @"(555)867-5309" | ||
| }; | ||
| [[MParticle sharedInstance].rokt selectPlacements:@"RoktLayout" attributes:attributes]; | ||
| } | ||
|
|
||
| - (nullable NSString *)stringValue:(id)value { | ||
| if ([value isKindOfClass:[NSString class]]) { | ||
| return (NSString *)value; | ||
| } | ||
| if ([value isKindOfClass:[NSNumber class]]) { | ||
| return [(NSNumber *)value stringValue]; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| - (nullable NSNumber *)numberValue:(id)value { | ||
| if ([value isKindOfClass:[NSNumber class]]) { | ||
| return (NSNumber *)value; | ||
| } | ||
| if ([value isKindOfClass:[NSString class]]) { | ||
| NSString *string = (NSString *)value; | ||
| if (string.length == 0) { | ||
| return nil; | ||
| } | ||
| return @([string longLongValue]); | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.