Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Binding Does Nothing #14775

Closed
Tazzi3 opened this issue Apr 26, 2023 · 20 comments
Closed

iOS Binding Does Nothing #14775

Tazzi3 opened this issue Apr 26, 2023 · 20 comments
Labels
area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging partner/macios Issues for the Mac / iOS SDK platform/iOS 🍎 s/needs-attention Issue has more information and needs another look s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Milestone

Comments

@Tazzi3
Copy link

Tazzi3 commented Apr 26, 2023

Description

I can successfully get an iOS Objective-C library (.a file) to bind following the Xamarin binding tutorial found here: https://learn.microsoft.com/en-us/xamarin/cross-platform/macios/binding/objective-c-libraries.
I also used the library that the tutorial recommended to follow which is located here: https://github.com/xamarin/ios-samples/tree/master/BindingSample

Now I am trying to do the same for MAUI. When selecting a MAUI iOS binding library, adding the ios static library, and copying in the same code, it says it compiled but it has not actually done anything. The dll produced has no functions present when decompiling and checking it. Its almost like VS does not recognize that it is a binding library setup and basically does nothing.

Things I have tried for a fix:

  1. Creating a new MAUI ios binding template using the dotnet commands in powershell
  2. Downloading a MAUI ios binding example and trying to compile that directly such as: https://github.com/drasticactions/WebP.Touch/tree/net6-port/WebP.Touch
  3. Changing from a .netcore 7 to .netcore6 project which fails too.

Am I missing something critical for MAUI template to work in comparison to how XAMARIN does it?

Steps to Reproduce

  1. Create new MAUI iOS Binding Library in Visual Studio 22
  2. Load in a native iOS library (.a) and add any exports to the code
  3. Compile and then decompile DLL to see now functions available

Link to public reproduction project repository

https://github.com/Tazzi3/MAUI-ObjC-Binding

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

all iOS

Did you find any workaround?

No work around.

Relevant log output

No response

@Tazzi3 Tazzi3 added the t/bug Something isn't working label Apr 26, 2023
@drasticactions
Copy link
Contributor

A few things:

  1. I think this issue may be better suited for https://github.com/xamarin/xamarin-macios, as it has to do with the Mac/iOS SDK and not MAUI (The UI Framework), which is what this repo is for.
  2. Pulling down your code, the errors being returned are correct:
  • It's complaining that it can't find things like NSString and Export, because you need to include the namespaces for them.
using System;

using UIKit;
using Foundation;
using ObjCRuntime;
  • After adding that, it's complaining you don't have XMGreeting, which is not included in your binding. You would need to include that.
  • There's also an error with the [Model(AutoGeneratedName = true)] for XMCustomViewDelegate, remove AutoGeneratedName and it should work.,
// @protocol XMCustomViewDelegate<NSObject>
    [Protocol]
    [Model]
    [BaseType(typeof(NSObject))]
    interface XMCustomViewDelegate

I pulled down my repo that you listed, built it with no errors and it worked fine when I referenced the output in another project.

The only other thing I can think of is that you can't directly reference binding projects with other projects. If you're trying to, for example, reference a binding project with your MAUI app, that won't work. You would need to reference the output DLLs of the binding (or create a Nuget package and reference that) and then it should work.

@rolfbjarne Am I correct? Does this sound right?

@rolfbjarne
Copy link
Member

The only other thing I can think of is that you can't directly reference binding projects with other projects. If you're trying to, for example, reference a binding project with your MAUI app, that won't work. You would need to reference the output DLLs of the binding (or create a Nuget package and reference that) and then it should work.

You can reference a binding project from another project, it'll kind of work. The IDE will tell you that there's a problem (red squiggly lines when trying to use anything from the binding project), but if you try to build, it'll build just fine (this is a known issue). That said, just referencing the output dll makes the IDE happy as well, so most people do that.

Otherwise everything else is correct.

@Tazzi3
Copy link
Author

Tazzi3 commented Apr 26, 2023

Thanks for the responses guys!

@drasticactions I do not get any errors at all when I compile. I added those namespaces and still get no errors, infact, I can absolutely trash the entire ApiDefinition file with random errors and changes and it still claims to compile fine. What version of VS are you using and the dot net version installed? (I have listed mine at the bottom of this comment).

Also thankyou for pointing out the XMGreeting enum, that is suppose to be in the StructsAndEnums class. But that is also a perfect example of the project compiling without any problems when clearly it should be failing to compile.

Do note that if I create a Xamarin Binding project, it will compile and I can then reference the xamarin DLL in a xamarin project.
But this does not work for MAUI binding project, I get the previously mentioned issues.

To verify this is not a problem on just my computer. I have just done a fresh install of Visual Studio 2022 community on a laptop, and I get the exact same behaviour.

The installed version I am running on both computers is:

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.5.4

