I am attempting to return a javascript promise from an Obj-c class, but the promise returned in the script always appears undefined. Is valueWithNewPromiseInContext supported in UIWebView or only in WKWebView?
My class has a JSExport protocol:
@protocol TestMe <JSExport>
- (void)test:(JSValue*)testID;
@end
@interface TestMe : NSObject <WebAuthN>
+ (void)installInContext:(JSContext*)context;
- (JSValue*)test:(JSValue*)testID;
@end
My test method:
- (JSValue *)test:(JSValue*)testID {
if (@available(iOS 13.0, *)) {
return [JSValue valueWithNewPromiseInContext:_jsContext fromExecutor:^(JSValue *fulfill, JSValue *reject) {
LogDebug("PKWebAuthN newPromise fulfill(%@) reject(%@)\n", fulfill, reject);
// test fulfill
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
JSValue *v = [JSValue valueWithObject:@"success" inContext:self->_jsContext];
[fulfill callWithArguments:@[v]];
});
}];
} else {
_jsContext.exception = [JSValue valueWithNewErrorFromMessage:@"webauthn not supported by this iOS version" inContext:_jsContext];
return [JSValue valueWithUndefinedInContext:_jsContext];
}
}
I successfully get my TestMe object added to the javascript JSContext (my test: method does get called from javascript)
In my web page javascript, I create the object:
function testme(testNumber) {
var testit = new TestMe();
let testpromise = testit.test(testNumber);
document.getElementById("testit").innerHTML = "testpromise: " + testpromise;
console.log("testpromise: " + testpromise);
// testpromise is always undefined here!!!
try {
testpromise.then(
function fulfilled(arg){
document.getElementById("testit").innerHTML = "fulfilled: " + arg;
console.log("fulfilled: " + arg);
webauthn = nil;
},
function rejected(err) {
document.getElementById("testit").innerHTML = "rejected: " + arg;
console.log("rejected: " + err);
webauthn = nil;
}
);
} catch(err) {
console.log("caught error from testpromise: " + err);
webauthn = nil;
}
}
Where I call testit.test, the value testpromise is always undefined instead of being the promise returned from my ObjC class.
My web view is UIWebView and not WKWebView