2

I'm working on an OSX app that should be able to scan a view images, then generate a PDF and so on.

But I'm already stuck in the first step, since it seems I can't get IKScannerDeviceView to work and there is little to no help to be found.

This is what I have until now, following another question here:

import Cocoa
import Quartz

class ViewController: NSViewController, IKScannerDeviceViewDelegate, ICScannerDeviceDelegate, ICDeviceBrowserDelegate {
    @IBOutlet  var scannerView: IKScannerDeviceView!

    var deviceBrowser:ICDeviceBrowser!

    override func viewDidLoad() {
        super.viewDidLoad()

        scannerView.delegate = self

        deviceBrowser = ICDeviceBrowser()
        deviceBrowser.delegate = self
        deviceBrowser.browsedDeviceTypeMask = .Scanner
        deviceBrowser.start()
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }




    func scannerDeviceDidBecomeAvailable(scanner: ICScannerDevice) {
        scanner.requestOpenSession()
    }

    func deviceBrowser(browser: ICDeviceBrowser, didAddDevice device: ICDevice, moreComing: Bool) {

        if device.type == ICDeviceType.Scanner{
            scannerView.scannerDevice = device as! ICScannerDevice
        }
    }

    func deviceBrowser(browser: ICDeviceBrowser, didRemoveDevice device: ICDevice, moreGoing: Bool) {
        device.requestCloseSession()
    }

    func didRemoveDevice(device: ICDevice) {
        device.requestCloseSession()
    }

    func device(device: ICDevice, didEncounterError error: NSError?) {
        print("error")
        print(error?.description)
    }
}

This is what happens: enter image description here

As you can see, I the scan button is gray and I can't scan. Scanning works when accessed in the settings

Edit: I tried to implement the ICDeviceBrowserDelegate like in the ScannerBrowser example from Apple, but no method is ever called, although the example from Apple works...

import Cocoa
import Quartz

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var deviceBrowser:ICDeviceBrowser!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        deviceBrowser = ICDeviceBrowser()
        deviceBrowser.delegate = self
        deviceBrowser.browsedDeviceTypeMask = ICDeviceTypeMask(rawValue:
            ICDeviceLocationTypeMask.Local.rawValue |
                ICDeviceLocationTypeMask.Shared.rawValue |
                ICDeviceLocationTypeMask.Bonjour.rawValue |
                ICDeviceLocationTypeMask.Remote.rawValue |
                ICDeviceLocationTypeMask.Bluetooth.rawValue)!
        deviceBrowser.start()

        print(deviceBrowser)
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


}

extension AppDelegate: ICDeviceBrowserDelegate{
    func deviceBrowser(browser: ICDeviceBrowser, didAddDevice device: ICDevice, moreComing: Bool) {
        print("didAddDevice")
        print(device)
    }

    func deviceBrowser(browser: ICDeviceBrowser, didRemoveDevice device: ICDevice, moreGoing: Bool) {
        print("didRemoveDevice")
        print(device)
    }

    func deviceBrowser(browser: ICDeviceBrowser, deviceDidChangeName device: ICDevice) {
        print("deviceDidChangeName")
        print(device)
    }

    func deviceBrowser(browser: ICDeviceBrowser, deviceDidChangeSharingState device: ICDevice) {
        print("deviceDidChangeSharingState")
        print(device)
    }

    func deviceBrowser(browser: ICDeviceBrowser, requestsSelectDevice device: ICDevice) {
        print("requestSelectDevice")
        print(device)
    }
}
user2529173
  • 1,884
  • 6
  • 30
  • 43
  • Which delegate methods are called? Check out the ScannerBrowser sample from Apple. – Willeke Aug 16 '16 at 15:08
  • No delegate method is called... Updated the question – user2529173 Aug 16 '16 at 18:54
  • Add `ICDeviceTypeMaskScanner` to `browsedDeviceTypeMask`. And don't forget to set the delegate of the device in `didAddDevice`. – Willeke Aug 16 '16 at 21:29
  • Oh cool, the didAddDevice method gets called now after I've added the ICDeviceTypeMaskSca‌nner . That's a bit strange in Swift... – user2529173 Aug 17 '16 at 07:45
  • So, this won't even compile for me in Xcode 8.2.1. It says the delegate method parameter names are incorrect, and offers up the remove method signature. Thing is, I copied and pasted from the header! – Rick Jan 29 '17 at 00:26
  • Red herring—I added the didRemove method, and now it all compiles. Just Swift reflecting Apple’s quality of late. – Rick Jan 29 '17 at 02:23

0 Answers0