Discover and Connect Epson Printer in iOS (Swift)
By: Amjad Ali
Tags: iOS App Development, Epson Printer Support, Swift, Epson, Integration

As we previously discussed here the integration of Epson Printer in your iOS dummy App

Today we will connect that previous discovered IP/Bluetooth of Epson Printer within App :D

Discovered Printers

Sounds good right?! let's start from basic things as in the previous article we discussed many details about the adding SDK and set it within App please concentrate that right now we are discovering Printers both Lan as well Bluetooth

Viewcontroller and UI is required

As my recommended UI mentioned the previous Article, So moving forward by clicking the discovered button we move another controller here in this controller we added these things

Tableview: Where Printers will be discovered or shown

Button (Refresh): by clicking it new printers will be updated/ refreshed and can be discovered in Table view

All the above details you can see through the image, So moving forward as we know how to add the file in the storyboard give a name to file as DiscoveryViewController where printers can discover and connect

Outlet of Tableview

By adding above outlet let me add here one thing more that also inherits UITableViewDataSource and UITableViewDelegate to use that table view within your app and general we know about delegate and data source either wee add through storyboard or in Viewdidload

after adding those inherit views as we know needs to add Protocols of those controllers but let me add here that also add Epos2DiscoveryDelegate as inherit controller along with UITableViewDelegate after adding that it needs also to add their protocols like

This is what it looks like!!

adding inherit controllers and Protocols

If it looks like above Image than great we are going along let now move towards some coding

Initiate some Fileprivate var within your existing controller, i.e.

fileprivate var printerList: [Epos2DeviceInfo] = [] fileprivate var filterOption: Epos2FilterOption = Epos2FilterOption() var delegate: DiscoveryViewDelegate?

after initializing above variable move to view did load and add

printerView.delegate = self printerView.dataSource = self

PrinterView we give the name to our table view any other name you give no fight about a name

Now some recommended override function we are adding in our controller

- ViewDidAppear ad ViewWillDisapper

Now in ViewDidAppear

initialize constate and equal it as Epos2Discovery.start(filterOption, delegate: self)

let result = Epos2Discovery.start(filterOption, delegate: self)

and

if result != EPOS2_SUCCESS.rawValue { print(“nothing”)

}

printerView.reloadData()

if result does not equal to EPOS2_SUCCESS than it will print nothing else numbers of Printers will be added in result as soon as our view controller ap

And in viewWillDisappear

add while Epos2Discovery.stop() == EPOS2_ERR_PROCESSING.rawValue {

// retry stop function

}

printerList.removeAll()

So as we leave the controller it should stop searching Printer

After Implementing didAppear and WillDisappear

So now before moving to Tableview Protocols for Discovering these printers, we need to fill our onDiscovery Protocols so add So simply append deviceInfo in printerList and reload PrinterView

Now move towards over Tableview’s Protocols, So with starting three basic functions as numberOfSections, numberOfRowsInSection and cellForRowAt

Important In cellForRowAt we check that indexPath.section is equal to 0? if yes than within Condition we check indexPath.row is greater than equals to zero and greater than printerList.count? (Printers) if it is than we add printerList[indexPath.row].deviceName in textLabel and printerList[indexPath.row].target in detailTextLabel

And counter of printerList in numberOfRowsInSection and return 2 in numberOfSections

As here see!!

Tableview Protocol stubs

Above please once run your app you will be surprised that Printers will be discovered within your app (If Printer is connected with same LAN)

Printers are discovered

So great!! the Printers are being discovered within your app :)

Now for connecting as we know, DidSelect is Important function So before a move that final function for Connect that Printer we need to to add one function named connectDevice in which we add details of Bluetooth and LAN delegates for connecting and also refresh/ reload functionality within Refresh button

Connect and Refresh/Restart Button

Run and click Refresh button you can see the printers are refreshing

Now its time to move didselect function however Article goes too long soo look didselect function with some delegate and returning back to the root functionality

DidSelectRowAt

and yes with very start above the controller do must add

protocol DiscoveryViewDelegate { func discoveryView(_ sendor:DiscoveryViewController, onSelectPrinterTarget target:String) } the delegated function don't forget this!!

Now just Click any printer you want to add It will be added and shown in your Textfield

Like

Discover and Connect Printer

Great You Implemented :)

If you need further details. email me: amjadalie@outlook.com

Next article, I will let you know the details of Print Receipt

Thanks