我正在创建一个在 Mac 和 iPhone 之间进行通信的应用程序。我正在使用Peertalk为此目的。
我刚刚将 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(fd, (struct sockaddr*)&addr, socklen) == -1) {
if (error) *error = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
return NO;
}
但是当我运行原始代码时,连接成功,并且获得了 PTUsbHub
对象和连接返回 success
值。
obj c 中的示例代码
- (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
}];
}
我的快速代码
@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
}
所以问题是我没有收到附加或分离通知,因为连接不成功。
是否需要任何许可?或者你能找出真正的问题是什么吗?
提前致谢
最佳答案
终于,我发现了自己的错误。这是关于应用程序沙箱的。您可以阅读有关沙箱的文章
- https://developer.apple.com/documentation/security/app_sandbox_entitlements
- https://developer.apple.com/app-sandboxing/
这是 mac 的安全功能。
我刚刚取消选中应用程序沙箱,它正在运行。
谢谢
关于ios - Peertalk 套接字连接在尝试连接时返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56250300/