Installed Workload Id      Manifest Version       Installation Source
---------------------------------------------------------------------
maui-windows               7.0.81/7.0.100         VS 17.5.33530.505
maui-maccatalyst           7.0.81/7.0.100         VS 17.5.33530.505
maccatalyst                16.2.2035/7.0.100      VS 17.5.33530.505
maui-ios                   7.0.81/7.0.100         VS 17.5.33530.505
ios                        16.2.2035/7.0.100      VS 17.5.33530.505
maui-android               7.0.81/7.0.100         VS 17.5.33530.505
android                    33.0.46/7.0.100        VS 17.5.33530.505

@rachelkang rachelkang added platform/iOS 🍎 partner/macios Issues for the Mac / iOS SDK labels Apr 26, 2023
@rachelkang rachelkang added this to the Backlog milestone Apr 26, 2023
@ghost
Copy link

ghost commented Apr 26, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@rachelkang rachelkang added the s/needs-attention Issue has more information and needs another look label Apr 26, 2023
@drasticactions drasticactions added the s/move-to-vs-feedback Tells automation to ask the person to use VS Feedback to report the issue label Apr 27, 2023
@ghost
Copy link

ghost commented Apr 27, 2023

Thanks for the issue report @Tazzi3! This issue appears to be a problem with Visual Studio, so we ask that you use the VS feedback tool to report the issue. That way it will get to the routed to the team that owns this experience in VS.

If you encounter a problem with Visual Studio, we want to know about it so that we can diagnose and fix it. By using the Report a Problem tool, you can collect detailed information about the problem, and send it to Microsoft with just a few button clicks.

  1. Go to the Visual Studio for Windows feedback tool or Visual Studio for Mac feedback tool to report the issue
  2. Close this bug, and consider adding a link to the VS Feedback issue so that others can follow its activity there.

@drasticactions
Copy link
Contributor

drasticactions commented Apr 27, 2023

I'm doing this on my Mac, I haven't tried to build and reference a Binding project on Windows.

In any case, this issue wouldn't be one for this Repo: It's unrelated to the MAUI UI Framework. This sounds like a tooling issue on Windows, so I would file an issue there and see.

@rolfbjarne
Copy link
Member

Ah, I didn't notice that this happens on Windows.

