使用IP地址连接到Bonjour服务

时间:2014-01-15 11:51:02

标签: ios objective-c bonjour nsnetservice

虽然我知道使用Bonjour是为了让我们不会弄乱我的手,但是我需要编写一个可以“通过指定IPAddress手动添加新服务”的应用程序。

根据文档,可以通过按名称创建连接(其名称现在是ipaddress)[参考:https://developer.apple.com/library/ios/documentation/Networking/Conceptual/NSNetServiceProgGuide/Articles/ResolvingServices.html#//apple_ref/doc/uid/20001078-SW7]

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    switch (buttonIndex) {
        case 0:
            //Cancel
            break;
        case 1:
            //OK
            NSLog(@"%@",[[alertView textFieldAtIndex:0] text]);
            [self searchServiceOfType:SERVICE_TYPE inDomain:@"" withName:[[alertView textFieldAtIndex:0] text]];
            break;
        default:
            break;
    }
    }


-(void)searchServiceOfType:(NSString *)serviceType inDomain:(NSString *)domain withName:(NSString*)serviceName{

    NSNetService *service;
    service = [[NSNetService alloc] initWithDomain:domain
                                              type:serviceType
                                              name:serviceName];
    [service setDelegate:self];
    [service stop];
    [service resolveWithTimeout:5.0];
}

服务无法解决errordict

Printing description of errorDict:

{
    NSNetServicesErrorCode = "-72004";
    NSNetServicesErrorDomain = 10;
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

错误代码-72004NSNetServicesBadArgumentError。 根据

的文件
- (id)initWithDomain:(NSString *)domain type:(NSString *)type name:(NSString *)name

您应该将@"local."(而不是@"")用于本地域。