2016-11-14 8 views
2

연결하려는 서비스가 Bonjour를 통해 게시되었습니다.
Bonjour 브라우저를 사용하여 모든 정보를 찾을 수 있지만 프로그래밍 방식으로 데이터를 수집하려고하면 얻을 수있는 유일한 가치는 서비스 이름입니다.Swift를 사용하는 Bonjour Service Browser가 serviceinfo를 가져 오지 않습니다.

NetService 대리인이 설정되고 netServiceWillPublish 함수가 호출됩니다. DidNotPublish 또는 DidPublish 함수가 실행되지 않습니다.

netServiceBrowser 함수는 게시 된 모든 netServices를 가져 오지만 모든 속성은 데이터 유형의 기본값으로 설정됩니다.

import UIKit 

class BMNSDelegate : NSObject, NetServiceDelegate { 
    func netServiceWillPublish(_ sender: NetService) { 
     print("netServiceWillPublish:\(sender)"); //This method is called 
    } 


    func netService(_ sender: NetService, didNotPublish errorDict: [String : NSNumber]){ 
     print("didNotPublish:\(sender)"); 
    } 

    func netServiceDidPublish(_ sender: NetService) { 
     print("netServiceDidPublish:\(sender)"); 
    } 
    func netServiceWillResolve(_ sender: NetService) { 
     print("netServiceWillResolve:\(sender)"); 
    } 

    func netService(_ sender: NetService, didNotResolve errorDict: [String : NSNumber]) { 
     print("netServiceDidNotResolve:\(sender)"); 
    } 

    func netServiceDidResolveAddress(_ sender: NetService) { 
     print("netServiceDidResolve:\(sender)"); 
    } 

    func netService(_ sender: NetService, didUpdateTXTRecord data: Data) { 
     print("netServiceDidUpdateTXTRecordData:\(sender)"); 
    } 

    func netServiceDidStop(_ sender: NetService) { 
     print("netServiceDidStopService:\(sender)"); 
    } 

    func netService(_ sender: NetService, 
        didAcceptConnectionWith inputStream: InputStream, 
        outputStream stream: OutputStream) { 
     print("netServiceDidAcceptConnection:\(sender)"); 
    } 
} 

class BMBrowserDelegate : NSObject, NetServiceBrowserDelegate, NetServiceDelegate { 

    func netServiceBrowser(_ netServiceBrowser: NetServiceBrowser, 
          didFind netService: NetService, 
          moreComing moreServicesComing: Bool) { 
     let nsnsdel = BMNSDelegate() 
     netService.delegate = nsnsdel 
     netService.resolve(withTimeout: 1) 
     print(netService.domain) // local. 
     print(netService.name) // This property is correct 
     print(netService.type) // _http._tcp. 
     print(netService.addresses) // Optional([]) 
     print(netService.hostName) // nil 
     print(netService.port) // -1 
     print(moreServicesComing) //false 
    } 

} 


let SERVICE_TYPE = "_http._tcp." 
let BM_DOMAIN = "local." 
let browser = NetServiceBrowser() 
let nsbdel = BMBrowserDelegate() 
browser.delegate = nsbdel 
browser.searchForServices(ofType: SERVICE_TYPE, inDomain:   BM_DOMAIN) 
RunLoop.current.run() 

답변

2

오류는 ServiceBrowserDelegate 함수에서 발견되는 서비스 어딘가에 저장 아니므 함수의 마지막 폐기된다는 점이다.

은 여기 작업 예제를 발견
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/61f0c753a080040e4a74b912e6c18dd97fe8bcaa/bk2ch24p853bonjour/ch37p1101bonjour/ViewController.swift

class ViewController: UIViewController, NetServiceBrowserDelegate, NetServiceDelegate { 
var nsb : NetServiceBrowser! 
var services = [NetService]() 

@IBAction func doButton (_ sender: Any!) { 
    print("listening for services...") 
    self.services.removeAll() 
    self.nsb = NetServiceBrowser() 
    self.nsb.delegate = self 
    self.nsb.searchForServices(ofType:"_daap._tcp", inDomain: "") 
} 

func updateInterface() { 
    for service in self.services { 
     if service.port == -1 { 
      print("service \(service.name) of type \(service.type)" + 
       " not yet resolved") 
      service.delegate = self 
      service.resolve(withTimeout:10) 
     } else { 
      print("service \(service.name) of type \(service.type)," + 
       "port \(service.port), addresses \(service.addresses)") 
     } 
    } 
} 

func netServiceDidResolveAddress(_ sender: NetService) { 
    self.updateInterface() 
} 

func netServiceBrowser(_ aNetServiceBrowser: NetServiceBrowser, didFind aNetService: NetService, moreComing: Bool) { 
    print("adding a service") 
    self.services.append(aNetService) 
    if !moreComing { 
     self.updateInterface() 
    } 
} 

func netServiceBrowser(_ aNetServiceBrowser: NetServiceBrowser, didRemove aNetService: NetService, moreComing: Bool) { 
    if let ix = self.services.index(of:aNetService) { 
     self.services.remove(at:ix) 
     print("removing a service") 
     if !moreComing { 
      self.updateInterface() 
     } 
    } 
    } 
} 
+0

솔루션이 제대로 작동하지만, 때로는 netServiceBrowser didFind 서비스를 호출 나던. – SML