Skip to content
Browse files

Unregister from notifications on dealloc. Handle case where there is …

…a dangling notification observer still registered when a user cancells the sign-in
  • Loading branch information...
1 parent 6c9d8ae commit c0c687c9c38bb5cc31970afae5ebd66d19197e3a @eriksundin eriksundin committed Mar 6, 2013
Showing with 41 additions and 18 deletions.
  1. +41 −18 AFOAuth1Client/AFOAuth1Client.m
View
59 AFOAuth1Client/AFOAuth1Client.m
@@ -137,6 +137,9 @@ static inline BOOL AFQueryStringValueIsTrue(NSString *value) {
@interface AFOAuth1Client ()
@property (readwrite, nonatomic, copy) NSString *key;
@property (readwrite, nonatomic, copy) NSString *secret;
+@property (readwrite, nonatomic, strong) id applicationLaunchNotificationObserver;
+
+-(void)unregisterFromApplicationLaunchNotifications;
- (NSDictionary *)OAuthParameters;
- (NSString *)OAuthSignatureForMethod:(NSString *)method
@@ -177,6 +180,18 @@ - (id)initWithBaseURL:(NSURL *)url
return self;
}
+- (void)dealloc
+{
+ [self unregisterFromApplicationLaunchNotifications];
+}
+
+-(void)unregisterFromApplicationLaunchNotifications {
+ if (self.applicationLaunchNotificationObserver) {
+ [[NSNotificationCenter defaultCenter] removeObserver:self.applicationLaunchNotificationObserver];
+ self.applicationLaunchNotificationObserver = nil;
+ }
+}
+
- (NSDictionary *)OAuthParameters {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:kAFOAuth1Version forKey:@"oauth_version"];
@@ -257,31 +272,39 @@ - (void)authorizeUsingOAuthWithRequestTokenPath:(NSString *)requestTokenPath
{
[self acquireOAuthRequestTokenWithPath:requestTokenPath callback:callbackURL accessMethod:(NSString *)accessMethod success:^(AFOAuth1Token *requestToken) {
__block AFOAuth1Token *currentRequestToken = requestToken;
-
- __block id applicationLaunchNotificationObserver = [[NSNotificationCenter defaultCenter]
- addObserverForName:kAFApplicationLaunchedWithURLNotification
- object:nil
- queue:[NSOperationQueue mainQueue]
- usingBlock:^(NSNotification *notification) {
-
- [[NSNotificationCenter defaultCenter] removeObserver:applicationLaunchNotificationObserver];
- NSURL *url = [[notification userInfo] valueForKey:kAFApplicationLaunchOptionsURLKey];
-
- currentRequestToken.verifier = [AFParametersFromQueryString([url query]) valueForKey:@"oauth_verifier"];
-
- [self acquireOAuthAccessTokenWithPath:accessTokenPath requestToken:currentRequestToken accessMethod:accessMethod success:^(AFOAuth1Token * accessToken) {
+
+ // Make sure not to register multiple notification observers.
+ // Handles the case where sign-in was cancelled by the user while in the external browser.
+ if (self.applicationLaunchNotificationObserver) {
+ [self unregisterFromApplicationLaunchNotifications];
+ }
+
+ self.applicationLaunchNotificationObserver = [[NSNotificationCenter defaultCenter]
+ addObserverForName:kAFApplicationLaunchedWithURLNotification
+ object:nil
+ queue:[NSOperationQueue mainQueue]
+ usingBlock:^(NSNotification *notification) {
+
+ // Unregister from further notifications.
+ [self unregisterFromApplicationLaunchNotifications];
+
+ NSURL *url = [[notification userInfo] valueForKey:kAFApplicationLaunchOptionsURLKey];
+
+ currentRequestToken.verifier = [AFParametersFromQueryString([url query]) valueForKey:@"oauth_verifier"];
+
+ [self acquireOAuthAccessTokenWithPath:accessTokenPath requestToken:currentRequestToken accessMethod:accessMethod success:^(AFOAuth1Token * accessToken) {
self.accessToken = accessToken;
if (success) {
- success(accessToken);
+ success(accessToken);
}
- } failure:^(NSError *error) {
+ } failure:^(NSError *error) {
if (failure) {
- failure(error);
+ failure(error);
}
- }];
}];
-
+ }];
+
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:requestToken.key forKey:@"oauth_token"];
#if __IPHONE_OS_VERSION_MIN_REQUIRED

0 comments on commit c0c687c

Please sign in to comment.
Something went wrong with that request. Please try again.