Communicating with your iOS app over USB (C# and/or Xamarin)




Communicating with your iOS app over USB (C# and/or Xamarin)

So, you're writing an iOS app that needs to communicate with a desktop client. A quick google search would suggest that the only way to do this would be to create a small socket server on the desktop client that listens on a given port, and have the iOS app connect to the desktop client through your local network. This definitely works, and often times it's good enough. If you're familiar with socket programming, this is definitely straight forward. However, there are definitely some drawbacks. The app has to be on the same network as the client (which isn't always possible in enterprise environments where your computer is on some private wired network). The client opens a port to your computer that anyone on the network can access, which can be a security concern. Initial setup is also a bit more tedious as the app user must enter the IP Address and Port into the app in order to connect. Being able to achieve "iOS App to Desktop" communication via USB cable would solve all of these problems, and make (in my opinion) the app much more convenient. 

The problem with this approach is just how little documentation there is regarding how this can be achieved. Some forum posts say it's flat out not possible. Others will say that you need to sign up for Apple's MFi program (a long and tedious process). Well, after spending the last week scouring the internet, I'm here to tell you that it definitely is possible without MFi. And it's not even all that hard once you understand what's happening. So, here's everything I've learned.

A couple quick notes

I'm primarily a C#/.NET developer, and so I'm writing this assuming you are too. The concepts are pretty universal, but the examples will be in C#. I'm using Xamarin.iOS to build the iOS app, so even the iOS code will be in C#.

Also, I think it's good to note that for developers writing Swift/Objective-C code, there is a pretty awesome platform tool called Peertalk that does everything I'm going to talk about. Unfortunately, there's no Xamarin binding. I almost decided to switch off Xamarin to use this before I finally figured out how to do what Peertalk does very quickly and easily. 

How does Apple do it?

When you plug in your iOS device to your computer, you're able to sync your data via the USB cable. The way Apple achieves this is via a very handy program called usbmuxd. This is a program that's running on your computer, waiting for an iOS device to connect. If you've installed iTunes on your machine, you've also installed this program. On Windows, the service that hosts this program is named "Apple Mobile Device Service", and can be seen in services.msc. So, what does usbmuxd do?

usbmuxd

usbmuxd stands for USB Multiplexing Daemon. If you're anything like me, "USB" is the only really familiar term in that name. But what this program does is actually pretty simple (and extremely useful to us). It's a background process (daemon) that can tell your iOS device to map (multiplex) all the data received from the USB cable to a network port on the iOS device, and return a socket connection to that port. 

So, practically, what does this mean for us? This means that, if you do something like this on the iOS device
        
                   var socket = new Socket(AddressFamily.InterNetwork,
                       SocketType.Stream,
                       ProtocolType.Tcp);

                   socket.Bind(new IPEndPoint(IPAddress.Loopback, 5050));
                   socket.Listen(100);
                   socket.BeginAccept((ar) =>
                   {
                       var connectionAttempt = (Socket) ar.AsyncState;
                       var connectedSocket = connectionAttempt.EndAccept(ar);
                       // Do something with "connectedSocket"
                   }, socket);

The above code (which is running on your iOS device) is now listening for an incoming connection on port 5050 (which I chose completely arbitrarily). I know what you may be thinking. "I thought we weren't going to be using network connections. What gives!?"

This is where usbmuxd does its magic. From your desktop app, you can send a message to usbmuxd that informs the connected iOS device to receive all USB data on a network port. In our case, we'd want to have usbmuxd tell our iOS device to receive USB data on port 5050. usbmuxd will oblige, and give you a socket handle to send data on and receive data from. At this point, your iOS app will call the "BeginAccept" callback, and you'll have a network socket that's not really a "network" socket. This will work even if your computer and iOS device are on different networks, or no networks at all. All data is going through your USB cable!

So...how do I actually communicate with usbmuxd

Fortunately, there's a nuget package for that! The iMobileDevice.Net nuget page can be found here. It's not exactly the most well documented tool, but it works very well. It does assume that the usbmuxd service is running on your machine. Easiest way to do that is to just make sure you have iTunes installed. If you don't want to have a dependency on iTunes, you can do what the people at Duet Display did, and extract the appropriate msi from the iTunes installer, and just require that as a prerequisite of your application


The AppleApplicationSupport64.msi AppleMobileDeviceSupport6464.msi installer will install the service you need.

Once you know that usbmuxd is actually on your machine, you can start using the nuget package. This package is really just a wrapper around a C library, and it shows. There are a lot of IntPtr's and out parameters, so it's not very pretty. But it works.

Here's a snippet from a project I'm working on. The below code listens for an iOS device event (which usbmuxd calls for us). When the event is a "DeviceAdd" event, I attempt to connect to it. 

        
        public void BeginListening()
        {
            _iDeviceApi.idevice_event_subscribe(_eventCallback, new IntPtr());
        }

        private iDeviceEventCallBack EventCallback()
        {
            return (ref iDeviceEvent devEvent, IntPtr data) =>
            {
                switch (devEvent.@event)
                {
                    case iDeviceEventType.DeviceAdd:
                        Connect(devEvent.udidString);
                        break;
                    case iDeviceEventType.DeviceRemove:
                        break;
                    default:
                        return;
                }
            };
        }



The Connect method gets a "DeviceHandle" reference, using the udid of the connected device. Once it gets that handle, it calls the "ReceiveDataFromDevice" method.

        private void Connect(string newUdid)
        {
            _iDeviceApi.idevice_new(out iDeviceHandle deviceHandle, newUdid).ThrowOnError();
            var error =_iDeviceApi.idevice_connect(deviceHandle, 5050, out iDeviceConnectionHandle connection);
            if (error != iDeviceError.Success) return;
            ReceiveDataFromDevice(connection);
        }

        private void ReceiveDataFromDevice(iDeviceConnectionHandle connection)
        {
            Task.Run(() =>
            {
                while (true)
                {
                    uint receivedBytes = 0;
                    _iDeviceApi.idevice_connection_receive(connection, _inboxBuffer, (uint)_inboxBuffer.Length,
                        ref receivedBytes);
                    if (receivedBytes <= 0) continue;

                    // Do something with your received bytes
                }
            });
        }


And finally, the below line sends data to the iOS device.
  
        _iDeviceApi.idevice_connection_send(connection, bytesPending, (uint)bytesPending.Length, ref sentBytes);


There are a lot of other functions available to you, but these are some important ones. Using these, you can start sending data to your iOS device via USB. Your iOS app won't even know that it's a USB connection. To the app, it's just another TCP Network Connection.

Hopefully this saves you some time, and feel free to comment or message me if you have any questions.

Happy Coding!

Comments

  1. Hi Carlos,
    i'm trying to follow your example and use the library from nuget but ad soon as i call a method on it, i get a DllNotFoundExcecption telling me that it cannot find 'libimobiledevice'
    I have installed the full iTunes (just to be sure) but still no lock. Looking on the services, i can see that Apple Mobile Service is up and running.
    Thanks
    Stefano

    ReplyDelete
    Replies
    1. Hi Stefano,

      I don't think this has to do with your Apple Mobile Service installation. If you have iTunes, and iTunes can recognize when you plug in your device, then it's up and running.

      It sounds like iMobileDevice.Net isn't able to locate the dll's it needs to run. It's a bit odd how they've set it up, but if you look at the Demo project they have in their GitHub

      https://github.com/libimobiledevice-win32/imobiledevice-net/tree/master/iMobileDevice-net.Demo

      You can see how they've done it. In their CSProj file, they've created a build task to copy their dll's to a win7-x86 and a win7-x64 folder. You may have to do something similar to get this to work.

      Delete
  2. Hi Carlos,
    thanks for the reply. Turned out it was a target issue: by default my sample application was targeting .NET Framework 4.6 but the libraries bundled inside the nuget package target specifically version 4.0
    I've been able to run the code :) now i "only" need to figure out how to use this library, since as you said, there's close to zero documentation.

    ReplyDelete
    Replies
    1. Hi, do you guys have any working examples for Xamarin Forms?

      Delete
    2. It would be the same a described above; Xamarin Forms is just a UI library on top of Xamarin [native] so all the C# code is going to be exactly the same whether you're using Forms or not.

      Delete
  3. No problem! Glad you enjoyed it.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  5. Thanks for sharing this Informative content. Well explained. Got to learn new things from your Blog on. ios app development online course

    ReplyDelete
  6. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...
    Thank you very much for this one

    iOS Training

    iOS Course in Chennai

    ReplyDelete
  7. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…iOS App Development Online Course Hyderabad

    ReplyDelete
  8. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    xamarin course
    xamarin training course

    ReplyDelete
  9. This is an interesting method of getting USB communication to a PC, but I must ask how much latency does this solution have both ways?

    ReplyDelete
  10. Great detailed and explained post about Communicating with your iOS app over USB . I am happy if the word is spread. Much obliged for the insight. It's very useful to read it.

    Xamarin App Development Services

    ReplyDelete
  11. One of the key features said to be coming to
    the HomePod is the ability to make or receive phone calls
    Read More At iOS Online Course Bangalore

    ReplyDelete
  12. thanks for interesting post.. i'm really enjoy to visit this site Wordpress Website Development in Dubai

    ReplyDelete
  13. Can you Please Provide both c# and iOS code for app as well as desktop app. Its hard to undertstand this code as it is in fractions.

    ReplyDelete
  14. What would be iOS app code if we want to write iOS app into objective C or swift can you please tell me?

    ReplyDelete
  15. Such a nice post. I really like it. Thanks for sharing.
    android-vs-ios

    ReplyDelete
  16. Thanks for sharing the useful blog about Communicating with an iPhone App over USB using C# & Xamarin along with its Source code.

    iOS App Development Company in Coimbatore


    ReplyDelete
  17. Awesome and interesting article. Great things you've always shared with us. Thanks
    IOS Application Development Pakistan

    ReplyDelete
  18. i m getting unknown error on line

    var error =_iDeviceApi.idevice_connect(deviceHandle, 5050, out iDeviceConnectionHandle connection);

    ReplyDelete
  19. Great blog and very innovative, informative to read. It is useful for the iphone users and they fault to see any problem here comes an immediate solution. Great information!
    Apple Service Center in Chennai
    Motorola Service Center in Chennai

    ReplyDelete
  20. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting. So know it's helpful..
    Mobile App Development Company in Dubai
    Android App Development Company in Dubai
    Mobile App Development Company
    Mobile App Development Company in UAE

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. Thanks for the post and the information you are posted is really good and very worth to learn about all technologies.
    Moto Service Center in Chennai
    Oppo Service Center in Chennai

    ReplyDelete
  23. The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.
    IOS Development pakistan

    ReplyDelete
  24. This post still rocks even after 2 years. I like it and have been reading it over and over just like I have done on this USB Type C Vs Micro USB, Which Is Better?. Hope you also like it.

    ReplyDelete
  25. iOS and android apps Software House Audacity24
    Audacity24 (#ADS) is one of the fast growing IT services provider in international market. We provide quality and reliable IT solutions; combination of real-time analytics, data integration and process in a comprehensive way enables our customers to drive their business with maximum operational efficiency and customized systems for making smarter decisions for better services.

    ReplyDelete
  26. The modern era is ruled by technology and mobile phones are an integral part of this newer world. Realizing the growing trend of apps among users these days, countless businesses are opting for mobile app development. hire an Android app development company in USA. The biggest advantage of hiring a native company is that there is no time zone difference, no language difference, and no communication issue.

    ReplyDelete
  27. Very nice information...amazing post. Thanks for sharing..
    we offer a variety of web design and development services for any sized web projects. We believe that a truly professional and well-designed website will be an effective marketing tool. Our team of web designers and web developers have the tools to take your website to the next level.
    Web development company winnipeg
    Mobile app development winnipeg

    ReplyDelete

  28. Nice blog..i was really impressed by seeing this blog, it was very interesting and it is very useful for me.also the information which you have mentioned here is correct and impressive. Really appreciate.
    Hire Xamarin Developer
    Xamarin Development Company

    ReplyDelete
  29. The biggest challenge that many of them face is how to create and update iOS apps for business needs. Organizations need developers who can provide custom iOS app development but the remuneration per hour is huge.

    ReplyDelete
  30. I reading your post. This is very nice article.I want to twite to my followers.
    If you need baclink servicesBacklink work is here please contact me

    ReplyDelete
  31. Nice post. Thanks for sharing! Can you Please Provide both c# and xamarin iOS code for app as well as desktop app. Its hard to undertstand this code.

    ReplyDelete
  32. wonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    AWS course in Chennai

    ReplyDelete
  33. Thanks for this great and useful information. Daynil Group Solutions is one of the leading IT outsourcing companies in India offering Custom Software development ,Web development, Mobile app development, Devops development services all over the world. Reduce your cost by up to 55-65% by outsourcing your software development with us. In Daynil Group we provide end-end services that include product development, testing, and AWS cloud deployment. We use automated CI/CD tooling and an agile development framework. Hire Reactnative developer , Hire android developer ,Hire ios developer at low cost and save upto 60% of your Development cost

    ReplyDelete
  34. I am really enjoying reading your well written articles.
    It looks like you spend a lot of effort and time.
    Reactjs Training in Chennai |
    Best Reactjs Training Institute in Chennai |
    Reactjs course in Chennai

    ReplyDelete

  35. I’m glad to find another amazing app development blogger.
    on demand service app development

    ReplyDelete
  36. I’m extremely impressed with your writing skills and also with the layout on your weblog. Thank you for sharing. Also visit is fce okene diploma admission  form out

    ReplyDelete
  37. Hi! I simply wanted to give you a huge thumbs up for the great information you have right here on this post. I'll be returning to your blog for more information soon. Thanks for sharing this information on your blog. Do you like shooter games? If yes then refer to my blog and improve your click per second.

    ReplyDelete
  38. i read your article finds very usefull i also have some usefull info about Andriod App Development
    Andriod App Development!

    ReplyDelete
  39. Wow! what a great compilation of blogging advice, The CodeWash. Our offshore developers build apps based on the latest available iOS technology in the market. They are capable of building all kinds of apps for small and large businesses. Hire iOS app Developers with the top experience and highest exposure in serving international clients. 

    ReplyDelete
  40. Thanks for sharing this great ideas with readers like us, it contains such relevant and useful information kebbi state college of nursing basic nursing admission forms out

    ReplyDelete
  41. Enhance communication with your iOS app through effective strategies and techniques. Explore valuable insights and resources available on App On Radar to optimize interactions and user engagement, ensuring your app delivers a seamless and satisfying experience.

    ReplyDelete

Post a Comment

Popular posts from this blog

Creating a PayPal IPN Web API Endpoint

Strategy Pattern: Switch Statements, Begone!