首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CFReadStream - Obj-C中的错误处理

CFReadStream - Obj-C中的错误处理
EN

Stack Overflow用户
提问于 2017-01-25 16:35:01
回答 1查看 93关注 0票数 0

如何处理流中的错误?如果用户连接到错误的网络,我想处理这个问题。谢谢!

代码:

代码语言:javascript
复制
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"IP HERE", 7777, &readStream, &writeStream);
_inputStream = (NSInputStream *)CFBridgingRelease(readStream);
_outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);

[_inputStream setDelegate:self];
[_outputStream setDelegate:self];

[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[_inputStream open];
[_outputStream open];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-26 01:47:18

我想出了一个对我的案子有用的解决方案。这段代码将在控制台中打印出当前网络的BSSID,我只需使用if -语句检查BSSID是否与首选网络的BSSID匹配:

代码语言:javascript
复制
#import <SystemConfiguration/CaptiveNetwork.h>

//Checks which network the user is connected to.
CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
NSLog(@"Connected at: %@", myDict);
NSDictionary *myDictionary = (__bridge_transfer NSDictionary*)myDict;
NSString * BSSID = [myDictionary objectForKey:@"BSSID"];
NSLog(@"BSSID is: %@", BSSID);

//Handling wrong/correct BSSID.
if (![BSSID isEqualToString:@"PREFERRED BSSID HERE"]) {
    //Handle error however you want.
}
else {
    //If correct BSSID, handle that here however you want.
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41856851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档