I'm trying to set upp a communication between a iPhone app and a Mac app using NSNetService.
I've come so far as to setup the Mac app to publish a NSNetService that the iPhone app can discover but I can't figure out how send data between them.
In my Mac app 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 - (void)netServiceDidPublish:(NSNetService *)sender
(in the Mac app) is triggered.
In the iPhone app I make an instance of NSNetServiceBrowser
and after a second or two it triggers the didFindService
delegate method.
- (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 directly after this it triggers the next delegate method - (void)netServiceDidResolveAddress:(NSNetService *)sender
.
Here I don't know if it's connected of not, can't find any connect method och connect state on sender
.
So I tried writing to the output-stream with:
[self.connectedService getInputStream:&istream outputStream:&ostream];
NSString *test = @"test";
NSData *data = [test dataUsingEncoding:NSUTF8StringEncoding];
[ostream write:[data bytes] maxLength:[data length]];
And also updating the TXTRecordData with [sender setTXTRecordData:data];
. I don't get any errors and nothing happens in my Mac App. The Mac app has the delegate method - (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
.
I don't know how to proceed. I figure I'm missing something, but I don't know if it's in the Mac app or iPhone app. I would guess that I first need to connect somehow from the iPhone app to the Mac app, and in the Mac app add something that listens to a connection.