本文以及相关的系列文章是我总结的iOS网络开发方面的知识点,本文是第二篇,主要分析了Cocoa Streams中的几个重要类
Cocoa Streams实际上是Objective-C对CFNetwork的简单封装,主要包括了三个类:NSStream, NSInputStream, and NSOutputStream。本部分的接口接口比較简单,使用方法一目了然。
我在这里就仅仅列出接口,方便查阅。
对CFNnework不明确的看IOS研究之网络编程(一)-CFNetwork使用具体解释
NSStream接口例如以下:
@interface NSStream : NSObject - (void)open; - (void)close; - (id <NSStreamDelegate>)delegate; - (void)setDelegate:(id <NSStreamDelegate>)delegate; // By default, a stream is its own delegate, and subclassers of NSInputStream and NSOutputStream must maintain this contract. [someStream setDelegate:nil] must restore this behavior. As usual, delegates are not retained. - (id)propertyForKey:(NSString *)key; - (BOOL)setProperty:(id)property forKey:(NSString *)key; - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode; - (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode; - (NSStreamStatus)streamStatus; - (NSError *)streamError; @end @protocol NSStreamDelegate <NSObject> @optional - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode; @end #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))//不能在ios上使用 @interface NSStream (NSSocketStreamCreationExtensions) + (void)getStreamsToHost:(NSHost *)host port:(NSInteger)port inputStream:(NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream; @end #endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@
interface
NSStream
:
NSObject
-
(
void
)
open
;
-
(
void
)
close
;
-
(
id
<
NSStreamDelegate
>
)
delegate
;
-
(
void
)
setDelegate
:
(
id
<
NSStreamDelegate
>
)
delegate
;
// By default, a stream is its own delegate, and subclassers of NSInputStream and NSOutputStream must maintain this contract. [someStream setDelegate:nil] must restore this behavior. As usual, delegates are not retained.
-
(
id
)
propertyForKey
:
(
NSString *
)
key
;
-
(
BOOL
)
setProperty
:
(
id
)
property
forKey
:
(
NSString *
)
key
;
-
(
void
)
scheduleInRunLoop
:
(
NSRunLoop *
)
aRunLoop
forMode
:
(
NSString *
)
mode
;
-
(
void
)
removeFromRunLoop
:
(
NSRunLoop *
)
aRunLoop
forMode
:
(
NSString *
)
mode
;
-
(
NSStreamStatus
)
streamStatus
;
-
(
NSError *
)
streamError
;
@
end
@
protocol
NSStreamDelegate
<
NSObject
>
@
optional
-
(
void
)
stream
:
(
NSStream *
)
aStream
handleEvent
:
(
NSStreamEvent
)
eventCode
;
@
end
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))//不能在ios上使用
@
interface
NSStream
(
NSSocketStreamCreationExtensions
)
+
(
void
)
getStreamsToHost
:
(
NSHost *
)
host
port
:
(
NSInteger
)
port
inputStream
:
(
NSInputStream *
*
)
inputStream
outputStream
:
(
NSOutputStream *
*
)
outputStream
;
@
end
#endif
|
NSInputStream和NSOutputStream都是继承NSStream,接口例如以下
@interface NSInputStream : NSStream - (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len; - (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len; - (BOOL)hasBytesAvailable; @end @interface NSOutputStream : NSStream - (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)len; - (BOOL)hasSpaceAvailable; @end
1
2
3
4
5
6
7
8
9
10
|
@
interface
NSInputStream
:
NSStream
-
(
NSInteger
)
read
:
(
uint8_t *
)
buffer
maxLength
:
(
NSUInteger
)
len
;
-
(
BOOL
)
getBuffer
:
(
uint8_t *
*
)
buffer
length
:
(
NSUInteger *
)
len
;
-
(
BOOL
)
hasBytesAvailable
;
@
end
@
interface
NSOutputStream
:
NSStream
-
(
NSInteger
)
write
:
(
const
uint8_t *
)
buffer
maxLength
:
(
NSUInteger
)
len
;
-
(
BOOL
)
hasSpaceAvailable
;
@
end
|
但这里getStreamsToHost方法,在iOS上不可用。那么在iOS上改怎样初始化NSStream呢?这事实上是iOS的一个小bug。苹果已经给出了答案,须要我们自己为NSStream加入一个类目: