2013-09-25 98 views
0

我正在使用CocoaAsyncSocket Library嘗試建立從我的iOS設備到某個地方的服務器的簡單TCP連接,現在我試圖簡單地嘗試檢查我的設置,並且我正在連接到遠程IP地址。嘗試連接到iOS上的TCP服務器的SIGBART錯誤

下載我已經打開了以下項目庫後:

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

BTLECentralViewController和UIViewController中沒有窗口屬性,你要使用:

self.window.rootViewController = self.view_controller; 
[self.window makeKeyAndVisible]; 

這應該放在AppDelegate中(UIResponder)班,我相信。

相關問題