Skip to content

Commit

Permalink
fixed js script indetention
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Espinola committed May 29, 2024
1 parent 8201153 commit a54fb89
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 78 deletions.
53 changes: 28 additions & 25 deletions ATTNSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public final class ATTNSDK: NSObject {
}

// MARK: Public API
public func identify(userIdentifiers: [String: String]) {
public func identify(userIdentifiers: [String: Any]) {
userIdentity.mergeIdentifiers(userIdentifiers)
api.send(userIdentity)
}
Expand Down Expand Up @@ -172,31 +172,34 @@ extension ATTNSDK: WKScriptMessageHandler {
// MARK: WKNavigationDelegate
extension ATTNSDK: WKNavigationDelegate {
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let asyncJs = """
var p = new Promise(resolve => {
var timeoutHandle = null;
const interval = setInterval(function() {
e = document.querySelector('iframe');
if(e && e.id === 'attentive_creative') {
clearInterval(interval);
resolve('SUCCESS');
if (timeoutHandle != null) {
clearTimeout(timeoutHandle);
}
}
}, 100);
timeoutHandle = setTimeout(function() {
clearInterval(interval);
resolve('TIMED OUT');
}, 5000);
});
var status = await p;
status;
"""

guard #available(iOS 14.0, *) else { return }

webView.callAsyncJavaScript(asyncJs, in: nil, in: .defaultClient) { [weak self] result in
let asyncJs =
"""
var p = new Promise(resolve => {
var timeoutHandle = null;
const interval = setInterval(function() {
e = document.querySelector('iframe');
if(e && e.id === 'attentive_creative') {
clearInterval(interval);
resolve('SUCCESS');
if (timeoutHandle != null) {
clearTimeout(timeoutHandle);
}
}
}, 100);
timeoutHandle = setTimeout(function() {
clearInterval(interval);
resolve('TIMED OUT');
}, 5000);
});
var status = await p;
return status;
"""
webView.callAsyncJavaScript(
asyncJs,
in: nil,
in: .defaultClient
) { [weak self] result in
guard let self = self else { return }
guard case let .success(statusAny) = result else {
NSLog("No status returned from JS. Not showing WebView.")
Expand Down
26 changes: 13 additions & 13 deletions Example/Example/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ - (void)viewDidLoad {
}

// Replace with your Attentive domain to test with your Attentive account
_domain = @"YOUR_ATTENTIVE_DOMAIN";
// _domain = @"YOUR_ATTENTIVE_DOMAIN";
_domain = @"mobileapps";
_mode = @"production";

// Setup for Testing purposes only
Expand Down Expand Up @@ -76,18 +77,17 @@ - (IBAction)creativeButtonPress:(id)sender {

// Display the creative, with a callback handler
// You can also call [sdk trigger:self.view] without a callback handler
[sdk trigger:self.view
handler:^(NSString *triggerStatus) {
if (triggerStatus == CREATIVE_TRIGGER_STATUS_OPENED) {
NSLog(@"Opened the Creative!");
} else if (triggerStatus == CREATIVE_TRIGGER_STATUS_NOT_OPENED) {
NSLog(@"Couldn't open the Creative!");
} else if (triggerStatus == CREATIVE_TRIGGER_STATUS_CLOSED) {
NSLog(@"Closed the Creative!");
} else if (triggerStatus == CREATIVE_TRIGGER_STATUS_NOT_CLOSED) {
NSLog(@"Couldn't close the Creative!");
}
}];
[sdk trigger:self.view handler:^(NSString * _Nonnull triggerStatus) {
if (triggerStatus == CREATIVE_TRIGGER_STATUS_OPENED) {
NSLog(@"Opened the Creative!");
} else if (triggerStatus == CREATIVE_TRIGGER_STATUS_NOT_OPENED) {
NSLog(@"Couldn't open the Creative!");
} else if (triggerStatus == CREATIVE_TRIGGER_STATUS_CLOSED) {
NSLog(@"Closed the Creative!");
} else if (triggerStatus == CREATIVE_TRIGGER_STATUS_NOT_CLOSED) {
NSLog(@"Couldn't close the Creative!");
}
}];
}

- (IBAction)sendIdentifiersButtonPress:(id)sender {
Expand Down
23 changes: 0 additions & 23 deletions ExampleSwift/ExampleSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
588EB7262995BC3A00C0163A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 587DC5C82995551A0082F8D0 /* Main.storyboard */; };
FB0E49E92BFBBA2F0025E281 /* attentive_ios_sdk_framework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FB0E49E82BFBBA2F0025E281 /* attentive_ios_sdk_framework.framework */; };
FB0E49EA2BFBBA2F0025E281 /* attentive_ios_sdk_framework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FB0E49E82BFBBA2F0025E281 /* attentive_ios_sdk_framework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
FB9EB7E72C067B3800B80F2D /* ATTNSDKFramework in Frameworks */ = {isa = PBXBuildFile; productRef = FB9EB7E62C067B3800B80F2D /* ATTNSDKFramework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -67,7 +66,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
FB9EB7E72C067B3800B80F2D /* ATTNSDKFramework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -167,7 +165,6 @@
);
name = "ExampleSwift - SPM";
packageProductDependencies = (
FB9EB7E62C067B3800B80F2D /* ATTNSDKFramework */,
);
productName = ExampleSwift;
productReference = 588EB72C2995BC3A00C0163A /* ExampleSwift - SPM.app */;
Expand Down Expand Up @@ -198,7 +195,6 @@
);
mainGroup = 58389AA12987647900A31A35;
packageReferences = (
FB9EB7E52C067B3800B80F2D /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */,
);
productRefGroup = 58389AAB2987647900A31A35 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -596,25 +592,6 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
FB9EB7E52C067B3800B80F2D /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/vespinola/attentive-ios-sdk.git";
requirement = {
branch = main;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
FB9EB7E62C067B3800B80F2D /* ATTNSDKFramework */ = {
isa = XCSwiftPackageProductDependency;
package = FB9EB7E52C067B3800B80F2D /* XCRemoteSwiftPackageReference "attentive-ios-sdk" */;
productName = ATTNSDKFramework;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 58389AA22987647900A31A35 /* Project object */;
}
4 changes: 2 additions & 2 deletions Sources/Objc/ObjcATTNSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public final class ObjcATTNSDK: NSObject {

@objc(identify:)
public func identify(userIdentifiers: NSDictionary) {
guard let dict = userIdentifiers as? [String: String] else {
NSLog("UserIdentifiers casting to [String: String] failed")
guard let dict = userIdentifiers as? [String: Any] else {
NSLog("UserIdentifiers casting to [String: Any] failed")
return
}
sdk.identify(userIdentifiers: dict)
Expand Down

This file was deleted.

0 comments on commit a54fb89

Please sign in to comment.