Xamarin iOS Swift library .a error on Visual studio mac - ios

I have found some issue while run a iOS App on Xamarin iOS. Kindly help me to succeed.
I have tried to create a Xamarin iOS App. I have .a library. It contains NSObject class and single function.
import Foundation
#objc(CustomSwift)
class CustomSwift: NSObject {
func getValue() -> String {
return "Hi, This is a Custom swift.a"
}
}
And I created a fat library by using
lipo -create -output
I have imported .a library on Xamarin iOS Binding library project.
ApiDefinition.cs
using System;
using ObjCRuntime;
using Foundation;
using UIKit;
namespace CustomSwiftLibrary
{
[BaseType(typeof(NSObject))]
interface CustomSwift
{
[Export ("getValue")]
string Value { get; }
}
}
And I call my .a library class on ViewController.cs
CustomSwift customObject = new CustomSwift();
System.Diagnostics.Debug.Print(customObject.Value);

Related

Swift: import framework but can't find class in it

I import a swift framework to a swift project, but when I call the class in this framework, Xcode throw a compile error "Use of undeclared type 'xxxx(class name)' ".
I feel Xcode have found the framework, otherwise it will complaint "can't find xxx(framework name)".
But why Xcode can't find the class of this framework.
I have tried remove and re-add framework, and delete DeivedData files, but all of them not work. I haven't use CocoaPods to import framework.
Any idea?
In a Framework that was built for Release (and not Debug), the class symbols need to be accessible.
Make sure you are you trying to access Public or Open classes from your Swift framework.
// set the Framework class to Public
public class rnHello{
// set the initializer to public, otherwise you cannot invoke class
public init() {
}
// set the function to public, as it defaults to internal
public static func world() {
print("hello from a static method")
}
}
Now you can access this via your Swift code or if you attach lldb using:
lldb) po rnHello.world()
Make sure that the FrameWorkSearch path in the BuildSettings of the project is reflecting the correct path to your framework.

How to import Objective-C in Swift framework

i create a swift framework, and create a objectivC file in the framework project.
swiftClass.swift:
Public swiftClass{
func test(){
var c = objectivCClass();
}
}
objectivCClass.h:
#Interface objectivCClass{
}
#end
I know make objectivCClass.h to public header in Build Phases , and import it in swiftFramework.h , the framework can works ok.
But I do not make objectivCClass public in Build Phases and do not want user knows the class. If I make objectivCClass.h to project/private header , An error happen.
Who know how to handle it?

Unresolved public classes in a Cocoa Touch Framework

I'm trying to build a framework to export a few classes as an importable "package of sort".
My Framework project is called "MyFramework"... is was created as a Cocoa Touch Framework project in Swift.
I've added a single swift file called ImplementationTest.swift
It contains the following :
public class ImplementationTest {
public init(){
print("Init called");
}
public func echo(request: String){
print ("Echo called with "+request);
}
}
The framework builds fine and I get a MyFramework.framework folder.
I add that framework to a new swift application project and added it as Linked Framework and Library as well as Embedded Binaries.
I can "import MyFramework" just fine.
But if I try "var test=ImplementationTest()", I get the following error :
use of unresolved identifier "ImplementationTest"
If I try "var test=MyFramework.ImplementationTest()" I get :
module 'MyFramework' has no member name "ImplementationTest"
Am I doing this wrong ? Can my framework only have one single exported class ? and should it be named the same name as the framework itself ?
Check after you build the framework if you can access it via in, show finder.
If not, then try building it using generic ios device.

passing values from unity 3D to objective c file

My app has two windows. First one is unity 3D exported to ios which uses vuforia kit for doing augmnted reality. Second is native xcode files. I need to pass a string value from unity to native objective c file because I need to display some values based on that. I'm using xcode 7 and unity 5. Please help me understand how to do it. Thanks in advance.
This can be done with the native plugin support in Unity. You need to import the native method into a class and call it via another method. You can find more documentation on it here: http://docs.unity3d.com/Manual/NativePlugins.html
Here is a really simple code example that should fit your needs.
The C# class:
using System.Runtime.InteropServices;
public static class SomeScript
{
[DllImport("__Internal")]
private static extern void _NativeMethodName(string stringValue);
public static void PassStringValue(string stringValue)
{
_NativeMethodName(stringValue);
}
}
And the native plugin code:
extern "C" {
void _NativeMethodName(const char* stringValue)
{
NSLog(#"Passed string value: %s", stringValue);
}
}

Write Unity IOS plugin in Swift code

Is it possible to write unity IOS plugin in Swift?
I already have a working swift framework and want to use it as a plugin in Unity
I saw some places which say it can only be done on Objective-c but is there a workaround for swift?
How to call Unity methods
Unity interface functions are defined in UnityInterface.h in Xcode project built by Unity. This header file is imported in UnitySwift-Bridging-Header.h, so you can call the functions directly in your Swift codes.
To call Unity methods, use UnitySendMessage function like below:
// Example.swift
import Foundation
class Example : NSObject {
static func callUnityMethod(_ message: String) {
// Call a method on a specified GameObject.
UnitySendMessage("CallbackTarget", "OnCallFromSwift", message)
}
}
How to access Swift classes from Unity
Step 1: Create your Swift classes.
// Example.swift
import Foundation
class Example : NSObject {
static func swiftMethod(_ message: String) {
print("\(#function) is called with message: \(message)")
}
}
Step 2: Include "unityswift-Swift.h" and define C functions to wrap Swift classes in .mm file (Objective-C++).
// Example.mm
#import <Foundation/Foundation.h>
#import "unityswift-Swift.h" // Required
// This header file is generated automatically when Xcode build runs.
extern "C" {
void _ex_callSwiftMethod(const char *message) {
// You can access Swift classes directly here.
[Example swiftMethod:[NSString stringWithUTF8String:message]];
}
}
Step 3: Create interface class to call exported C functions from C#.
// Example.cs
using System.Runtime.InteropServices;
public class Example {
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void _ex_callSwiftMethod(string message);
#endif
// Use this method to call Example.swiftMethod() in Example.swift
// from other C# classes.
public static void CallSwiftMethod(string message) {
#if UNITY_IOS && !UNITY_EDITOR
_ex_callSwiftMethod(message);
#endif
}
}
Step 4: Call the method from your C# code.
Example.CallSwiftMethod("Hello, Swift!");
The file names of UnitySwift-Bridging-Header.h and unityswift-Swift.h are defined in "Objective-C Bridging Header" entry and "Objective-C Generated Interface Header Name" entry in Build Settings. These settings and other settings about Swift compiler are set automatically by PostProcesser when the Unity build runs.
Requirements
iOS 7 or later
Compatibility
Unity 5.3.5f1 Xcode 7.3.1
As top-level Swift is simply not accessible from Unity, the "workaround" for Swift is to write an Objective-C wrapper class around it, and access that.
Depending on the amount and complexity of your Swift code that might still be the most optimal approach.

Resources