.net maui and swift

Joe Arnon 20 Reputation points
2024-09-04T18:39:19.09+00:00

hi guys,

I'm trying to build a .NET Maui class library which exposes some interface that can be used by a macOS swift application.  I'm using .NET Maui 8.0 and Xcode 15.4

can someone guide me to how it can be done?

here is the example of the interface I want to expose:

namespace BridgesServices

{

public delegate void doAction(string action);

public interface IBridgesServices

{

    bool Init(doAction doAction);

    void SendRequest(string action);

}

}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,411 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,161 Reputation points
    2024-09-06T16:53:07.7+00:00

    you can build a native library (iOS/mac support is experimental). you need to define the all the entry points as static [UnmanagedCallersOnly] methods . this limits the type of parameters, for example strings are not supported.

    https://video2.skills-academy.com/en-us/dotnet/core/deploying/native-aot/interop

    for c# to call the native library, you will need to use [DllImport] for the entry points. you could build an additional .net dll to expose c# callable methods.

    for swift to call the library, you would need to create a c/c++ header file.

    https://developer.apple.com/documentation/swift/using-imported-c-functions-in-swift

    you can also create a swift package to wrap the library.

    https://www.swift.org/documentation/articles/wrapping-c-cpp-library-in-swift.html

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 30,666 Reputation points Microsoft Vendor
    2024-09-06T02:05:48.95+00:00

    Hello,

    I need a dual communication between swift native app and a .net Maui class library using this interface. While the native swift app initiates the communication.

    You cannot use the MAUI class library in a swift project, it's not supported. The iOS library can be used in Maui, but not vice versa.

    See What is .NET MAUI? - .NET MAUI | Microsoft Learn

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.