2017-01-25 2 views
0

Wie gehe ich mit Fehlern in einem Stream um? Wenn der Benutzer mit dem falschen Netzwerk verbunden ist, möchte ich damit umgehen. Vielen Dank!Fehlerbehandlung in CFReadStream - Obj-C

Code:

- (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]; 
} 

Antwort

0

dachte ich, eine Lösung aus, die in meinem Fall gearbeitet. Dieser Code wird die BSSID des aktuellen Netzwerks in der Konsole ausgeben, und ich überprüfe einfach, ob die BSSID mit der für mein bevorzugtes Netzwerk übereinstimmt, mit einer if-Anweisung:

#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. 
} 
}