Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Unregister from application launch notifications #29

Merged
merged 4 commits into from

4 participants

@eriksundin

I ran into problems when using the AFOAuth1Client in a process where there is log-out functionality in the application and the authentication process needs to be restarted.
The client never unregisters from the kAFApplicationLaunchedWithURLNotification notification, getting the result of multiple observation blocks getting executed.

Added functionality

  • unregisters from the notification inside the notification block.
  • handle the case where the user cancels an auth flow and returns to the app: -- unregister any previous registered notification block before registering a new one -- unregister in dealloc to fullfil the contract to addObserverForName:object:queue:usingBlock.
@yelled3

looks great - I was just about to write it :-)
merge?

@yelled3 yelled3 referenced this pull request in OldPodsHome/SHAccountManager
Closed

Unregister from application launch notifications #1

@seivan

:+1:
Nicely done.

@seivan

The method name should have an indicator that it's about observers. As far as we're concerned, there is no such thing as registerToApplicationLaunchNotifications
The private property is called applicationLaunchNotificationObserver.

Also I think starting bracers should be on a new line :) (Like the rest of the application).
Look at your dealloc.
I personally prefer this style (I even add semicolons at the end of a selector name like the interface).

@eriksundin

Good points @seivan. The naming of the unregister method is not really clear, I used removeApplicationLaunchNotificationObserver instead to clearly reference the observer.

Also fixed the braces :-)

@mattt mattt merged commit aa789ce into AFNetworking:master
@mattt

Merged! Thanks, @eriksundin! Tagging a new release now. Really happy to have this in for 0.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 6, 2013
  1. @eriksundin
  2. @eriksundin

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

    eriksundin authored
    …a dangling notification observer still registered when a user cancells the sign-in
Commits on Mar 11, 2013
  1. @eriksundin
Commits on Mar 27, 2013
  1. @eriksundin

    Renamed unregisterFromApplicationLaunchNotifications to removeApplica…

    eriksundin authored
    …tionLaunchNotificationObserver
This page is out of date. Refresh to see the latest.
Showing with 50 additions and 18 deletions.
  1. +50 −18 AFOAuth1Client/AFOAuth1Client.m
View
68 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)removeApplicationLaunchNotificationObserver;
- (NSDictionary *)OAuthParameters;
- (NSString *)OAuthSignatureForMethod:(NSString *)method
@@ -177,6 +180,19 @@ - (id)initWithBaseURL:(NSURL *)url
return self;
}
+- (void)dealloc
+{
+ [self removeApplicationLaunchNotificationObserver];
+}
+
+-(void)removeApplicationLaunchNotificationObserver
+{
+ if (self.applicationLaunchNotificationObserver) {
+ [[NSNotificationCenter defaultCenter] removeObserver:self.applicationLaunchNotificationObserver];
+ self.applicationLaunchNotificationObserver = nil;
+ }
+}
+
- (NSDictionary *)OAuthParameters {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:kAFOAuth1Version forKey:@"oauth_version"];
@@ -257,24 +273,40 @@ - (void)authorizeUsingOAuthWithRequestTokenPath:(NSString *)requestTokenPath
{
[self acquireOAuthRequestTokenWithPath:requestTokenPath callback:callbackURL accessMethod:(NSString *)accessMethod success:^(AFOAuth1Token *requestToken) {
__block AFOAuth1Token *currentRequestToken = requestToken;
- [[NSNotificationCenter defaultCenter] addObserverForName:kAFApplicationLaunchedWithURLNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
- 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);
- }
- } failure:^(NSError *error) {
- if (failure) {
- failure(error);
- }
- }];
- }];
-
+
+ // 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 removeApplicationLaunchNotificationObserver];
+ }
+
+ self.applicationLaunchNotificationObserver = [[NSNotificationCenter defaultCenter]
+ addObserverForName:kAFApplicationLaunchedWithURLNotification
+ object:nil
+ queue:[NSOperationQueue mainQueue]
+ usingBlock:^(NSNotification *notification) {
+
+ 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);
+ }
+ } failure:^(NSError *error) {
+ if (failure) {
+ failure(error);
+ }
+ }];
+
+ // Unregister from further notifications.
+ [self removeApplicationLaunchNotificationObserver];
+
+ }];
+
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:requestToken.key forKey:@"oauth_token"];
#if __IPHONE_OS_VERSION_MIN_REQUIRED
Something went wrong with that request. Please try again.