這就是我結束的!這對未來通過這些人來說應該是有用的。
#import <Foundation/Foundation.h>
@protocol LoadJsonDelegate <NSObject, NSURLConnectionDelegate>
@optional
- (void) downloadFinished;
- (void) downloadReceivedData;
- (void) dataDownloadFailed: (NSString *) reason;
@end
@interface LoadURLJson : NSObject
{
NSMutableData *receivedData;
int expectedLength;
}
@property (nonatomic, strong) NSMutableData *receivedData;
@property (strong) NSString *urlString;
@property (weak) id <LoadJsonDelegate> delegate;
-(void)start;
-(void)cancel;
+ (id)download:(NSString *)aURLString withDelegate:(id <LoadJsonDelegate>)aDelegate;
@end
的.M
#import "LoadURLJson.h"
#define SAFE_PERFORM_WITH_ARG(THE_OBJECT, THE_SELECTOR, THE_ARG) (([THE_OBJECT respondsToSelector:THE_SELECTOR]) ? [THE_OBJECT performSelector:THE_SELECTOR withObject:THE_ARG] : nil)
@implementation LoadURLJson
@synthesize receivedData, delegate, urlString;
+ (id) download:(NSString *)aURLString withDelegate:(id <LoadJsonDelegate>)aDelegate
{
if (!aURLString)
{
NSLog(@"Error. No URL string");
return nil;
}
LoadURLJson *loadJson = [[self alloc] init];
loadJson.urlString = aURLString;
loadJson.delegate = aDelegate;
[loadJson start];
return loadJson;
}
-(void)start
{
receivedData = [NSMutableData data];
NSURL *url = [NSURL URLWithString:urlString];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url]delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
// Check for bad connection
expectedLength = [response expectedContentLength];
if (expectedLength == NSURLResponseUnknownLength)
{
NSString *reason = [NSString stringWithFormat:@"Invalid URL [%@]", urlString];
SAFE_PERFORM_WITH_ARG(delegate, @selector(dataDownloadFailed:), reason);
[connection cancel];
[self cleanup];
return;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
SAFE_PERFORM_WITH_ARG(delegate, @selector(downloadReceivedData), nil);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
SAFE_PERFORM_WITH_ARG(delegate, @selector(downloadFinished), nil);
}
-(void)cleanup
{
self.urlString = nil;
}
-(void)dealloc
{
[self cleanup];
}
-(void)cancel
{
[self cleanup];
}
@end
使用率 - 在yourView.h
#import "LoadURLJson.h"
@interface ViewController : UIViewController <LoadJsonDelegate>
{
LoadURLJson *loadJson;
}
youView.m
隨着電話:
loadJson = [LoadURLJson download:@"url" withDelegate:self];
然後實現
-(void)downloadFinished
{
NSData *data = [[NSData alloc] initWithData:loadJson.receivedData];
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",dictionary);
}
基於關閉DownloadHelper這裏:https://github.com/erica/iOS-5-Cookbook
BSD許可證。
來源
2012-06-16 06:14:11
KDM
你想要的是在異步API(特別是'[NSURLConnection start]')之上的同步操作。沒有一些沉重的線程雜耍,你無法做到這一點,無論如何這是一個壞主意 - 異步是真的要走。不要把異步性扔掉。 –
我知道你是對的我一直在自己辯論 - 所以你認爲我應該去協議路由,並使所有需要它的類符合協議? – KDM