我正在编写一个iPhone应用程序,涉及在后台通过VoIP套接字处理来自服务器的网络事件。我已成功设置套接字,.plist中的后台服务信息以及相应的流,但我无法弄清楚如何在回调中做任何有用的事情。我不知道如何从回调函数中访问我的应用程序的其余状态。这是我在AppDelegate中设置连接的地方:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"AppDelegate: didFinishLaunchingWithOptions");
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)@"irc.freenode.net");
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host, 6667, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
CFStreamClientContext myContext = {
0,
viewController,
(void *(*)(void *info))CFRetain,
(void (*)(void *info))CFRelease,
(CFStringRef (*)(void *info))CFCopyDescription
};
CFOptionFlags registeredEvents = kCFStreamEventHasBytesAvailable |
kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered;
if(CFReadStreamSetClient(readStream, registeredEvents, clientCB, &myContext)) {
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
}
if (!CFReadStreamOpen(readStream)) {
NSLog(@"Read stream could not be opened");
}
else {
NSLog(@"Successfully opened read stream");
}
return YES;
}
这是CFStream事件的回调函数,我遇到了问题:
void clientCB(CFReadStreamRef stream, CFStreamEventType event, void* myPtr) {
switch(event) {
case kCFStreamEventHasBytesAvailable:{
UInt8 buf[32];
CFIndex bytesRead = CFReadStreamRead(stream, buf, 32);
if (bytesRead > 0) {
NSLog(@"Readstream Not Empty %i", buf[0]);//dummy debug marker
}
//I WANT TO ACCESS CLASSES, PROPERTIES, FUNCTIONS, ETC. ABOUT THE REST OF MY PROGRAM HERE (E.G. GET A WORKING POINTER BACK TO THE MAIN APPDELEGATE).
break;
}
case kCFStreamEventErrorOccurred:
case kCFStreamEventEndEncountered:
default:
break;
}
}
这两个函数都在同一个AppDelegate类中定义。我试过通过CFStreamClientContext结构传递指针:VoIP_TestViewController* testViewController = context->info;
,我试图通过:[[UIApplication sharedApplication] delegate]
访问主appDelegate,我尝试了通常的Obj-C和C结构(“这个“,”自我“,”超级“)。
非常感谢任何帮助!
答案 0 :(得分:0)
确保将主应用代理头文件导入到需要调用它的视图中。
用它来调用它:
nameOfYourAppDelegate * appDelegate =(_ nameOfYourAppDelegate *)[[UIApplication sharedApplication] delegate];
//比你可以使用appDelegate做任何你想要的(即appDelegate.somView.someProperty)
答案 1 :(得分:0)
啊,我是个白痴。我忘了导入成员类的类头(即我在尝试访问[ClassA.ClassB方法B]时忘记导入ClassB)。当然,XCode只提供了一个无法识别的选择器运行时错误,而不是一个缺少的声明编译器错误,使得调试比它本来应该更难。谢谢你!#/ p>