.h文件,使用了单例模式的宏
#import <Foundation/Foundation.h>
#import "Singleton.h"
/**
SSDP服务
*/
@interface OpenHABSSDPService : NSObject
singleton_interface(OpenHABSSDPService)
/**
加载SSDP
*/
- (void)loadCocoaSSDP;
@end
.m文件的实现内容
#import "OpenHABSSDPService.h"
#import <CocoaSSDP/SSDPServiceBrowser.h>
#import <CocoaSSDP/SSDPService.h>
#import <CocoaSSDP/SSDPServiceTypes.h>
#define kOpenHABkST @"urn:schemas-upnp-org:service:xmyy:openhab2"
#define kOpenHABkSsdpST @"urn:schemas-upnp-org:device:service:xmyy:openhab2"
@interface OpenHABSSDPService ()<SSDPServiceBrowserDelegate>
{
SSDPServiceBrowser *_browser;
NSMutableArray *_services;
}
@end
@implementation OpenHABSSDPService
singleton_implementation(OpenHABSSDPService)
-(instancetype)init
{
self = [super init];
if (self)
{
}
return self;
}
#pragma mark - SSDP服务相关
- (void)loadCocoaSSDP{
_browser = [[SSDPServiceBrowser alloc] initWithServiceType:kOpenHABkST];
_browser.delegate = self;
[_browser startBrowsingForServices];
_services = [[NSMutableArray alloc] init];
}
#pragma mark - CocoaSSDP browser delegate methods
- (void) ssdpBrowser:(SSDPServiceBrowser *)browser didNotStartBrowsingForServices:(NSError *)error {
NSLog(@"SSDP Browser got error: %@", error);
}
- (void) ssdpBrowser:(SSDPServiceBrowser *)browser didFindService:(SSDPService *)service {
NSLog(@"\nSSDP Browser found: %@ \n IP:%@", service.serviceType,service.location);
if (nil == service.location)
{
return;
}
NSString *serviceLocStr = [service.location absoluteString];
}
- (void) ssdpBrowser:(SSDPServiceBrowser *)browser didRemoveService:(SSDPService *)service {
NSLog(@"SSDP Browser removed: %@", service);
}
@end