Ios 尝试连接时,Peertalk套接字连接返回-1
我正在创建一个可以在Mac和iPhone之间通信的应用程序。我正在为此目的使用 我刚刚将Peertalk核心文件复制到我的项目并运行,但是套接字连接返回-1Ios 尝试连接时,Peertalk套接字连接返回-1,ios,objective-c,swift,macos,sockets,Ios,Objective C,Swift,Macos,Sockets,我正在创建一个可以在Mac和iPhone之间通信的应用程序。我正在为此目的使用 我刚刚将Peertalk核心文件复制到我的项目并运行,但是套接字连接返回-1 // Connect socket struct sockaddr_un addr; addr.sun_family = AF_UNIX; strcpy(addr.sun_path, "/var/run/usbmuxd"); socklen_t socklen = sizeof(addr); if (connect(
// Connect socket
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/var/run/usbmuxd");
socklen_t socklen = sizeof(addr);
if (connect(fd, (struct sockaddr*)&addr, socklen) == -1) {
if (error) *error = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
return NO;
}
但是当我运行原始代码时,连接成功了,我得到了PTUsbHub
对象和连接返回success
值
objc中的示例代码
- (void)startListeningForDevices {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserverForName:PTUSBDeviceDidAttachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
NSNumber *deviceID = [note.userInfo objectForKey:@"DeviceID"];
//code to connect
}];
[nc addObserverForName:PTUSBDeviceDidDetachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
NSNumber *deviceID = [note.userInfo objectForKey:@"DeviceID"];
//code to disconnect
}];
}
我的swift密码
@objc private func startListening(){
let nc = NotificationCenter.default
nc.addObserver(forName: NSNotification.Name.PTUSBDeviceDidAttach, object: PTUSBHub.shared(), queue: nil) { (notification) in
let deviceID = notification.userInfo?["DeviceID"] as? NSNumber
}
nc.addObserver(forName: NSNotification.Name.PTUSBDeviceDidDetach, object: PTUSBHub.shared(), queue: nil, using: { note in
let deviceID = note.userInfo?["DeviceID"] as? NSNumber
}
所以问题是我没有收到附加或分离通知,因为连接不成功
是否需要任何许可?或者你能有人找出真正的问题是什么吗
提前谢谢最后,我发现了我的错误。这是关于应用程序沙箱的。您可以阅读有关沙箱的文章 这是mac的安全功能 我刚刚取消了应用程序沙盒检查,它正在工作
谢谢那么,
错误是什么?据我所知,这个目标C代码是他们的示例代码。你实际的Swift代码是什么?@Alexander现在请看看你到底在哪里建立连接?