5

I'm trying to link a native iOS framework that uses AFNetworking internally, I'm building it initially as a prototype to move some of my company's ObjC IP over to be linkable with Xamarin as well in the future.

I started by making a simple class that loads kittens using AFNetworking into an UIImageView

Header file:

#import <UIKit/UIKit.h>

@interface FWSImageRepo : NSObject
+(UIImageView *)imageViewFromURLString:(NSString *)urlString;
@end

Implementation

@implementation FWSImageRepo

+(UIImageView *)imageViewFromURLString:(NSString *)urlString{

    UIImageView *imageView = [[UIImageView alloc] init];

    NSURL *imageUrl= [NSURL URLWithString:@"http://www.pets4homes.co.uk/images/articles/1334/large/6-health-issues-to-watch-for-in-young-kittens-52f62cff5cabb.jpg"];

    if(imageUrl != nil) {
        [imageView setImageWithURL:imageUrl];
    }

    return imageView;
}

@end

I created a framework compiler target and I've copied the framework over to the Xamarin iOS project. I used Objective Sharpie to generate the bindings below

// @interface FWSImageRepo : NSObject
[BaseType(typeof(NSObject))]
interface FWSImageRepo
{
    // +(UIImageView *)imageViewFromURLString:(NSString *)urlString;
    [Static]
    [Export("imageViewFromURLString:")]
    UIImageView ImageViewFromURLString(string urlString);
}

All of these are linked as instructed in a binding project, the framework is linked as a native reference and the bindings project is referenced by a single page Xamarin iOS app.

now a mere initialization of the class var fwrepo = new FWSImageRepo(); indicates to me that the framework isn't liked to the project at all. the error doesn't change even if I don't link the frameworks in the bindings project

Could not create an native instance of the type 'FWSImageRepo': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

Any reason to check what's going on? I've tried everything and exhausted my search online. theres no example of this requirement anywhere as far as I've searched and barely any direction with this. I've tried the solutions from the others facing the same issues. how do I look for where I've gone wrong?

Here's a link for the projects I've created https://drive.google.com/open?id=0B2f9RlRxZKoZcElIRkpLZnF6WVU

Jay Kannan
  • 1,372
  • 3
  • 14
  • 30

2 Answers2

3

This doesn't exactly solve the problem, but thanks to @chris making me realize that it's an issue with not loading the class, the solution was actually at modifying the iOS build settings to match the following choices

I'm still only able to access the method statically, as below:

// @interface FWSImageRepo : NSObject
[BaseType(typeof(NSObject))]
public interface FWSImageRepo
{
    // +(UIImageView *)imageViewFromURLString:(NSString *)urlString;
    [Static]
    [Export("imageViewFromURLString:")]
    UIImageView ImageViewFromURLString(string urlString);
}

and access it like this

var imageview = FWSImageRepo.ImageViewFromURLString("");

any idea why I can't use it as an instance (I removed the [Static] attribute?

Jay Kannan
  • 1,372
  • 3
  • 14
  • 30
  • To clarity the static part. A plus sign in front of a method in Objective-C means it's a class method (a.k.a. static). That's why you are only able to bind it as a static method. Objective-C methods with a minus sign in front of it are instance methods. – Leon Lucardie Apr 26 '21 at 09:26
1

Could not create an native instance of the type 'FWSImageRepo': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

This suggests the native library in question has not been loaded. I would walk through the documentation:

https://developer.xamarin.com/guides/ios/advanced_topics/native_interop/

to determine where you project settings aren't loading in the native library.

Chris Hamons
  • 1,510
  • 11
  • 22