@@ -17,7 +17,8 @@ namespace Plugin.PaystackInline.Forms.Plugin.iOS
17
17
{
18
18
public class PaystackWebViewRenderer : ViewRenderer < PaystackWebView , WKWebView > , IWKScriptMessageHandler
19
19
{
20
- const string JavaScriptFunction = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}" ;
20
+ const string PaymentJavaScriptFunction = "function invokePaymentAction(data){window.webkit.messageHandlers.invokePayAction.postMessage(data);}" ;
21
+ const string ClosePaymentJavaScriptFunction = "function invokeClosePaymentAction(data){window.webkit.messageHandlers.invokeCloseAction.postMessage(data);}" ;
21
22
WKUserContentController userController ;
22
23
23
24
protected override void OnElementChanged ( ElementChangedEventArgs < HybridWebView > e )
@@ -26,10 +27,16 @@ protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView>
26
27
27
28
if ( Control == null )
28
29
{
30
+ var webviewElement = ( HybridWebView ) Element ;
29
31
userController = new WKUserContentController ( ) ;
30
- var script = new WKUserScript ( new NSString ( JavaScriptFunction ) , WKUserScriptInjectionTime . AtDocumentEnd , false ) ;
31
- userController . AddUserScript ( script ) ;
32
- userController . AddScriptMessageHandler ( this , "invokeAction" ) ;
32
+ var paymentscript = new WKUserScript ( new NSString ( PaymentJavaScriptFunction ) , WKUserScriptInjectionTime . AtDocumentEnd , false ) ;
33
+ var closeScript = new WKUserScript ( new NSString ( ClosePaymentJavaScriptFunction ) , WKUserScriptInjectionTime . AtDocumentEnd , false ) ;
34
+ var paystackScript = new WKUserScript ( new NSString ( $ "payWithPaystack({ webviewElement . Data } )") , WKUserScriptInjectionTime . AtDocumentEnd , false ) ;
35
+ userController . AddUserScript ( paymentscript ) ;
36
+ userController . AddUserScript ( closeScript ) ;
37
+ userController . AddUserScript ( paystackScript ) ;
38
+ userController . AddScriptMessageHandler ( this , "invokePayAction" ) ;
39
+ userController . AddScriptMessageHandler ( this , "invokeCloseAction" ) ;
33
40
34
41
var config = new WKWebViewConfiguration { UserContentController = userController } ;
35
42
var webView = new WKWebView ( Frame , config ) ;
@@ -38,7 +45,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView>
38
45
if ( e . OldElement != null )
39
46
{
40
47
userController . RemoveAllUserScripts ( ) ;
41
- userController . RemoveScriptMessageHandler ( "invokeAction" ) ;
48
+ userController . RemoveScriptMessageHandler ( "invokePayAction" ) ;
49
+ userController . RemoveScriptMessageHandler ( "invokeCloseAction" ) ;
42
50
var hybridWebView = e . OldElement as HybridWebView ;
43
51
hybridWebView . CleanUp ( ) ;
44
52
}
@@ -51,7 +59,16 @@ protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView>
51
59
52
60
public void DidReceiveScriptMessage ( WKUserContentController userContentController , WKScriptMessage message )
53
61
{
54
- Element . InvokeCallbackAction ( message . Body . ToString ( ) ) ;
62
+ if ( message . Name == "invokePayAction" )
63
+ {
64
+ Element . InvokeCallbackAction ( message . Body . ToString ( ) ) ;
65
+ }
66
+ if ( message . Name == "invokeCloseAction" )
67
+ {
68
+ Element . InvokeCloseAction ( ) ;
69
+ }
70
+
55
71
}
72
+
56
73
}
57
74
}
0 commit comments