I am trying to set up a connection between an iPhone application and a Mac application using NSNetService.
I went as far as setting up a Mac application to publish NSNetService, which can open an iPhone application, but I cannot figure out how to send data between them.
In my Mac application, I publish my NSNetService with:
self.netService = [[NSNetService alloc] initWithDomain:@"" type:@"_sputnik._tcp" name:@"" port:port]; if (self.netService) { [self.netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:@"PrivateMyMacServiceMode"]; [self.netService setDelegate:self]; [self.netService publish]; [self.netService startMonitoring]; NSLog(@"SUCCESS!"); } else { NSLog(@"FAIL!"); }
Everything looks fine and the delegate method starts - (void)netServiceDidPublish:(NSNetService *)sender (on a Mac application).
In an iPhone application, I make an instance of NSNetServiceBrowser , and after a second or two it calls the didFindService delegate didFindService .
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing { TRACE; NSLog(@"moreComing: %d", moreComing); self.connectedService = aNetService; if (!moreComing) { self.connectedService.delegate = self; [self.connectedService resolveWithTimeout:30]; } }
And right after that, it starts the following delegate method - (void)netServiceDidResolveAddress:(NSNetService *)sender .
Here I do not know if this is due to not, cannot find any och connect method on sender .
So, I tried to write to the output stream using
[self.connectedService getInputStream:&istream outputStream:&ostream]; NSString *test = @"test"; NSData *data = [test dataUsingEncoding:NSUTF8StringEncoding]; [ostream write:[data bytes] maxLength:[data length]];
As well as updating TXTRecordData using [sender setTXTRecordData:data]; . I am not getting any errors and nothing is happening in my Mac application. The Mac application has a delegate method - (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data .
I do not know how to act. I suppose I am missing something, but I donβt know if it was in a Mac or iPhone application. I would suggest that I first need to somehow connect from the iPhone application to the Mac, and in the Mac application add something that listens for the connection.