想要完成的功能:
在一个APP中插入一个TCP Socket,然后连接到我的Server,可以远程接受我的指令进行一些操作。
创建一个CommandClient类
CommandClient.h
#import "CommandClient.h"
#import <UIKit/UIKit.h>
@implementation CommandClient
+ (CommandClient *)sharedInstance {
static CommandClient *sharedInstance = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (void)initialize {
static dispatch_once_t once;
dispatch_once(&once, ^{
NSLog(@"Start Creating GCDAsyncSocket");
FMUtils = objc_getClass("FMUtils");
socketClient =[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
});
}
- (BOOL)startServer {
NSLog(@"Start startServer GCDAsyncSocket");
NSError *error;
NSString *host = @"192.168.0.9";
uint16_t port = 8666;
BOOL ret = [socketClient connectToHost:host onPort:port error:&error];
if (!ret) {
NSLog(@"socketClient Connection: %@", error);
}
return ret;
}
- (void)stopServer {
NSLog(@"Start stopServer GCDAsyncSocket");
[socketClient disconnect];
}
#pragma mark - GCDAsyncSocketDelegate
// 已连接
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
[socketClient readDataWithTimeout:-1 tag:0];
NSLog(@"Connected: %@:%d", host, port);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"State" message:@"Connection Server Success!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert addButtonWithTitle:@"Yes"];
[alert show];
}
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString* sName = [dict objectForKey:@"sName"];
if ([sName isEqual:@"getSkey"]) {
NSString* sid = [dict objectForKey:@"sid"];
NSString* result = [FMUtils encrySkey:sid];
NSData *requestData = [result dataUsingEncoding:NSUTF8StringEncoding];
[sock writeData:requestData withTimeout:-1. tag:0];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"State" message:@"send Server Success!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; //这两句注释掉就会百分百崩溃
[alert release];
}
[socketClient readDataWithTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
}
@end
xm文件:
#import"CommandClient.h"
#import <UIKit/UIKit.h>
%hook UIApplication
- (id)init {
self = %orig;
if (self) {
// init server when launched
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[[CommandClient sharedInstance] initialize];
[[CommandClient sharedInstance] startServer];
}];
// start/stop server for background
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[[CommandClient sharedInstance] stopServer];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[[CommandClient sharedInstance] startServer];
}];
}
return self;
}
%end
不添加这两句就百分百崩溃
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“State” message:@“send Server Success!” delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert release];
错误信息:
task_set_exception_ports(B07, 400, F03, 0, 0) failed with error (4: (os/kern) invalid argument)
而且我测试了下,不停的发包的过程中,只要滑动界面,也会崩溃。
我对iOS的机制不太了解,狗神,请问哪里做错了?