iOS4 实现 -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]?

2023-11-29

我该如何实施-[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]对于 iOS

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0

#import <objc/runtime.h>
#import "NSURLConnection+iOS4.h"

// Dynamically add -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:].
void *sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue, void (^handler)(NSURLResponse*, NSData*, NSError*));
void *sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue, void (^handler)(NSURLResponse*, NSData*, NSError*)) {

    // How should we implement this?

}

@implementation NSURLConnection (SendAsync)

+ (void)load {
    SEL sendAsyncSelector = @selector(sendAsynchronousRequest:queue:completionHandler:);
    if (![NSURLConnection instancesRespondToSelector:]) {
        class_addMethod([self class], sendAsyncSelector, (IMP)sendAsynchronousRequest4, "v@:@@@");
    }
}

@end

#endif

// NSURLConnection+SendAsync.h

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0

#import <Foundation/Foundation.h>

@interface NSURLConnection (SendAsync)

@end

#endif


// NSURLConnection+SendAsync.m

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0

typedef void (^URLConnectionCompletionHandler)(NSURLResponse *response, NSData *data, NSError *error);

@interface URLConnectionDelegate : NSObject <NSURLConnectionDataDelegate>

@property (nonatomic, strong) NSURLResponse *response;
@property (nonatomic, strong) NSMutableData *data;
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, copy) URLConnectionCompletionHandler handler;

@end 

@implementation URLConnectionDelegate 

@synthesize response;
@synthesize data;
@synthesize queue;
@synthesize handler;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)theResponse {
    self.response = theResponse;
    [data setLength:0]; // reset data
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
    [data appendData:theData]; // append incoming data
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    self.data = nil;
    if (handler) { [queue addOperationWithBlock:^{ handler(response, nil, error); }]; }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    // TODO: Are we passing the arguments to the block correctly? Should we copy them?
    if (handler) { [queue addOperationWithBlock:^{ handler(response, data, nil); }]; }
}

@end

#import <objc/runtime.h>
#import "NSURLConnection+SendAsync.h"

// Dynamically add @property (nonatomic,readonly) UIViewController *presentingViewController.
void sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue,
                              URLConnectionCompletionHandler handler);
void sendAsynchronousRequest4(id self, SEL _cmd, NSURLRequest *request, NSOperationQueue *queue,
                              URLConnectionCompletionHandler handler) {

    URLConnectionDelegate *connectionDelegate = [[URLConnectionDelegate alloc] init];
    connectionDelegate.data = [NSMutableData data];
    connectionDelegate.queue = queue;
    connectionDelegate.handler = handler;
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request
                                                                delegate:connectionDelegate];
    NSAssert(connection, nil);
}

@implementation NSURLConnection (SendAsync)

+ (void)load {
    SEL sendAsyncSelector = @selector(sendAsynchronousRequest:queue:completionHandler:);
    if (![NSURLConnection instancesRespondToSelector:sendAsyncSelector]) {
        class_addMethod(object_getClass([self class]),
                        sendAsyncSelector, (IMP)sendAsynchronousRequest4, "v@:@@@");
    }
}

@end

#endif
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS4 实现 -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]? 的相关文章

