我正在使用CocoaAsyncSocket Library尝试建立一个简单的TCP连接,从我的iOS设备到某个地方的服务器,现在我只是想尝试检查我的设置而且我连接到远程IP地址。
下载库后,我打开了以下项目:
GCD-> Xcode-> ConnectTest->移动
我打开了这个,因为我相信这是最简单的例子,并展示了我需要做的事情。 我在编辑器中运行了该程序而没有触及代码,它按预期工作。但是当我将代码带到我的程序时,我得到了以下日志错误:
-[BTLECentralViewController window]: unrecognized selector sent to instance 0x17e2c460
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BTLECentralViewController window]: unrecognized selector sent to instance 0x17e2c460'
*** First throw call stack:
(0x314a0e8b 0x3b79a6c7 0x314a47b7 0x314a2f55 0x313f1e98 0xc5e89 0x33c2ab3b 0x33c2a8f9 0x33db75a3 0x33cd51df 0x33cd4fe9 0x33cd4f7d 0x33c26533 0x338adf43 0x338a9767 0x338a95f9 0x338a900d 0x338a8e1f 0x338a2b4d 0x3146bf71 0x314698ff 0x31469c4b 0x313d4541 0x313d4323 0x3610b2eb 0x33c8b1e5 0xc4bb9 0x3bc93ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
同样我从他们的AppDelegate类中设置了连接代码。我把它拿到了视频控制器中,我想要激活它。
所以现在我的viewcontroller.h看起来像这样:
#import <UIKit/UIKit.h>
@class GCDAsyncSocket;
@class BTLECentralViewController;
@interface BTLECentralViewController : UIViewController <UIApplicationDelegate>
{
GCDAsyncSocket *async_socket;
}
@property (nonatomic) IBOutlet BTLECentralViewController *view_controller;
@end
随附的.m文件如下。我只会发布我已添加新代码的代码方法。我不会浪费你的时间通过整个班级。
#import "GCDAsyncSocket.h"
#if USE_SECURE_CONNECTION
#define HOST @"www.paypal.com"
#define PORT 443
#else
#define HOST @"google.com";
#define PORT 80
#endif
- (void)viewDidLoad
{
[super viewDidLoad];
// Start up the CBCentralManager
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
dispatch_queue_t mainQueue = dispatch_get_main_queue();
async_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
#if USE_SECURE_CONNECTION
{
NSString *host = HOST;
uint16_t port = PORT;
DDLogInfo(@"Connecting to \"%@\" on port %hu...", host, port);
self.viewController.label.text = @"Connecting...";
NSError *error = nil;
if (![asyncSocket connectToHost:@"www.paypal.com" onPort:port error:&error])
{
DDLogError(@"Error connecting: %@", error);
self.viewController.label.text = @"Oops";
}
}
#else
{
NSString *host = HOST;
uint16_t port = PORT;
// DDLogInfo(@"Connecting to \"%@\" on port %hu...", host, port);
_estimote_3_label.text = @"Connecting...";
NSError *error = nil;
if (![async_socket connectToHost:host onPort:port error:&error])
{
// DDLogError(@"Error connecting: %@", error);
_estimote_3_label.text = @"Oops";
}
// You can also specify an optional connect timeout.
// NSError *error = nil;
// if (![asyncSocket connectToHost:host onPort:80 withTimeout:5.0 error:&error])
// {
// DDLogError(@"Error connecting: %@", error);
// }
}
#endif
// Normal iOS stuff...
self.window.rootViewController = self.view_controller;
[self.window makeKeyAndVisible];
}
正如我所说,我对此的希望是在调试日志中出现一个“已连接”字符串。但是当我运行它时,我得到调试日志错误,我已经粘贴在顶部,并且在我的主类中出现了SIGBART错误,如下所示:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
任何有更多使用图书馆经验的人都可以帮助我,告诉我它做错了什么以及为什么我会收到这些错误?
请注意,在编辑器中,示例项目工作正常并在我的iPhone 5上运行它可以正常工作。只是当我将相关代码带到我的项目中时,我得到了这些问题。
答案 0 :(得分:0)
BTLECentralViewController和UIViewController没有您尝试使用的窗口属性:
self.window.rootViewController = self.view_controller;
[self.window makeKeyAndVisible];
我应该把它放在AppDelegate(UIResponder)类中。我相信。