如何将 Objective-C 静态库绑定到 Xamarin.iOS? [英] How to bind an Objective-C static library to Xamarin.iOS?
问题描述
我已阅读 Xamarin 的文档.
这是我在 Objective-C 中的测试类:
#import "XamarinBundleLib.h"@implementation XamarinBundleLib+(NSString *)testBinding{return @"Hello Binding";}@结尾
很简单,只有一种方法.
这是我的 C# 类:
命名空间 ResloveName{[BaseType (typeof (NSObject))]公共部分接口 IXamarinBundleLib {[静态,导出(testBinding")]NSString TestBinding {get;}}}
然后这是我的 AppDelegate 代码:
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions){//在应用程序启动后覆盖自定义点.//如果您的应用程序不需要,您可以安全地删除此方法string testStr = ResloveName.IXamarinBundleLib.TestBinding.ToString();System.Console.WriteLine("testStr="+testStr);返回真;}
当我运行该应用程序时,出现此异常:
TestBinding 属性为空.我一定是哪里出错了,我该如何解决?
去年我写了一篇非常详细的博客文章,内容是关于从 ObjC 代码创建一个适用于 Xamarin.iOS 绑定项目的静态库,你可以找到它
问题可能是您的 libxyz.linkwith.cs
缺少一些信息,如果看起来像这样:
使用ObjCRuntime;[装配:LinkWith(libFoo.a",SmartLink = true,ForceLoad = true)]
它肯定缺少有关您的胖库支持的架构的一些重要信息(缺少第二个参数target
),您可以使用以下命令检索您当前的静态库支持的架构
xcrun -sdk iphoneos lipo -info path/to/your/libFoo.a
你应该得到这样的输出
fat文件中的架构:Foo/libFoo.a是:i386 armv7 x86_64 arm64
所以我们知道这个静态库支持 i386 armv7 x86_64 arm64
,我们应该通过提供第二个参数 target
来提供我们的 LinkWith
属性支持的拱门> 如下:
使用ObjCRuntime;[组装:LinkWith(libFoo.a",LinkTarget.ArmV7 | LinkTarget.Arm64 | LinkTarget.Simulator | LinkTarget.Simulator64,SmartLink = true,ForceLoad = true)]
还要确保 LinkWith
属性的第一个参数与您的静态库文件名(在我的例子中为libFoo.a")相匹配.
我建议再次检查的另一件事是静态库的 Build Action
(在我的例子中为 libFoo.a
)已正确设置为 ObjcBindingNativeLibrary
如下所示:
希望这有帮助!
I have read the documentation from Xamarin.
And this is my test class in Objective-C:
#import "XamarinBundleLib.h"
@implementation XamarinBundleLib
+(NSString *)testBinding{
return @"Hello Binding";
}
@end
It's very easy, just one method.
And this is my C# class:
namespace ResloveName
{
[BaseType (typeof (NSObject))]
public partial interface IXamarinBundleLib {
[Static,Export ("testBinding")]
NSString TestBinding {get;}
}
}
Then this is my AppDelegate code:
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
string testStr = ResloveName.IXamarinBundleLib.TestBinding.ToString ();
System.Console.WriteLine ("testStr="+testStr);
return true;
}
When I run the application, I get this exception:
The TestBinding attributes is null. I must be wrong somewhere, so how can I fix it?
I wrote a very detailed blog post about creating a static library from ObjC code last year that works on Xamarin.iOS binding projects and you can find it here (just in case :wink::wink:).
That being said if you already have a fat static library in your hands and it is already added into your Xamarin.iOS Binding Project as shown here:
The issue could be that your libxyz.linkwith.cs
is missing some information, if it looks like this:
using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", SmartLink = true, ForceLoad = true)]
it is definitely missing some important information about the architectures supported by your fat library (it is missing the second argument target
), you can use the following command to retrieve what architectures your current static library supports
xcrun -sdk iphoneos lipo -info path/to/your/libFoo.a
and you should get something like this as output
Architectures in the fat file: Foo/libFoo.a are: i386 armv7 x86_64 arm64
So we know this static library supports i386 armv7 x86_64 arm64
and we should provide our LinkWith
attribute the supported archs by providing the second argument target
as follows:
using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", LinkTarget.ArmV7 | LinkTarget.Arm64 | LinkTarget.Simulator | LinkTarget.Simulator64, SmartLink = true, ForceLoad = true)]
Also make sure that the first parameter of the LinkWith
attribute matches your static library file name ("libFoo.a" in my case).
The other thing I would suggest double checking is that the Build Action
of your static library (libFoo.a
in my case) is correctly set to ObjcBindingNativeLibrary
as show here:
Hope this helps!
这篇关于如何将 Objective-C 静态库绑定到 Xamarin.iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!