<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                **@Block傳值** **.h** ~~~ #import <Foundation/Foundation.h> typedef void(^MyBlock)(NSData *); typedef void(^LengthBlock)(float); @interface HMTMyCustomNetRequest : NSObject<NSURLConnectionDataDelegate> @property (nonatomic,retain)NSMutableData * data; @property (nonatomic,retain)NSURLConnection * connection; @property (nonatomic,copy)MyBlock finishLoadBlock; @property (nonatomic,copy)LengthBlock lengthBlock; // GET請求方式 - (void)requestForGETWithUrl:(NSString *)urlString; // POST請求方式 - (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data; // 取消請求 - (void)cancelNSURLConnection; @end ~~~ **.m** ~~~ #import "HMTMyCustomNetRequest.h" @interface HMTMyCustomNetRequest (){ NSUInteger leng; } @end @implementation HMTMyCustomNetRequest - (void)dealloc{ RELEASE_SAFELY(_data); RELEASE_SAFELY(_connection); Block_release(_finishLoadBlock); Block_release(_lengthBlock); [super dealloc]; } - (void)requestForGETWithUrl:(NSString *)urlString{ NSURL * url = [NSURL URLWithString:urlString]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setHTTPMethod:@"GET"]; /** * Special Considerations During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled. */ self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; } - (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data{ NSURL * url = [NSURL URLWithString:urlString]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:data]; self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; } #pragma mark - 取消協議異步方法調用(關鍵關鍵很關鍵,不做這一步經常出現崩潰的地方) - (void)cancelNSURLConnection{ /** * 如果數據在加載,但是因為其他一些原因,比如跳轉頁面做其他事,而不是正常的數據加載完成或者數據加載失敗 * 一定要手動取消網絡請求連接 * Special Considerations * During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the-- * ---connection finishes loading, fails, or is canceled. * self.connection = [NSURLConnection connectionWithRequest:self delegate:self]; * */ [_connection cancel]; self.connection = nil; } #pragma mark - 服務器開始給客戶端回傳數據,這個方法只會執行一次 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ // 初始化接收數據的空data self.data = [NSMutableData data]; // 取得數據總長度 leng = response.expectedContentLength; } #pragma mark - 客戶端持續接收數據,data是數據片段,整個數據分段返回,這個方法執行的次數取決于數據總長度[response expectedContentLength] - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [self.data appendData:data]; float progressLength = [self.data length] / (float)leng; if (_lengthBlock) { self.lengthBlock(progressLength); } //if (_finishLoadBlock) { //self.finishLoadBlock(_data); //} } #pragma mark - 數據完全下載成功,接收到完整數據 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // 調用完整數據的block方法 if (_finishLoadBlock) { self.finishLoadBlock(_data); } } #pragma mark - 數據下載失敗 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"請求網絡出錯:%@",error); } @end ~~~ **@delegate代理傳值** **.h** ~~~ #import <Foundation/Foundation.h> @class HMTNetWorkRequest; @protocol NetWorkRequestDelegate<NSObject> @optional //netWork請求成功 - (void)netWorkRequest:(HMTNetWorkRequest *)request didSuccessfulReceiveData:(NSData *)data; //netWork請求失敗 - (void)netWorkRequest:(HMTNetWorkRequest *)request didFailed:(NSError *)error; //獲取netWork的下載進度 - (void)netWorkRequest:(HMTNetWorkRequest *)request withProgress:(CGFloat)progress; @end @interface HMTNetWorkRequest : NSObject<NSURLConnectionDataDelegate> @property (nonatomic,assign) id<NetWorkRequestDelegate> delegate; @property (nonatomic,retain)NSURLConnection * connection; - (void)requestForGETWithUrl:(NSString *)urlString; - (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data; // 取消網絡請求 - (void)cancelRequest; @end ~~~ **.m** ~~~ #import "HMTNetWorkRequest.h" @interface HMTNetWorkRequest (){ NSUInteger _totalLength; } @property (nonatomic,retain)NSMutableData * receiveData; @end @implementation HMTNetWorkRequest - (void)dealloc{ RELEASE_SAFELY(_connection); RELEASE_SAFELY(_receiveData); [super dealloc]; } #pragma mark - 不用的時候一定要取消 - (void)cancelRequest{ [_connection cancel]; self.connection = nil; self.delegate = nil; } - (void)requestForGETWithUrl:(NSString *)urlString{ NSURL * url = [NSURL URLWithString:urlString]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setHTTPMethod:@"GET"]; /** * Special Considerations During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled. */ [NSURLConnection connectionWithRequest:request delegate:self]; [request release]; } - (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data{ NSURL * url = [NSURL URLWithString:urlString]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:data]; [NSURLConnection connectionWithRequest:request delegate:self]; [request release]; } #pragma mark - 服務器開始給客戶端回傳數據,這個方法只會執行一次 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ // 服務器開始回傳數據,客戶端需要創建一個空的,可變的Data對象,用于存儲每次獲取的數據片段 self.receiveData = [NSMutableData data]; // 取得請求數據的長度 _totalLength = [response expectedContentLength]; } #pragma mark - 客戶端持續接收數據,data是數據片段,整個數據分段返回,這個方法執行的次數取決于數據總長度[response expectedContentLength] - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [self.receiveData appendData:data]; CGFloat progress = [_receiveData length]/_totalLength; if ([_delegate respondsToSelector:@selector(netWorkRequest:withProgress:)]) { [_delegate netWorkRequest:self withProgress:progress]; } } #pragma mark - 數據完全下載成功,接收到完整數據 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ if ([_delegate respondsToSelector:@selector(netWorkRequest:didSuccessfulReceiveData:)]) { [_delegate netWorkRequest:self didSuccessfulReceiveData:_receiveData]; } self.receiveData = nil; } #pragma mark - 數據下載失敗 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"didFailWithError"); } @end ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看