Skip to content

Commit d65db64

Browse files
committed
Wire UAMessageCenterNativeBridge to handle bridge and dls in webview
1 parent 2b7c49b commit d65db64

3 files changed

Lines changed: 69 additions & 14 deletions

File tree

MauiSample/run-ios.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2626
PROJECT_DIR="$SCRIPT_DIR"
2727
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
2828

29-
# Ensure .NET is in PATH
30-
export PATH="$HOME/.dotnet:$PATH"
29+
# Ensure .NET is in PATH (prefer arm64 build if available)
30+
if [[ -f "$HOME/.dotnet10arm64/dotnet" ]]; then
31+
export PATH="$HOME/.dotnet10arm64:$PATH"
32+
else
33+
export PATH="$HOME/.dotnet:$PATH"
34+
fi
3135

3236
# Check if dotnet is available
3337
if ! command -v dotnet &> /dev/null; then

src/Airship.Net.MessageCenter/Controls/Platforms/iOS/MessageViewHandler.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public partial class MessageViewHandler : ViewHandler<MessageView, UIView>
1414
private UIView _containerView = null!;
1515
private WKWebView _webView = null!;
1616
private NSObject? _webViewObserver;
17+
private UAMessageCenterNativeBridge _nativeBridge = null!;
1718

1819
public MessageViewHandler() : base(PropertyMapper, CommandMapper)
1920
{
@@ -27,16 +28,19 @@ protected override UIView CreatePlatformView()
2728
{
2829
_containerView = new UIView();
2930
_containerView.BackgroundColor = UIColor.SystemBackground;
30-
31+
3132
// Create web view with configuration
3233
var config = new WKWebViewConfiguration();
3334
config.WebsiteDataStore = WKWebsiteDataStore.DefaultDataStore;
3435
config.Preferences.JavaScriptEnabled = true;
35-
36+
3637
_webView = new WKWebView(_containerView.Bounds, config);
3738
_webView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
38-
_webView.NavigationDelegate = new MessageWebViewDelegate(this);
39-
39+
40+
_nativeBridge = new UAMessageCenterNativeBridge();
41+
_nativeBridge.ForwardNavigationDelegate = new MessageWebViewDelegate(this);
42+
_webView.WeakNavigationDelegate = _nativeBridge.NavigationDelegate;
43+
4044
_containerView.AddSubview(_webView);
4145

4246
return _containerView;
@@ -59,7 +63,10 @@ protected override void DisconnectHandler(UIView platformView)
5963
_webViewObserver.Dispose();
6064
_webViewObserver = null;
6165
}
62-
66+
67+
_nativeBridge?.Dispose();
68+
_nativeBridge = null!;
69+
6370
_webView?.RemoveFromSuperview();
6471
_webView?.Dispose();
6572
_webView = null;
@@ -101,22 +108,21 @@ private void LoadMessage(string messageId)
101108
var bodyUrl = message.BodyURL;
102109
if (bodyUrl != null)
103110
{
104-
// Get authentication credentials
105-
AWAirshipWrapper.GetMessageCenterUserAuth((authString) =>
111+
// Get user for native bridge and auth
112+
AWAirshipWrapper.Shared.MessageCenter.Inbox.GetUserWithCompletionHandler((user) =>
106113
{
107114
NSRunLoop.Main.InvokeOnMainThread(() =>
108115
{
109-
if (authString != null)
116+
if (user != null)
110117
{
111-
// Create request with authentication header
118+
_nativeBridge.SetMessage(message, user);
112119
var mutableRequest = new NSMutableUrlRequest(bodyUrl);
113-
// The authString from basicAuthString already includes "Basic " prefix
114-
mutableRequest["Authorization"] = authString;
120+
mutableRequest["Authorization"] = user.BasicAuthString;
115121
_webView.LoadRequest(mutableRequest);
116122
}
117123
else
118124
{
119-
// Fallback to loading without auth if auth retrieval fails
125+
// Fallback to loading without auth if user retrieval fails
120126
var request = new NSUrlRequest(bodyUrl);
121127
_webView.LoadRequest(request);
122128
}
@@ -163,6 +169,21 @@ public void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigat
163169
{
164170
_handler.VirtualView?.SendLoadFailed(error.LocalizedDescription);
165171
}
172+
173+
[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
174+
public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
175+
{
176+
var url = navigationAction.Request.Url;
177+
if (url != null && url.Scheme != "http" && url.Scheme != "https")
178+
{
179+
decisionHandler(WKNavigationActionPolicy.Cancel);
180+
UAirship.ProcessDeepLink(url, (_) => { });
181+
}
182+
else
183+
{
184+
decisionHandler(WKNavigationActionPolicy.Allow);
185+
}
186+
}
166187
}
167188
}
168189
}

src/AirshipBindings.iOS.ObjectiveC/ApiDefinitions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,36 @@ interface UAMessageCenterUser
13471347
string BasicAuthString { get; }
13481348
}
13491349

1350+
// @protocol UAMessageCenterNativeBridgeDelegate
1351+
[Protocol (Name = "_TtP17AirshipObjectiveC35UAMessageCenterNativeBridgeDelegate_"), Model]
1352+
[BaseType (typeof(NSObject))]
1353+
interface UAMessageCenterNativeBridgeDelegate
1354+
{
1355+
[Abstract]
1356+
[Export ("close")]
1357+
void Close ();
1358+
}
1359+
1360+
// @interface UAMessageCenterNativeBridge : NSObject
1361+
[BaseType (typeof(NSObject), Name = "_TtC17AirshipObjectiveC27UAMessageCenterNativeBridge")]
1362+
interface UAMessageCenterNativeBridge
1363+
{
1364+
// navigationDelegate is bound as NSObject because Swift's `any WKNavigationDelegate`
1365+
// produces WKNavigationDelegateWrapper in .NET (not an NSObject), so we use
1366+
// WKWebView.WeakNavigationDelegate instead of NavigationDelegate.
1367+
[Export ("navigationDelegate", ArgumentSemantic.Strong)]
1368+
NSObject NavigationDelegate { get; }
1369+
1370+
[NullAllowed, Export ("nativeBridgeDelegate", ArgumentSemantic.Weak)]
1371+
UAMessageCenterNativeBridgeDelegate NativeBridgeDelegate { get; set; }
1372+
1373+
[NullAllowed, Export ("forwardNavigationDelegate", ArgumentSemantic.Strong)]
1374+
WebKit.IWKNavigationDelegate ForwardNavigationDelegate { get; set; }
1375+
1376+
[Export ("setMessage:user:")]
1377+
void SetMessage (UAMessageCenterMessage message, UAMessageCenterUser user);
1378+
}
1379+
13501380
// @interface UAMessageCenterViewControllerFactory : NSObject
13511381
[BaseType (typeof(NSObject), Name = "_TtC17AirshipObjectiveC36UAMessageCenterViewControllerFactory")]
13521382
interface UAMessageCenterViewControllerFactory

0 commit comments

Comments
 (0)