简体   繁体   中英

Connect To a Bonjour Service with IP address

While I know Bonjour is used so that we don't get our hands messy with IPAddresses, but I need to write an app that can "manually add a new service by specifying the IPAddress".

According to the docs, one can do that by creating a connection by name (where the name is now the ipaddress) [Reference: 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];
}

The service doesn't resolve with errordict

Printing description of errorDict:

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

What am I missing ?

Error code -72004 is NSNetServicesBadArgumentError . According to the documentation of

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

you should use @"local." (and not @"" ) for the local domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM