[英]Can I have a NSURLSession delegate method in a different ViewController in iOS7?
I am using iOS7 and Xcode 5. 我正在使用iOS7和Xcode 5。
I have instantiated a NSURLSession object with a URL in one ViewController and then I want to write its delegate in a different ViewController. 我在一个ViewController中实例化了一个带有URL的NSURLSession对象,然后我想在另一个ViewController中编写其委托。 Is it possible?
可能吗?
Basically, I want to make the call in one ViewController, and immediately after making the call I transit to the other ViewController. 基本上,我想在一个ViewController中进行调用,并且在调用之后立即转到另一个ViewController。 Once the transition is made, I want to receive the response in the second ViewController through the NSURLSession delegate methods and then display the data.
转换完成后,我想通过NSURLSession委托方法在第二个ViewController中接收响应,然后显示数据。 Is this possible?
这可能吗?
Note: I don't want to keep the user waiting in the first ViewController until the server comes back with its response, because while the server is processing the data, I can keep the user engaged in the second ViewController with other images/elements displayed. 注意:在服务器返回响应之前,我不想让用户在第一个ViewController中等待,因为在服务器处理数据时,我可以让用户在第二个ViewController中与其他图像/元素一起显示。
Your Api.h 您的Api.h
#import <Foundation/Foundation.h>
typedef void(^successTypeBlock)(NSData*);
typedef void(^errorTypeBlock)(NSError*);
@interface TestApi : NSObject<NSURLConnectionDelegate>
-(void)hitApiWithURL:(NSURL*)url successBlock:(successTypeBlock)success failureBlock:(errorTypeBlock)failure;
@end
your Api.m 您的Api.m
@interface TestApi()
{
}
@property(nonatomic,copy) errorTypeBlock errorBlock_;
@property(nonatomic,copy) successTypeBlock successBlock_;
@end
@implementation TestApi
-(void)hitApiWithURL:(NSURL*)url successBlock:(successTypeBlock)success failureBlock:(errorTypeBlock)failure{
errorBlock_=failure;
successBlock_=success;
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
successBlock_(data);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
errorBlock_(error);
}
@end
Now 现在
[<yourApiObj> hitApiWithURL:<your URL> successBlock:^(NSData* data){
//get success value here after server responce
} failureBlock:^(NSError* error){
//get error if any
}];
just gave you a basic skeleton improve and innovate as per your needs. 只是为您提供了根据您的需求进行改进和创新的基本框架。
Rather than doing it on the main thread I think you need use threading concept so that, you can accomplish this using the secondary thread. 我认为您无需在主线程上执行此操作,而是需要使用线程概念,以便可以使用辅助线程来完成此操作。 And even the UI will not get blocked.
甚至UI也不会被阻止。
Write a class and subclass it with NSOperation. 编写一个类,并使用NSOperation对其进行子类化。
refer this 参考这个
Apple's Sample code : https://developer.apple.com/library/mac/samplecode/NSOperationSample/Introduction/Intro.html#//apple_ref/doc/uid/DTS10004184 苹果的示例代码: https : //developer.apple.com/library/mac/samplecode/NSOperationSample/Introduction/Intro.html#//apple_ref/doc/uid/DTS10004184
Refer this wonderfull tutorial. 请参阅此精彩教程。 http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.