Permalink
Browse files

Added a redirects resolving category on NSURL. It has even blocks sup…

…port! (Turn on via kL0MuiKitSupportsBlocks = 1 on compile -- requires a blocks-capable runtime, a blocks-capable compiler like Plausible Blocks, and there's no direct support in the project to turn it on or off on demand).
  • Loading branch information...
1 parent 8533236 commit d1327232d1f9a15d1fd33f1742831d46e0f479cc @millenomi committed Jul 17, 2009
Showing with 166 additions and 3 deletions.
  1. +1 −0 MuiKit.h
  2. +13 −3 MuiKit.xcodeproj/project.pbxproj
  3. +23 −0 NSURL+L0URLResolvingRedirects.h
  4. +129 −0 NSURL+L0URLResolvingRedirects.m
View
@@ -14,6 +14,7 @@
#import "UIDevice+L0ModelDetection.h"
#import "L0ActionSheet.h"
#import "NSURL+L0URLParsing.h"
+#import "NSURL+L0URLResolvingRedirects.h"
#import "L0KVODictionaryAdditions.h"
#import "UIApplication+L0NetworkIndicator.h"
#import "L0KVODispatcher.h"
@@ -67,6 +67,8 @@
15B929540F90726A001E40B7 /* UIDevice+L0ModelDetection.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 15B929520F90726A001E40B7 /* UIDevice+L0ModelDetection.h */; };
15B929560F90726A001E40B7 /* UIDevice+L0ModelDetection.h in Headers */ = {isa = PBXBuildFile; fileRef = 15B929520F90726A001E40B7 /* UIDevice+L0ModelDetection.h */; };
15B929570F90726A001E40B7 /* UIDevice+L0ModelDetection.m in Sources */ = {isa = PBXBuildFile; fileRef = 15B929530F90726A001E40B7 /* UIDevice+L0ModelDetection.m */; };
+ 15C0401410110ECB0090B009 /* NSURL+L0URLResolvingRedirects.m in Sources */ = {isa = PBXBuildFile; fileRef = 15C0401210110ECB0090B009 /* NSURL+L0URLResolvingRedirects.m */; };
+ 15C04047101114A30090B009 /* NSURL+L0URLResolvingRedirects.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 15C0401110110ECB0090B009 /* NSURL+L0URLResolvingRedirects.h */; };
15C0C1680FDFD03B005A75B6 /* L0KVODictionaryAdditions.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 15C0C1540FDFCF22005A75B6 /* L0KVODictionaryAdditions.h */; };
15FD32410FB9600000EB8ED5 /* L0ActionSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = 15FD323F0FB9600000EB8ED5 /* L0ActionSheet.m */; };
15FD32420FB9600400EB8ED5 /* L0ActionSheet.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 15FD323E0FB9600000EB8ED5 /* L0ActionSheet.h */; };
@@ -117,6 +119,7 @@
15C0C1680FDFD03B005A75B6 /* L0KVODictionaryAdditions.h in CopyFiles */,
153B7FE40FF2054D00DDFD16 /* UIApplication+L0NetworkIndicator.h in CopyFiles */,
1518EF581010B6A5009C334E /* L0KVODispatcher.h in CopyFiles */,
+ 15C04047101114A30090B009 /* NSURL+L0URLResolvingRedirects.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -155,6 +158,8 @@
15B446660F79028E00DA63E9 /* MuiKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MuiKit.h; sourceTree = "<group>"; };
15B929520F90726A001E40B7 /* UIDevice+L0ModelDetection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIDevice+L0ModelDetection.h"; sourceTree = "<group>"; };
15B929530F90726A001E40B7 /* UIDevice+L0ModelDetection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+L0ModelDetection.m"; sourceTree = "<group>"; };
+ 15C0401110110ECB0090B009 /* NSURL+L0URLResolvingRedirects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURL+L0URLResolvingRedirects.h"; sourceTree = "<group>"; };
+ 15C0401210110ECB0090B009 /* NSURL+L0URLResolvingRedirects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+L0URLResolvingRedirects.m"; sourceTree = "<group>"; };
15C0C1540FDFCF22005A75B6 /* L0KVODictionaryAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = L0KVODictionaryAdditions.h; sourceTree = "<group>"; };
15FD323E0FB9600000EB8ED5 /* L0ActionSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = L0ActionSheet.h; sourceTree = "<group>"; };
15FD323F0FB9600000EB8ED5 /* L0ActionSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = L0ActionSheet.m; sourceTree = "<group>"; };
@@ -203,7 +208,7 @@
isa = PBXGroup;
children = (
15C0C1530FDFCEF1005A75B6 /* KVO Additions */,
- 15AF56390FBAAE9A00BE7F71 /* URL Decoding */,
+ 15AF56390FBAAE9A00BE7F71 /* URL Handling */,
15FD32440FB9600A00EB8ED5 /* Action Sheet with Identifiers */,
15B929510F907255001E40B7 /* UIDevice Model Additions */,
157891700F7CE909006FF0C0 /* Flip View Controller */,
@@ -293,13 +298,15 @@
name = "Flip View Controller";
sourceTree = "<group>";
};
- 15AF56390FBAAE9A00BE7F71 /* URL Decoding */ = {
+ 15AF56390FBAAE9A00BE7F71 /* URL Handling */ = {
isa = PBXGroup;
children = (
15AF56330FBAAE9200BE7F71 /* NSURL+L0URLParsing.h */,
15AF56340FBAAE9200BE7F71 /* NSURL+L0URLParsing.m */,
+ 15C0401110110ECB0090B009 /* NSURL+L0URLResolvingRedirects.h */,
+ 15C0401210110ECB0090B009 /* NSURL+L0URLResolvingRedirects.m */,
);
- name = "URL Decoding";
+ name = "URL Handling";
sourceTree = "<group>";
};
15B4459F0F78FAD200DA63E9 /* SDK */ = {
@@ -451,6 +458,7 @@
15AF56360FBAAE9200BE7F71 /* NSURL+L0URLParsing.m in Sources */,
153B7FE20FF2049300DDFD16 /* UIApplication+L0NetworkIndicator.m in Sources */,
1518EF571010B69D009C334E /* L0KVODispatcher.m in Sources */,
+ 15C0401410110ECB0090B009 /* NSURL+L0URLResolvingRedirects.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -494,6 +502,7 @@
CONFIGURATION_BUILD_DIR = "$(BUILD_DIR)/$(CONFIGURATION)";
CONFIGURATION_TEMP_DIR = "$(PROJECT_TEMP_DIR)/$(CONFIGURATION)";
GCC_PREFIX_HEADER = "$(INFINITELABS_TOOLS)/Common/iPhone/UIKit.pch";
+ SDKROOT = iphoneos2.2;
};
name = Debug;
};
@@ -504,6 +513,7 @@
CONFIGURATION_BUILD_DIR = "$(BUILD_DIR)/$(CONFIGURATION)";
CONFIGURATION_TEMP_DIR = "$(PROJECT_TEMP_DIR)/$(CONFIGURATION)";
GCC_PREFIX_HEADER = "$(INFINITELABS_TOOLS)/Common/iPhone/UIKit.pch";
+ SDKROOT = iphoneos2.2;
};
name = Release;
};
@@ -0,0 +1,23 @@
+//
+// NSURL+L0URLResolvingRedirects.h
+// MuiKit
+//
+// Created by ∞ on 17/07/09.
+// Copyright 2009 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#if kL0MuiKitAllowBlocks
+typedef void (^L0URLResolvingDidEndBlock)(NSURL*);
+#endif
+
+@interface NSURL (L0URLResolvingRedirects)
+
+- (void) beginResolvingRedirectsWithDelegate:(id) delegate selector:(SEL) selector;
+
+#if kL0MuiKitAllowBlocks
+- (void) beginResolvingRedirectsAndInvoke:(L0URLResolvingDidEndBlock) block;
+#endif
+
+@end
@@ -0,0 +1,129 @@
+//
+// NSURL+L0URLResolvingRedirects.m
+// MuiKit
+//
+// Created by ∞ on 17/07/09.
+// Copyright 2009 __MyCompanyName__. All rights reserved.
+//
+
+#import "NSURL+L0URLResolvingRedirects.h"
+
+@interface L0URLRedirectsResolver : NSObject {
+ NSURLConnection* connection;
+ id delegate;
+ SEL selector;
+#if kL0MuiKitAllowBlocks
+ L0URLResolvingDidEndBlock block;
+#endif
+
+ NSURL* lastSeenURL;
+}
+
+- (id) initWithURL:(NSURL*) url delegate:(id) delegate selector:(SEL) selector;
+#if kL0MuiKitAllowBlocks
+- (id) initWithURL:(NSURL*) url didEndBlock:(L0URLResolvingDidEndBlock) block;
+#endif
+
+- (void) startWithURL:(NSURL*) u;
+- (void) finish;
+
+@property(copy) NSURL* lastSeenURL;
+
+@end
+
+
+// ------------------
+
+@implementation NSURL (L0URLResolvingRedirects)
+
+- (void) beginResolvingRedirectsWithDelegate:(id) delegate selector:(SEL) selector;
+{
+ [[L0URLRedirectsResolver alloc] initWithURL:self delegate:delegate selector:selector];
+}
+
+#if kL0MuiKitAllowBlocks
+- (void) beginResolvingRedirectsAndInvoke:(L0URLResolvingDidEndBlock) block;
+{
+ [[L0URLRedirectsResolver alloc] initWithURL:self didEndBlock:block];
+}
+#endif
+
+@end
+
+@implementation L0URLRedirectsResolver
+
+- (id) initWithURL:(NSURL*) url delegate:(id) d selector:(SEL) s;
+{
+ if (self = [super init]) {
+ delegate = d;
+ selector = s;
+ [self startWithURL:url];
+ }
+
+ return self;
+}
+
+#if kL0MuiKitAllowBlocks
+- (id) initWithURL:(NSURL*) url didEndBlock:(L0URLResolvingDidEndBlock) b;
+{
+ if (self = [super init]) {
+ block = [b copy];
+ [self startWithURL:url];
+ }
+
+ return self;
+}
+#endif
+
+- (void) startWithURL:(NSURL*) u;
+{
+ self.lastSeenURL = u;
+ connection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:u] delegate:self];
+ [self retain]; // balanced in -finish
+}
+
+- (NSURLRequest*) connection:(NSURLConnection*) c willSendRequest:(NSURLRequest*) request redirectResponse:(NSURLResponse*) response;
+{
+ self.lastSeenURL = [response URL];
+ return request;
+}
+
+- (void) connectionDidFinishLoading:(NSURLConnection*) c;
+{
+ [self finish];
+}
+
+- (void) connection:(NSURLConnection*) c didFailWithError:(NSError*) e;
+{
+ self.lastSeenURL = nil;
+ [self finish];
+}
+
+- (void) finish;
+{
+ if (delegate && selector)
+ [delegate performSelector:selector withObject:self.lastSeenURL];
+
+#if kL0MuiKitAllowBlocks
+ block(self.lastSeenURL);
+#endif
+
+ [connection release];
+ connection = nil;
+
+ [self autorelease]; // balances the retain in -startWithURL:
+}
+
+@synthesize lastSeenURL;
+
+- (void) dealloc;
+{
+ [connection release];
+ self.lastSeenURL = nil;
+#if kL0MuiKitAllowBlocks
+ [block release];
+#endif
+ [super dealloc];
+}
+
+@end

0 comments on commit d132723

Please sign in to comment.