I am trying to create a custom framework work that will speak with the external accessory
public class AccessoryClass : NSObject, NSStreamDelegate {
func getConnectedAccessoris() -> Array<EAAccessory>{
let accessories : Array<EAAccessory> = EAAccessoryManager.sharedAccessoryManager().connectedAccessories
return accessories
}
public func openAccessory() {
let _accessories = getConnectedAccessoris()
var _accessory: EAAccessory?
for acsy in _accessories {
if acsy.protocolStrings.contains(PROTOCOL_STRING) {
_accessory = acsy
}
}
if _accessory != nil {
_session = EASession(accessory: _accessory!, forProtocol: protocolString)
if _session != nil {
_session?.inputStream?.delegate = self
_session?.inputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
_session?.inputStream?.open()
_session?.outputStream?.delegate = self
_session?.outputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
_session?.outputStream?.open()
}
}
}
func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
switch (eventCode) {
case NSStreamEvent.None:
print("NSStream None")
break
case NSStreamEvent.OpenCompleted:
print("Open Completed")
break
case NSStreamEvent.HasBytesAvailable:
print("Has Bytes Available")
// _readData()
break
case NSStreamEvent.HasSpaceAvailable:
print("Hase space Available")
// _writeData()
break
case NSStreamEvent.ErrorOccurred:
print("Error occurred")
break
case NSStreamEvent.EndEncountered:
print("End Encountered")
break
default:
print("No stream event")
break
}
}
}
I have tried like this, streamDelegate only getting crash. without this code _session?.inputStream?.delegate = self its working fine.
My doubt was is it possible to use ios delegate methods while creating custom framework. if not what's the procedure to use??
Thanks
Use guard and then set the delegate