2

虽然我知道使用了 Bonjour,这样我们就不会弄乱 IPAddresses,但我需要编写一个可以“通过指定 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;
}

我错过了什么?

4

1 回答 1

0

错误代码-72004NSNetServicesBadArgumentError。根据文档

- (id)initWithDomain:(NSString *)domain type:(NSString *)type name:(NSString *)name

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

于 2014-01-15T11:58:56.200 回答