I'm developing a watchOS 6 standalone app with Xcode and Objective-C. This app should connect to a TCP server. If I run the app on the simulator it works, if I run it on my physical Apple Watch Series 4 I get this error: The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -72000).Here is the code:InterfaceController.h#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
@interface InterfaceController : WKInterfaceController <NSStreamDelegate>
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSMutableArray *messages;
}
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *connectedLabel;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *dataReceivedLabel;
@endInterfaceController.m#import "InterfaceController.h"
@interface InterfaceController () {
NSString* dataToSend;
// Server Info
NSString* ipAddress;
NSString* port;
}
@end
@implementation InterfaceController
@synthesize connectedLabel;
@synthesize dataReceivedLabel;
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
ipAddress = @"192.168.0.68";
port = @"12345";
dataToSend = @"Hello TCP!";
[connectedLabel setText:@"Disconnected"];
}
- (IBAction)sendMessage {
NSString *response = [NSString stringWithFormat:@"msg:%@", dataToSend];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}
- (void) messageReceived:(NSString *)message {
[messages addObject:message];
[dataReceivedLabel setText:message];
NSLog(@"%@", message);
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@"stream event %lu", (unsigned long)streamEvent);
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
connectedLabel.text = @"Connected";
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream)
{
uint8_t buffer[1024];
NSInteger len;
while ([inputStream hasBytesAvailable])
{
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output)
{
NSLog(@"server said: %@", output);
[self messageReceived:output];
}
}
}
}
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"Stream has space available now");
break;
case NSStreamEventErrorOccurred:
NSLog(@"%@",[theStream streamError].localizedDescription);
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
connectedLabel.text = @"Disconnected";
NSLog(@"close stream");
break;
default:
NSLog(@"Unknown event");
}
}
- (IBAction)connectToServer {
NSLog(@"Setting up connection to %@ : %i", ipAddress, [port intValue]);
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) ipAddress, [port intValue], &readStream, &writeStream);
messages = [[NSMutableArray alloc] init];
[self open];
}
- (IBAction)disconnect {
[self close];
}
- (void)open {
NSLog(@"Opening streams.");
outputStream = (__bridge NSOutputStream *)writeStream;
inputStream = (__bridge NSInputStream *)readStream;
[outputStream setDelegate:self];
[inputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
[inputStream open];
[connectedLabel setText:@"Connected"];
}
- (void)close {
NSLog(@"Closing streams.");
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];
inputStream = nil;
outputStream = nil;
[connectedLabel setText:@"Disconnected"];
}
@end
Post
Replies
Boosts
Views
Activity
Hi,Is it possibile to make a watchOS 6 app that establish a Socket TCP Connection?Like what i did in iOS by using the CocoaAsyncSocket networking library.
I saw that the Core NFC framework is only available for iOS and Mac Catalyst.Therefore is it possible to make a watchOS app able to read NFC tags?Best regards.
Hello. I have Navigation View with a List and 2 cells like this:NavigationView {
List{
NavigationLink(destination: TabScene()) {
// Cella 1
HStack {
Image(title[0].icon)
Text(title[0].title).font(.system(size: 30))
}
}
// Cella 2
HStack {
Image(title[1].icon)
Text(title[1].title).font(.system(size: 30))
} // Con la tap sulla cella si attiva la State var a true
.onTapGesture {
self.showTabView.toggle()
} // Quando la state var va a true si presenta la scena desiderata.
.sheet(isPresented: $showTabView, content: {
CreditsView()
})
}
.navigationBarTitle("ARPlanets 2")
.navigationViewStyle(DefaultNavigationViewStyle())
.listStyle(GroupedListStyle())
}Currently the background is white and it becomes black with the dark mode. Anyway i want to choose a custom background Color for my app. How can i do that? So far i could not set a background Color for the Navigation View with a List embed inside it.
When the Apple TV is off, is it possibile to turn on it (and the tv attached to it via HDMI) with a remote push notification from a server from an app?
I have an app written in Objective-C. I tried to import it to Mac Catalyst and when i build it for "My Mac" i get this error:OpenGLES is deprecated and is not available when building for Mac Catalyst.How can i fix it? Best regards.