Skip to content

Commit

Permalink
Modernized FBSDKLoginKit
Browse files Browse the repository at this point in the history
Summary:
This diff modernizes the `FBSDKLoginKit` to support modern Objective-C syntax to increase type safety with both ObjC and Swift.

**General Changes**

* Removed Deprecated Methods, Classes, Properties
* Typedefs for all Objective-C blocks
* Used `instancetype` for inits
* Updated Xcode Projects/Schemes via. "Validate Project" option in Xcode
* Made `init`/`new` unavailable where/when possible
* Confirm `NSError **` throws on Swift
* Used `NS_SWIFT_UNAVAILABLE` where makes sense
* Ensured `NS_DESIGNATED_INITIALIZER` is used
* Added `NS_TYPED_EXTENSIBLE_ENUM` where made sense

**Updated Properties**

* Changed getter methods to `readonly` properties
* Changed getter/setter methods to `readwrite` properties
* Changed singletons to class properties
* Used dot notation for access to properties
* Added `getter=` for certain properties (e.g. `BOOL`)

**Added Nullability Annotations**

* Used `NS_ASSUME_NONNULL_BEGIN` & `NS_ASSUME_NONNULL_END` for files
* Used `_Nullable` for typedef blocks
* Used `nullable` & `null_resettable`
* Collections/Dictionaries `nonnull` when at all possible

**Added Generics**

* `NSArray` & `NSMutableArray`
* `NSSet` & `NSMutableSet`
* `NSDictionary` & `NSMutableDictionary`

**Added `NS_SWIFT_NAME`**

* Removed prefixes in favor of Swift namespaces
  * `FB` prefix for UI elements
* Added Dot notation where makes sense
* Swift init for class method creation methods

Reviewed By: jingping2015

Differential Revision: D9883550

fbshipit-source-id: 2fb40d44b66b033bb998bf9ae57d71a0c9d339d7
  • Loading branch information
Cody Winton authored and facebook-github-bot committed Jan 19, 2019
1 parent 9b682bb commit 5eae2d7
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 123 deletions.
1 change: 1 addition & 0 deletions FBSDKLoginKit/FBSDKLoginKit/FBSDKDeviceLoginCodeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
@abstract Describes the initial response when starting the device login flow.
@discussion This is used by `FBSDKDeviceLoginManager`.
*/
NS_SWIFT_NAME(DeviceLoginCodeInfo)
@interface FBSDKDeviceLoginCodeInfo : NSObject

/*!
Expand Down
2 changes: 1 addition & 1 deletion FBSDKLoginKit/FBSDKLoginKit/FBSDKDeviceLoginCodeInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@implementation FBSDKDeviceLoginCodeInfo

- (instancetype) initWithIdentifier:(NSString *)identifier
- (instancetype)initWithIdentifier:(NSString *)identifier
loginCode:(NSString *)loginCode
verificationURL:(NSURL *)verificationURL
expirationDate:(NSDate *)expirationDate
Expand Down
9 changes: 6 additions & 3 deletions FBSDKLoginKit/FBSDKLoginKit/FBSDKDeviceLoginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
/*!
@abstract A delegate for `FBSDKDeviceLoginManager`.
*/
NS_SWIFT_NAME(DeviceLoginManagerDelegate)
@protocol FBSDKDeviceLoginManagerDelegate <NSObject>

/*!
Expand All @@ -36,7 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
@param loginManager the login manager instance.
@param codeInfo the code info data.
*/
- (void)deviceLoginManager:(FBSDKDeviceLoginManager *)loginManager startedWithCodeInfo:(FBSDKDeviceLoginCodeInfo *)codeInfo;
- (void)deviceLoginManager:(FBSDKDeviceLoginManager *)loginManager
startedWithCodeInfo:(FBSDKDeviceLoginCodeInfo *)codeInfo;

/*!
@abstract Indicates the device login flow has finished.
Expand All @@ -60,13 +62,14 @@ NS_ASSUME_NONNULL_BEGIN
See [Facebook Device Login](https://developers.facebook.com/docs/facebook-login/for-devices).
*/
NS_SWIFT_NAME(DeviceLoginManager)
@interface FBSDKDeviceLoginManager : NSObject <NSNetServiceDelegate>

/*!
@abstract Initializes a new instance.
@param permissions permissions to request.
*/
- (instancetype)initWithPermissions:(nullable NSArray<NSString *> *)permissions
- (instancetype)initWithPermissions:(NSArray<NSString *> *)permissions
enableSmartLogin:(BOOL)enableSmartLogin
NS_DESIGNATED_INITIALIZER;

Expand All @@ -81,7 +84,7 @@ NS_DESIGNATED_INITIALIZER;
/*!
@abstract the requested permissions.
*/
@property (nullable, nonatomic, copy, readonly) NSArray<NSString *> *permissions;
@property (nonatomic, copy, readonly) NSArray<NSString *> *permissions;

/*!
@abstract the optional URL to redirect the user to after they complete the login.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
@abstract Represents the results of the a device login flow.
@discussion This is used by `FBSDKDeviceLoginManager`.
*/
NS_SWIFT_NAME(DeviceLoginManagerResult)
@interface FBSDKDeviceLoginManagerResult : NSObject

/*!
Expand Down
17 changes: 11 additions & 6 deletions FBSDKLoginKit/FBSDKLoginKit/FBSDKLoginButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#import "FBSDKTooltipView.h"

NS_ASSUME_NONNULL_BEGIN

@protocol FBSDKLoginButtonDelegate;

/**
Expand All @@ -40,7 +42,7 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginButtonTooltipBehavior)
/** Force disable. In this case you can still exert more refined
control by manually constructing a `FBSDKLoginTooltipView` instance. */
FBSDKLoginButtonTooltipBehaviorDisable = 2
};
} NS_SWIFT_NAME(FBLoginButton.TooltipBehavior);

/**
A button that initiates a log in or log out flow upon tapping.
Expand All @@ -55,6 +57,7 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginButtonTooltipBehavior)
`FBSDKLoginButton` has a fixed height of @c 30 pixels, but you may change the width. `initWithFrame:CGRectZero`
will size the button to its minimum frame.
*/
NS_SWIFT_NAME(FBLoginButton)
@interface FBSDKLoginButton : FBSDKButton

/**
Expand All @@ -72,20 +75,19 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginButtonTooltipBehavior)
/**
The publish permissions to request.
Use `defaultAudience` to specify the default audience to publish to.
Note this is converted to NSSet and is only
an NSArray for the convenience of literal syntax.
*/
@property (copy, nonatomic) NSArray *publishPermissions;
@property (copy, nonatomic) NSArray<NSString *> *publishPermissions;
/**
The read permissions to request.
Note, that if read permissions are specified, then publish permissions should not be specified. This is converted to NSSet and is only
an NSArray for the convenience of literal syntax.
*/
@property (copy, nonatomic) NSArray *readPermissions;
@property (copy, nonatomic) NSArray<NSString *> *readPermissions;
/**
Gets or sets the desired tooltip behavior.
*/
Expand All @@ -101,6 +103,7 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginButtonTooltipBehavior)
@protocol
A delegate for `FBSDKLoginButton`
*/
NS_SWIFT_NAME(LoginButtonDelegate)
@protocol FBSDKLoginButtonDelegate <NSObject>

@required
Expand All @@ -111,8 +114,8 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginButtonTooltipBehavior)
@param error The error (if any) from the login
*/
- (void)loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
error:(NSError *)error;
didCompleteWithResult:(nullable FBSDKLoginManagerLoginResult *)result
error:(nullable NSError *)error;

/**
Sent to the delegate when the button was used to logout.
Expand All @@ -129,3 +132,5 @@ didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
- (BOOL)loginButtonWillLogin:(FBSDKLoginButton *)loginButton;

@end

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion FBSDKLoginKit/FBSDKLoginKit/FBSDKLoginButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ - (void)_buttonPressed:(id)sender
}
}

FBSDKLoginManagerRequestTokenHandler handler = ^(FBSDKLoginManagerLoginResult *result, NSError *error) {
FBSDKLoginManagerLoginResultBlock handler = ^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if ([self.delegate respondsToSelector:@selector(loginButton:didCompleteWithResult:error:)]) {
[self.delegate loginButton:self didCompleteWithResult:result error:error];
}
Expand Down
44 changes: 15 additions & 29 deletions FBSDKLoginKit/FBSDKLoginKit/FBSDKLoginConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

/**
The error domain for all errors from FBSDKLoginKit
Error codes from the SDK in the range 300-399 are reserved for this domain.
*/
FOUNDATION_EXPORT NSErrorDomain const FBSDKLoginErrorDomain;
FOUNDATION_EXPORT NSErrorDomain const FBSDKLoginErrorDomain
NS_SWIFT_NAME(LoginErrorDomain);

#else

Expand All @@ -34,7 +37,8 @@ FOUNDATION_EXPORT NSErrorDomain const FBSDKLoginErrorDomain;
Error codes from the SDK in the range 300-399 are reserved for this domain.
*/
FOUNDATION_EXPORT NSString *const FBSDKLoginErrorDomain;
FOUNDATION_EXPORT NSString *const FBSDKLoginErrorDomain
NS_SWIFT_NAME(LoginErrorDomain);

#endif

Expand All @@ -54,6 +58,7 @@ typedef NS_ERROR_ENUM(FBSDKLoginErrorDomain, FBSDKLoginError)
Reserved.
*/
FBSDKLoginErrorReserved = 300,

/**
The error code for unknown errors.
*/
Expand All @@ -63,14 +68,17 @@ typedef NS_ERROR_ENUM(FBSDKLoginErrorDomain, FBSDKLoginError)
The user's password has changed and must log in again
*/
FBSDKLoginErrorPasswordChanged,

/**
The user must log in to their account on www.facebook.com to restore access
*/
FBSDKLoginErrorUserCheckpointed,

/**
Indicates a failure to request new permissions because the user has changed.
*/
FBSDKLoginErrorUserMismatch,

/**
The user must confirm their account with Facebook before logging in
*/
Expand All @@ -82,15 +90,17 @@ typedef NS_ERROR_ENUM(FBSDKLoginErrorDomain, FBSDKLoginError)
been disabled.
*/
FBSDKLoginErrorSystemAccountAppDisabled,

/**
An error occurred related to Facebook system Account store
*/
FBSDKLoginErrorSystemAccountUnavailable,

/**
The login response was missing a valid challenge string.
*/
FBSDKLoginErrorBadChallengeString,
};
} NS_SWIFT_NAME(LoginError);

/**
FBSDKDeviceLoginError
Expand All @@ -113,30 +123,6 @@ typedef NS_ERROR_ENUM(FBSDKLoginErrorDomain, FBSDKDeviceLoginError) {
The code you entered has expired.
*/
FBSDKDeviceLoginErrorCodeExpired = 1349152
};
} NS_SWIFT_NAME(DeviceLoginError);

/**
Deprecated
*/
typedef NS_ENUM(NSInteger, FBSDKLoginErrorCode)
{
FBSDKLoginReservedErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorReserved instead") = FBSDKLoginErrorReserved,
FBSDKLoginUnknownErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorUnknown instead"),
FBSDKLoginPasswordChangedErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorPasswordChanged instead"),
FBSDKLoginUserCheckpointedErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorUserCheckpointed instead"),
FBSDKLoginUserMismatchErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorUserMismatch instead"),
FBSDKLoginUnconfirmedUserErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorUnconfirmedUser instead"),
FBSDKLoginSystemAccountAppDisabledErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorSystemAccountAppDisabled instead"),
FBSDKLoginSystemAccountUnavailableErrorCode DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorSystemAccountUnavailable instead"),
FBSDKLoginBadChallengeString DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginErrorBadChallengeString instead"),
} DEPRECATED_MSG_ATTRIBUTE("use FBSDKLoginError instead");

/**
Deprecated
*/
typedef NS_ENUM(NSUInteger, FBSDKDeviceLoginErrorSubcode) {
FBSDKDeviceLoginExcessivePollingErrorSubcode DEPRECATED_MSG_ATTRIBUTE("use FBSDKDeviceLoginErrorExcessivePolling instead") = FBSDKDeviceLoginErrorExcessivePolling,
FBSDKDeviceLoginAuthorizationDeclinedErrorSubcode DEPRECATED_MSG_ATTRIBUTE("use FBSDKDeviceLoginErrorAuthorizationDeclined instead") = FBSDKDeviceLoginErrorAuthorizationDeclined,
FBSDKDeviceLoginAuthorizationPendingErrorSubcode DEPRECATED_MSG_ATTRIBUTE("use FBSDKDeviceLoginErrorAuthorizationPending instead") = FBSDKDeviceLoginErrorAuthorizationPending,
FBSDKDeviceLoginCodeExpiredErrorSubcode DEPRECATED_MSG_ATTRIBUTE("use FBSDKDeviceLoginErrorCodeExpired instead") = FBSDKDeviceLoginErrorCodeExpired
} DEPRECATED_MSG_ATTRIBUTE("use FBSDKDeviceLoginError instead");
NS_ASSUME_NONNULL_END
57 changes: 31 additions & 26 deletions FBSDKLoginKit/FBSDKLoginKit/FBSDKLoginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@class FBSDKLoginManagerLoginResult;

/// typedef for FBSDKLoginAuthType
typedef NSString *const FBSDKLoginAuthType NS_TYPED_EXTENSIBLE_ENUM NS_SWIFT_NAME(LoginAuthType);

/// Rerequest
FOUNDATION_EXPORT FBSDKLoginAuthType FBSDKLoginAuthTypeRerequest;

/// Reauthorize
FOUNDATION_EXPORT FBSDKLoginAuthType FBSDKLoginAuthTypeReauthorize;

/**
Describes the call back to the FBSDKLoginManager
@param result the result of the authorization
@param error the authorization error, if any.
*/
typedef void (^FBSDKLoginManagerRequestTokenHandler)(FBSDKLoginManagerLoginResult *result, NSError *error);
typedef void (^FBSDKLoginManagerLoginResultBlock)(FBSDKLoginManagerLoginResult *_Nullable result,
NSError *_Nullable error)
NS_SWIFT_NAME(LoginManagerLoginResultBlock);


/**
Expand All @@ -50,7 +63,7 @@ typedef NS_ENUM(NSUInteger, FBSDKDefaultAudience)
FBSDKDefaultAudienceOnlyMe,
/** Indicates that all Facebook users are able to see posts made by the application */
FBSDKDefaultAudienceEveryone,
};
} NS_SWIFT_NAME(DefaultAudience);

/**
FBSDKLoginBehavior enum
Expand Down Expand Up @@ -94,7 +107,7 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
Platform Policy to verify your app meets the restrictions.
*/
FBSDKLoginBehaviorWeb,
};
} NS_SWIFT_NAME(LoginBehavior);

/**
`FBSDKLoginManager` provides methods for logging the user in and out.
Expand All @@ -108,12 +121,13 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
If you are managing your own token instances outside of "currentAccessToken", you will need to set
"currentAccessToken" before calling logIn* to authorize further permissions on your tokens.
*/
NS_SWIFT_NAME(LoginManager)
@interface FBSDKLoginManager : NSObject

/**
Auth type
*/
@property (strong, nonatomic) NSString *authType;
@property (strong, nonatomic) FBSDKLoginAuthType authType;
/**
the default audience.
Expand All @@ -126,20 +140,6 @@ typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
*/
@property (assign, nonatomic) FBSDKLoginBehavior loginBehavior;

/**
@warning use logInWithReadPermissions:fromViewController:handler: instead
*/
- (void)logInWithReadPermissions:(NSArray *)permissions handler:(FBSDKLoginManagerRequestTokenHandler)handler
DEPRECATED_MSG_ATTRIBUTE("use logInWithReadPermissions:fromViewController:handler: instead");

/**
@warning use logInWithPublishPermissions:fromViewController:handler: instead
*/
- (void)logInWithPublishPermissions:(NSArray *)permissions handler:(FBSDKLoginManagerRequestTokenHandler)handler
DEPRECATED_MSG_ATTRIBUTE("use logInWithPublishPermissions:fromViewController:handler: instead");

/**
Logs the user in or authorizes additional permissions.
@param permissions the optional array of permissions. Note this is converted to NSSet and is only
Expand All @@ -158,9 +158,10 @@ DEPRECATED_MSG_ATTRIBUTE("use logInWithPublishPermissions:fromViewController:han
You can only do one login call at a time. Calling a login method before the completion handler is called
on a previous login will return an error.
*/
- (void)logInWithReadPermissions:(NSArray *)permissions
fromViewController:(UIViewController *)fromViewController
handler:(FBSDKLoginManagerRequestTokenHandler)handler;
- (void)logInWithReadPermissions:(NSArray<NSString *> *)permissions
fromViewController:(nullable UIViewController *)fromViewController
handler:(nullable FBSDKLoginManagerLoginResultBlock)handler
NS_SWIFT_NAME(logIn(readPermissions:from:handler:));

/**
Logs the user in or authorizes additional permissions.
Expand All @@ -180,9 +181,10 @@ DEPRECATED_MSG_ATTRIBUTE("use logInWithPublishPermissions:fromViewController:han
You can only do one login call at a time. Calling a login method before the completion handler is called
on a previous login will return an error.
*/
- (void)logInWithPublishPermissions:(NSArray *)permissions
fromViewController:(UIViewController *)fromViewController
handler:(FBSDKLoginManagerRequestTokenHandler)handler;
- (void)logInWithPublishPermissions:(NSArray<NSString *> *)permissions
fromViewController:(nullable UIViewController *)fromViewController
handler:(nullable FBSDKLoginManagerLoginResultBlock)handler
NS_SWIFT_NAME(logIn(publishPermissions:from:handler:));


/**
Expand All @@ -197,7 +199,8 @@ DEPRECATED_MSG_ATTRIBUTE("use logInWithPublishPermissions:fromViewController:han
This method will present UI the user. You typically should call this if `[FBSDKAccessToken isDataAccessExpired]` returns true.
*/
- (void)reauthorizeDataAccess:(UIViewController *)fromViewController
handler:(FBSDKLoginManagerRequestTokenHandler)handler;
handler:(FBSDKLoginManagerLoginResultBlock)handler
NS_SWIFT_NAME(reauthorizeDataAccess(from:handler:));

/**
Logs the user out
Expand All @@ -222,6 +225,8 @@ DEPRECATED_MSG_ATTRIBUTE("use logInWithPublishPermissions:fromViewController:han
If the `[FBSDKAccessToken currentAccessToken]` was from the account store, a succesful renewal will also set
a new "currentAccessToken".
*/
+ (void)renewSystemCredentials:(void (^)(ACAccountCredentialRenewResult result, NSError *error))handler;
+ (void)renewSystemCredentials:(ACAccountStoreCredentialRenewalHandler)handler;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 5eae2d7

Please sign in to comment.