随机推荐

  • 实时人脸检测不起作用

    这段代码没有显示相机中人脸的检测 即使没有错误 我希望在相机中实时检测到脸部 周围有红色乡绅 但我认为我没有正确放置代码 或者我应该在 Viewdidload 或其他东西中放置一些东西 import UIKit import CoreIma
  • cUrl - 获取 html 响应正文

    我确信这相当简单 我正在使用下面的函数来检索网站原始 html 为了解析它 在测试期间 我决定在 stackoverflow com 上运行我的代码 Chrome 不是获取 html 响应 而是打印出实际的站点 而不是将 html 分配给它
  • Java RegEx 不区分大小写吗?

    在Java中 当执行replaceAll来查找正则表达式模式时 例如 replaceAll i b w b s 1 b 1 删除重复的连续的不区分大小写的单词 例如测试测试 我不确定我把 i 我读到它应该在开头 但是如果我把它拿出来 那么我
  • 在 R 中如何舍入为 1、1.5、2 等而不是 1、2 或 1.1、1.2、1.3?

    我想将数字四舍五入到最接近的一半或整数 所以我想将 4 2 舍入到 4 4 3 到 4 5 以及 4 8 到 5 我使用舍入选项尝试了一些操作 gt round 4 34 1 1 4 3 gt round 4 34 1 4 gt round
  • Spark 是否可以设置默认存储级别?

    在 Spark 中 可以显式设置 RDD 和 Dataframe 的存储级别 但是否可以更改默认存储级别 如果可以 如何实现 如果不是 那为什么不可能呢 到处都有类似的问题 但答案只是指解决方案是显式设置存储级别 而无需进一步解释 我建议看
  • ggplot2 在 x 轴上的年变量末尾不断添加 0.5

    所以我试图制作一个分组条形图Year在 x 轴上和Number在 y 上 分组为Nationality并由面Municipality 下面是数据 2017 年在中间 所以没有显示 gt head pres munic Year Munici
  • Angular UI 路由器和使用动态模板

    我正在尝试使用 rootscope 值加载模板文件 就其名称而言 我有一个初始化控制器 它将 rootScope template 设置为 whatever html 然后我的路线如下 stateProvider state url acc
  • 强制光标移动到页面的一侧

    我想知道这是否可能 我想要一个链接 上面写着 单击我 下面还有一张图片 该图片也超链接到另一个页面 在用户尝试将鼠标悬停在 单击我 链接上之前 光标应该在到达 单击我 链接之前自动移动到图像 顺便说一下 这不会成为真正网站的一部分 这只是个
  • 将长模板文字行换行为多行,而不在字符串中创建新行

    在 es6 模板文字中 如何将长模板文字包装为多行而不在字符串中创建新行 例如 如果您这样做 const text a very long string that just continues and continues and conti
  • 如何根据状态栏中设备的时间更新时间?

    所以我有以 HH mm 格式显示时间的标签 该标签将每隔一分钟定期更新一次 我实际上可以通过使用来做到这一点timer就像下面的代码 class HomeVC UIViewController IBOutlet var timeLabel
  • Excel 索引匹配 - 具有多个结果的部分字符串

    我正在尝试调整我在在线示例电子表格中找到的这段代码 但我不太明白它 原始电子表格基本上基于用户定义的查找执行索引 匹配 并在串联列表中整齐地列出匹配项 示例电子表格的输出如下所示 https i stack imgur com DyahB
  • Django 通过电子邮件发送错误

    我一直在努力让电子邮件在 Django 中工作以进行日志记录以及 500 和 404 错误 但我一生都无法让它工作 我有DEBUG False以及所有其他设置 我的电子邮件设置如下 EMAIL HOST host EMAIL PORT 58
  • Objective-C 生成位于给定 cgrect 中的随机点

    我的要求是在给定区域生成一个随机点 即我有一个具有一定空间的 Cg 矩形 我需要在这个矩形中生成一个随机点 在这种情况下我该如何进行 CGPoint randomPointInRect CGRect r CGPoint p r origin
  • 在 Struts 2 中使用 getText() 获取属性

    我正在使用 JSP 开发 Struts2 框架 In my samplePrj properties文件 其中 com samplePrj Successmessage Saved Successful 是一个属性 我需要在我的 JSP 页
  • (在 Ubuntu 服务器上使用“imgkit”)wkhtmltopdf:无法连接到任何 X 显示器

    我有一个在远程 Ubuntu 服务器上运行的 python 脚本 在我的代码中的某个时刻 我创建了一个 HTML 文件 然后将其转换为 png 因此 我选择使用 imgkit 它能很好地完成这项工作 在将 python 脚本上传到远程 Ub
  • 如何获取已安装应用程序的大小?

    我正在尝试计算已安装应用程序的大小 我找到了答案here 我在一些设备上测试过 除了三星Galaxy Note3 4 3 之外 没有任何问题 我收到此错误 java lang NoSuchMethodException getPackage
  • 如何保护Android应用程序免遭盗窃和修改应用程序本身?

    这里有一篇关于最近在 Android 市场上发生的骗局的精彩报道 http www theregister co uk 2011 12 12 android market malware 有人从 root 手机上复制了一些流行的游戏 apk
  • git rebase:重新调整的提交仍在索引中吗?

    当我读到git 变基 我明白重新定位的提交应该丢失 我说应该是因为我注意到 知道重新调整的提交 sha 我可以回忆起来 假设我有以下三个提交 A gt B gt C where C的啥是cshaid 然后 如果我交互地变基fixing up
  • .loadby sos clr - 找不到指定的模块

    我试图弄清楚转储文件中的 CLR 异常是什么 但在尝试执行时遇到问题 0 000 gt loadby sos clr The call to LoadLibrary C ProgramData dbg sym clr dll 5348A1E
  • iOS4 实现 -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]?

    我该如何实施 NSURLConnection sendAsynchronousRequest queue completionHandler 对于 iOS if IPHONE OS VERSION MIN REQUIRED lt IPHON