iOS — A new LAN Network Scanner library has been born

How it started

It was March of 2011 when I was developing my first -real- project for iOS, the IT Buddy which started as an IP Calculator and now is considered as a real IT Buddy! I was concidering the case to add a LAN Scanner in IT Buddy from the day 1 but my knowledge was limited and there was no any open source projects that I could use.

Five years later and still no decent LAN Scanner library. There are couple of LAN Scanners available but not ready for production.

There is no decent LAN Scanner library for iOS

A lot of people were asking on StackOverflow how to implement a LAN Scanner so I decided that I will put all pieces together (SimplePing, SimplePingHelper, LAN-Scan and MacFinder) and create a decent open source libary for scanning your network through your iOS Device.

By decent I mean:

  1. Able to scan all subnets not only /24
  2. Fast searching without blocking UI
  3. Using the default technique for LAN Scan which is ping & query the ARP table.
  4. Shows IP & MAC Address of available hosts.
  5. Shows Brand of device based on MAC Address

MMLanScan

So the name is MMLanScan and it’s very easy to use.

Download here and copy the MMLanScan folder in your project!

Import MMLANScanner in your controller

#import "MMLANScanner.h"

Add the MMLANScannerDelegate to your controller

@interface YourViewController () <MMLANScannerDelegate>

Declare an MMLANScanner property

@property(nonatomic,strong)MMLANScanner *lanScanner;

Start the scan

[self.lanScanner stop];
self.lanScanner = [[MMLANScanner alloc] initWithDelegate:self];
self.connectedDevices = [[NSMutableArray alloc] init];
[self.lanScanner start];

Implement the delegates methods to receive events

- (void)lanScanDidFindNewAddressWithIP:(NSString*)ipAddress MACAddress:(NSString*)macAddress andHostname:(NSString*)hostname;
- (void)lanScanDidFinishScanning;
- (void)lanScanProgressPinged:(NSInteger)pingedHosts from:(NSInteger)overallHosts;
- (void)lanScanDidFailedToScan;

How it works

MMLanScan works like the classic network scanner. It first ping every host in the network in order to built the ARP table and then is trying to get the MAC Address for each host. If a MAC Address is found then it’s considered that the host exist in the network.

Technical Stuff

Do not update SimplePingHelper since is modified to work with MMLanScan

TODO

MMLanScan is not perfect and has room for improvment.

So if anyone would like to help:

  • C̶o̶n̶v̶e̶r̶t̶ ̶t̶h̶e̶ ̶O̶U̶I̶ ̶t̶e̶x̶t̶ ̶i̶n̶ ̶a̶ ̶d̶i̶c̶t̶i̶o̶n̶a̶r̶y̶ ̶s̶o̶ ̶w̶e̶ ̶c̶a̶n̶ ̶m̶a̶p̶ ̶M̶A̶C̶ ̶A̶d̶d̶r̶e̶s̶s̶ ̶w̶i̶t̶h̶ ̶v̶e̶n̶d̶o̶r̶
  • Make it work in a background thread. Apple’s SimplePing has issues when it comes to GCD (it’s build on C libraries and it seems their callbacks won’t work with GCD)
  • Get hostname from IP address method is not working

If you enjoyed this article check out our publication Rock n Null about mobile and technology!

You can find the library and a demo project here!