At the moment, building a binding project on Windows requires having a connection to a remote Mac (see also xamarin/xamarin-macios#16244).

@Tazzi3
Copy link
Author

Tazzi3 commented Apr 28, 2023

I will try again on the MAC and report back!

@Tazzi3
Copy link
Author

Tazzi3 commented May 1, 2023

@drasticactions @rolfbjarne We have had success with compiling in MAC Visual Studio. I can then confirm that the desired functions are displaying in the DLL when decompiling it.

When I copy the DLL over to Windows Visual Studio and attempt to use it in a MAUI application, I get the following error:
`Error CS0012 The type 'NSObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.iOS, Version=16.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065'

When looking at the Microsoft.iOS version in my MAC visual studio, it shows it is using Microsoft.iOS v16.2.46
Whereas the version in my Windows visual studio shows it is using v16.2.34

Is it possible to manually update the version? Or can I copy the DLL from the windows version onto the MAC to make them the same?
`

@rolfbjarne
Copy link
Member

When I copy the DLL over to Windows Visual Studio and attempt to use it in a MAUI application, I get the following error:
`Error CS0012 The type 'NSObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.iOS, Version=16.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065'

Can you get a binary build log (binlog) as described here: https://github.com/xamarin/xamarin-macios/wiki/Diagnosis#binary-build-logs?

@Tazzi3
Copy link
Author

Tazzi3 commented May 4, 2023

Can you get a binary build log (binlog) as described here: https://github.com/xamarin/xamarin-macios/wiki/Diagnosis#binary-build-logs?

Yes I can get that today. But I am getting the same issue as this person: xamarin/xamarin-macios#17418

The Microsoft.iOS SDK used between MAC and Windows Visual Studio is different. I have tried completely uninstalling and reinstalling Visual Studio 22 on both machines at the same time and they still use two different versions.

I can see there are newer versions of the SDK which can be seen on the nuget link: https://www.nuget.org/packages/Microsoft.iOS.Windows.Sdk/

But trying to install these results in the same error that the previous person got which was:
The package Microsoft.iOS.Sdk 16.2.46 has a package type DotnetPlatform that is incompatible with this project.

I did try a variety of different versions as well, but the v16.2.46 is the version that matches the MAC VS install.

@Eilon Eilon added the area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging label Jun 8, 2023
@rolfbjarne
Copy link
Member

Marking as need info: #14775 (comment)

@rolfbjarne rolfbjarne added s/needs-info Issue needs more info from the author and removed s/needs-attention Issue has more information and needs another look labels Oct 20, 2023
@ghost
Copy link

ghost commented Oct 20, 2023

Hi @Tazzi3. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@rolfbjarne rolfbjarne removed the s/move-to-vs-feedback Tells automation to ask the person to use VS Feedback to report the issue label Oct 20, 2023
@ghost ghost added the s/no-recent-activity Issue has had no recent activity label Oct 24, 2023
@ghost
Copy link

ghost commented Oct 24, 2023

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

@dynatom
Copy link

dynatom commented Oct 27, 2023

Same issue, iOS binding api definition not working.

Android binding just copy jar frome Xamarin Android binding will be success, AndroidBinding project compile success, and add it to Android maui project (<ProjectReference Include="..\Android_BindingLibrary\Android_BindingLibrary.csproj" />), it can use api in jar file.

In iOS binding, copy all .a files and ApiDefinition.cs/StructsAndEnums.cs files, the iOSBinding project can compile success, but add it to iOS maui project (<ProjectReference Include="..\iOS_BindingLibrary\iOS_BindingLibrary.csproj" />), it can't find any api which was defined in ApiDefinition.cs.

Add .a/.framework to iOSBinding project is like the following:

  <ItemGroup>
    <NativeReference Include="libTestiOS.a">
      <Kind>Static</Kind>
      <ForceLoad>True</ForceLoad>
    </NativeReference>
    <NativeReference Include="TestiOS2.framework">
      <Kind>Framework</Kind>
      <Frameworks></Frameworks>
      <LinkerFlags></LinkerFlags>
      <WeakFrameworks></WeakFrameworks>
      <ForceLoad>True</ForceLoad>
      <NeedsGccExceptionHandling>False</NeedsGccExceptionHandling>
      <IsCxx>False</IsCxx>
      <SmartLink>False</SmartLink>
    </NativeReference>
  </ItemGroup>

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author s/no-recent-activity Issue has had no recent activity labels Oct 27, 2023
@rolfbjarne
Copy link
Member

In iOS binding, copy all .a files and ApiDefinition.cs/StructsAndEnums.cs files, the iOSBinding project can compile success, but add it to iOS maui project (<ProjectReference Include="..\iOS_BindingLibrary\iOS_BindingLibrary.csproj" />), it can't find any api which was defined in ApiDefinition.cs.

There's a known issue where intellisense in the IDE won't work, but the build will actually succeed. Can you try building the project and see if that works? One workaround is to reference the binding assembly directly instead of the binding project, in which case intellisense will work as expected.

@dynatom
Copy link

dynatom commented Oct 31, 2023

In iOS binding, copy all .a files and ApiDefinition.cs/StructsAndEnums.cs files, the iOSBinding project can compile success, but add it to iOS maui project (<ProjectReference Include="..\iOS_BindingLibrary\iOS_BindingLibrary.csproj" />), it can't find any api which was defined in ApiDefinition.cs.

There's a known issue where intellisense in the IDE won't work, but the build will actually succeed. Can you try building the project and see if that works? One workaround is to reference the binding assembly directly instead of the binding project, in which case intellisense will work as expected.

Thanks, it build success and work correctly with wrong intellisense.
Binding project: ApiDefinition.cs
MAUI project: Platforms\iOS\Program.cs
But 【reference the dll directly】 still has wrong intellisense.

The test lib files and define are these:
TestiOS.h

#ifndef TestiOS_h
#define TestiOS_h

#import <Foundation/Foundation.h>

@interface TestiOS : NSObject
- (int) add:(int)a b:(int)b;
@end
#endif /* TestiOS_h */

TestiOS.m

#import "TestiOS.h"

@implementation TestiOS

- (int) add:(int)a b:(int)b {
    return a+b;
}

@end

ApiDefinition.cs <-- Create from sharpie

using Foundation;

namespace BindingiOS
{
    // @interface TestiOS : NSObject
    [BaseType(typeof(NSObject))]
    interface TestiOS
    {
        // -(int)add:(int)a b:(int)b;
        [Export("add:b:")]
        int Add(int a, int b);
    }
}

Test code in Program.cs

	BindingiOS.TestiOS testobj = new BindingiOS.TestiOS();
	int a = 15;
	int b = 57;
	int c = testobj.Add(a, b);

@rolfbjarne
Copy link
Member

But 【reference the dll directly】 still has wrong intellisense.

Can you show your csproj file?

@Zhanglirong-Winnie Zhanglirong-Winnie added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Jan 19, 2024
@ghost
Copy link

ghost commented Jan 19, 2024

Hi @Tazzi3. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@Zhanglirong-Winnie
Copy link
Collaborator

Verified this issue with Visual Studio Enterprise 17.9.0 Preview 3 and VSMac 17.6.8.400. Builds without errors using user-provided projects. And when I try to reference a binding project from another project, it still works fine.

@ghost ghost closed this as completed Jan 26, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 26, 2024
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging partner/macios Issues for the Mac / iOS SDK platform/iOS 🍎 s/needs-attention Issue has more information and needs another look s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants