Creating Xamarin.iOS Binding Project for iOS Libraries

Context

I have been working on integrating a third party library build with native languages (objective c / c++) into a Xamarin application and I though I’d share my learnings

Some information about the library

  • I had couple of static libraries .a files.
  • I had two frameworks .framework files
  • I had four bundle folders .bundle folders.

Environment

  • Xamarin Studio on MAC.
  • XCode.
  • XCode Command line tools.

Find out what  architectures you will support

First thing we want to know. Is against which architecture these .a files are build against.

There is a command line tool called “lipo” that you can use to detect the architectures of the .a file like this

lipo -info xx.a

For me this tool was giving incorrect result so I had to use Clang like this

clang -arch armv7  -gmodules xx.a

Then what we need to do is to try and test what architectures we need to support if the architecture is not supported in the .a file you we will  get

“Undefined symbols for architecture arm64”

Create your binding project

I will not go through how to create a brand new binding project you can follow the Walkthrough from Xamarin

This walkthrough is great for straight forward scenarios but in my case I had to do extra steps.

After finishing your project and adding the .a files open the .cs file generated , it should look like this at the beginning.

[assembly: LinkWith ( “RoverLink.a”, SmartLink = true,  ForceLoad = true)]

Then what we need to do is from prev step add the supported architectures

[assembly: LinkWith ( “RoverLink.a”,LinkTarget.ArmV7s | LinkTarget.ArmV7, SmartLink = true,  ForceLoad = true)]

if the library build with c++ then it should be like this

[assembly: LinkWith ( “RoverLink.a”,LinkTarget.ArmV7s | LinkTarget.ArmV7, SmartLink = true, IsCxx = true, ForceLoad = true)]

Now you want to see what frameworks does your library depend on (this is different from the two frameworks file that I have at the beginning ) and then add them like this

[assembly: LinkWith ( “RoverLink.a”,LinkTarget.ArmV7s | LinkTarget.ArmV7, SmartLink = true, IsCxx = true, ForceLoad = true, Frameworks = “CoreGraphics Foundation UIKit SystemConfiguration CoreText QuartzCore MessageUI”)]

if your library depend on a dynamic libraries .dylib like libz.dylib then add them like this

[assembly: LinkWith ( “RoverLink.a”,LinkTarget.ArmV7s | LinkTarget.ArmV7, SmartLink = true, IsCxx = true, ForceLoad = true, Frameworks = “CoreGraphics Foundation UIKit SystemConfiguration CoreText QuartzCore MessageUI”  ,LinkerFlags = “-ObjC -all_load -lstdc++ -lz”)]

So what is it all about.

  • -ObjC is for objective c
  • -isdc++ for C++
  • The library called libz.dylib then add it like that –lz

If you have multiple .a files you need to do the same by adding them to the same project.

Now comes to the frameworks. Inside the framework folder you will find Headers folder and executable file with no extension.What you will need to do is rename this executable and add .a extension and treat it like the previous files.

And for the Header files in the Headers folder we will use Objective Sharpie to generate the Api interface as per the walkthrough.

Using your binding project

As per the walkthrough you need to add a reference to your generated dll. But what I want to talk about is how to use the Bundle folders that came with the SDK.

This was a bit confusing because inside the bundle folder there were some .xib files and I thought I need to treat them differently but turns out not.

So what you need to do is just drag the bundle folder , or Add Existing Folder and select your bundle folder , But make sure you add it to the root of the project.

After that if your SDK is looking for some assets in there bundle folder it should find it.

Thanks

Advertisements

Multiple Windows of Xamarin Studio in Mac

It seams strange but is is true , you can not have more than one Xamarin Studio window or instance by default and you need a hack to get this happening

To do so we need to this 

1 – Open Applications folder in mac

2 – Go to Utilities

3 – Double click on Apple Script Editor

4 – Click on New Document at the lower left corner

5 – Type this in the windows that comes up

do shell script “open -n /Applications/Xamarin\\ Studio.app/”

6 – Save as Application and name it what you want

Image

 

7 – Cosmetic Step is to change the icon of this script to look like Xamarin Studio 

  1. Right Click on the new created script file in Application folder 
  2. Click on Get Info
  3. Then leave it and Right Click on Xamarin Studio and click on “Show Package Content”
  4. Then go to Content \Resources 
  5. You should find Xamarin Icon 
  6. Drag and drop the Xamarin icon into the info of the Script that we created (Make sure you drag it into the icon as it shows in the photo)

Image

After that you can Lunch more than one Xamarin Studio by running this script that acts like a Application from here 

Image

My Dev Environment

Working with different developers in different project and organisations made me able to see different development tools (Productivity tools) I’m a DotNet Developer so I’m not talking about Visual Studio but talking about other tools surrounding  this main dev tool

Source Control

I use Git for about 3 years now if you don’t you need to consider doing NOW

1 – I use on Windows Git Extensions also you can check GUIS for other Git UI clients on different platforms

2 – While installing git make sure in the installation you choose the option to run git commands from the Windows Command Line because Bash (Unix command line) is very slow on windows

3 – In your profile in Windows you should have some shortcuts that help you speed up your dev process you can find mine here https://github.com/waelemara/WindowsProfile

Visual Studio

Obviously Visual Studio is the main and the most important tool we use as .net developers but there are alot of things you can do to it to make it much better

1 – Resharper this is the most beautiful thing that happened to Visual Studio it adds a lot of goodness to VS it adds 85 shortcuts , refactoring snippets and it have a very good integration with LINQ it is a MUST have tool

2 – Web Essentials it is an Addin that gives you many Web features like tow views to LESS file editing and so on

That it for now i will keep this post updated as i add to my dev environment

 

Enable Ctrl Arrow in Parallels and Mavericks

one of the issues I find when using Parallels on my Mac (and I’m a developer ) is the Ctrl Arrow combinations with Shift and this is not enabled by default because on Mac Ctrl Arrow switch between Full Screen Apps , but there is a solution for this ,

image

1 – Go to Parallels –> Preferences –> Shortcuts Tab –> System Shortcuts

change the “Send system shortcuts” to Always

image

2 – System Preferences –> Security & Privacy –> Accessibility

you should find Parallels in the list on the right hand side check Parallels desktop

note : if you find every thing is grayed out click on the lock at the bottom of the window

 

Ninject Auto Register

Auto Register Classes and interfaces for Ninject IoC not by name but by inheritance

So usually when you use Ninject you have to register or bind you classes and interfaces like this

IKernel kernel = new StandardKernel();

kernel.Bind().To();

and this is good but tedious , what if you have big library and you don’t want to bother your self with remembering extra step when you add a new service

and also what if you are introducing Ninject into a project.

so why i think this library is good one thing , it just auto register (Bind)

also it does not depend on a naming convention , it looks for the inheritance and do the binding

Ex:

using (var standardKernel = new Ninject.StandardKernel()) {

NinjectAuto.Register(Contracts.AssemblyInfo.GetExecutingAssembly(), MockServices.AssemblyInfo.GetExecutingAssembly(), standardKernel);

var service = standardKernel.Get();

string writeSomething = service.WriteSomething();

Assert.AreEqual(writeSomething, “Mock Service”);

}

that it , just give where i should find the contracts , where are the implementation and let it do the rest

You can find the source code here

https://github.com/waelemara/NinjectAutoRegister

Also it is available as NuGet Package

https://www.nuget.org/packages/NinjectAutoRegister/

Async Controllers ( Thank you .net 4.5 )

Async controllers are basically normal controllers but for long running processes that can take time to complete ( Web Services , IO operations , Report Generation , etc )

so instead of using normal controllers that block the execution and may block the web server we usually use Async controllers

So how is that is done before and what is new in Asp.Net MVC 4.5

Old Way 

https://github.com/waelemara/async-controllers/tree/master/Before

New Way

https://github.com/waelemara/async-controllers/tree/master/New

I will focus on the new way here , in .net 4.5 we can use the ( async , await ) new keywords to tell the compiler , well we have an async method being called , this applies to actions so we can mark the action to be and async and instead of breaking the logic into two methods ( that looks ugly and breaks the logical sequence of the code ) to the normal action method but we need to tell the compiler this is an async controller

so the normal code looks like this

public ActionResult Index()
        {
            var faceBookCount =  GetFacebookCommentCount(); // takes some time
            var commentCount = GetFacebookLikeCountAsync(); // takes some time
            var faceBookStatus = new FaceBookStatus()
                {
                    FaceBookCount = faceBookCount,
                    CommentCount = commentCount
                };
            return View(faceBookStatus);
        }
to be :
public async Task<ActionResult> Index()
        {
            var faceBookCount = await GetFacebookCommentCountAsync();
            var commentCount = await GetFacebookLikeCountAsync();
            var faceBookStatus = new FaceBookStatus()
                {
                    FaceBookCount = faceBookCount,
                    CommentCount = commentCount
                };
            return View(faceBookStatus);
        }
so this makes the application pool release the thread that is handling your request and then assign another one to handle the request after the function marked with await finished work so the web server does not get blocked

self invoking anonymous function expression

So usually in writing JavaScrip in any website , things get missy and memory consumption gets nasty and a lot of time spent on profiling and debugging why the JavaScript gets so slow one of the reasons i found making writing JavaScript is that any thing can work , just place a script tag and write whatever you want and it will work
So i want to give you an example of writing a better JavaScript that can make your JavaScript run with better performance and this is called “Self Infoking anonymous function excepression” or in other words encapsulating your code which is written in various JavaScript files , so every one off them will be in it’s own temporarily scop and will be destructed once the execution finishes
BAD
<script type=”text/javascript”>
var showAlert = function(){
alert(x);
};
</script>

What this code means that your showAlert function is exposed to the global namespace level which in meny cases does not have logical meaning unless you are having this as a utility function and want to expose it
GOOD
<script type=”text/javascript”>
(function(){
var showAlert = function(){
alert(x);
})();
</script>

Now you have this anonymous function that will be self invoked and it will have it’s own scoop so your showAlert function wont be exposed to the Global namespace

HAR (HTTP Archive)

Har file is a JSON-formatted file that contain a trace of a Web browsers interaction with a given site. (Wikipedia)

so basically you can export all the interations between the browser and server (Css , javascript , images and most importantly Ajax request ) into a file that can be imported later

so why should i care about this , usually this is used when you want to send some meta data to developer when testing on a staging environment or production environment

Lets go into practice ..

first how to export this in chrome you can press F12 or right click and then “Inspect element” and then in the network tab right click as shown in the bellow image 

Image

and then you can save this in a .har file on your disk 

then we can import this in two ways

  • Online

http://www.softwareishard.com/blog/har-viewer/

http://ericduran.github.io/chromeHAR/

  • Desktop

Fiddler2