- fix facebook connection

This commit is contained in:
Giuseppe Nucifora 2016-12-28 19:42:56 +01:00
parent 0c2b3f1c25
commit cc11f1efac
3 changed files with 67 additions and 53 deletions

View File

@ -108,6 +108,7 @@
- (void)applicationDidBecomeActive:(UIApplication *)application - (void)applicationDidBecomeActive:(UIApplication *)application
{ {
[FBSDKAppEvents activateApp];
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} }
@ -116,6 +117,16 @@
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} }
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
#pragma mark - Remote Notification #pragma mark - Remote Notification
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

View File

@ -144,7 +144,16 @@
NSLog(@"response : %@",error); NSLog(@"response : %@",error);
}];*/ }];*/
[PNUser socialLoginWithBlockSuccess:^(PNUser * _Nullable responseObject) { /*[PNUser socialLoginWithBlockSuccess:^(PNUser * _Nullable responseObject) {
} failure:^(NSError * _Nonnull error) {
}];*/
[PNUser socialUserFromViewController:self blockSuccess:^(PNUser * _Nullable responseObject) {
NSLog(@"%@",[[PNUser currentUser] JSONFormObject]);
} failure:^(NSError * _Nonnull error) { } failure:^(NSError * _Nonnull error) {

View File

@ -39,10 +39,10 @@ static bool isFirstAccess = YES;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
isFirstAccess = NO; isFirstAccess = NO;
USER = [[super allocWithZone:NULL] initForCurrentUser]; USER = [[super allocWithZone:NULL] initForCurrentUser];
}); });
return USER; return USER;
} }
@ -76,26 +76,26 @@ static bool isFirstAccess = YES;
if (isFirstAccess) { if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd]; [self doesNotRecognizeSelector:_cmd];
} }
NSDictionary *savedUser = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]]; NSDictionary *savedUser = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]];
Class objectClass = NSClassFromString([[self class] PNObjClassName]); Class objectClass = NSClassFromString([[self class] PNObjClassName]);
if (savedUser) { if (savedUser) {
self = [[objectClass alloc] initWithLocalJSON:savedUser]; self = [[objectClass alloc] initWithLocalJSON:savedUser];
} }
else { else {
self = [[objectClass alloc] init]; self = [[objectClass alloc] init];
} }
if (self) { if (self) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self autoLogin]; [self autoLogin];
}); });
} }
return self; return self;
} }
@ -119,7 +119,7 @@ static bool isFirstAccess = YES;
if(self.email && [self.email isValidEmail] && self.password && [self.password isValid]){ if(self.email && [self.email isValidEmail] && self.password && [self.password isValid]){
return YES; return YES;
} }
return NO; return NO;
} }
@ -155,9 +155,9 @@ static bool isFirstAccess = YES;
[[self class] GETWithEndpointAction:@"user/profile" [[self class] GETWithEndpointAction:@"user/profile"
progress:nil progress:nil
success:^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject) { success:^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject) {
NSLogDebug(@"%@",[responseObject objectForKey:@"user"]); NSLogDebug(@"%@",[responseObject objectForKey:@"user"]);
[[[self class] currentUser] populateObjectFromJSON:[responseObject objectForKey:@"user"]]; [[[self class] currentUser] populateObjectFromJSON:[responseObject objectForKey:@"user"]];
[[[self class] currentUser] updateFacebookData]; [[[self class] currentUser] updateFacebookData];
[[[self class] currentUser] saveLocally]; [[[self class] currentUser] saveLocally];
@ -166,7 +166,7 @@ static bool isFirstAccess = YES;
if (success) { if (success) {
success([[self class] currentUser]); success([[self class] currentUser]);
} }
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLogDebug(@"%@",error); NSLogDebug(@"%@",error);
if (failure) { if (failure) {
@ -184,7 +184,7 @@ static bool isFirstAccess = YES;
- (void) registerWithBlockProgress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress - (void) registerWithBlockProgress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
Success:(nullable void (^)(PNUser * _Nullable responseObject))success Success:(nullable void (^)(PNUser * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure { failure:(nullable void (^)(NSError * _Nonnull error))failure {
[[self class] POSTWithEndpointAction:@"registration/register" parameters:[self JSONFormObject] [[self class] POSTWithEndpointAction:@"registration/register" parameters:[self JSONFormObject]
progress:uploadProgress progress:uploadProgress
success:^(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject) { success:^(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject) {
@ -284,8 +284,8 @@ static bool isFirstAccess = YES;
} }
+ (void) socialUserFromViewController:(UIViewController* _Nullable) viewController + (void) socialUserFromViewController:(UIViewController* _Nullable) viewController
blockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success blockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure { failure:(nullable void (^)(NSError * _Nonnull error))failure {
if (!viewController) { if (!viewController) {
viewController = [PNObjectUtilities topViewController]; viewController = [PNObjectUtilities topViewController];
@ -314,27 +314,21 @@ static bool isFirstAccess = YES;
} }
} }
else { else {
[[PNObjectConfig sharedInstance] refreshTokenForUserWithFacebookID:[result objectForKey:@"id"] facebookToken:[FBSDKAccessToken currentAccessToken].tokenString withBlockSuccess:^(BOOL refreshSuccess) {
PNUser *user = [[self class] new];
PNUser *user = [[self class] new]; [user setFirstName:[result objectForKey:@"first_name"]];
[user setLastName:[result objectForKey:@"last_name"]];
[user setFacebookId:[result objectForKey:@"id"]]; [user setEmail:[result objectForKey:@"email"]];
[user setAuthenticated:YES]; [user setFacebookId:[result objectForKey:@"id"]];
[user setFacebookUser:YES]; [user setAuthenticated:YES];
[user saveLocally]; [user setFacebookUser:YES];
[user reloadFormServer]; [user saveLocally];
USER = user; USER = user;
if (success) { if (success) {
success(user); success(user);
} }
} failure:^(NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
} }
}]; }];
@ -345,18 +339,18 @@ static bool isFirstAccess = YES;
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[@"public_profile",@"email",@"user_birthday"] fromViewController:viewController handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { [login logInWithReadPermissions: @[@"public_profile",@"email",@"user_birthday"] fromViewController:viewController handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) { if (error) {
NSLog(@"Process error"); NSLogDebug(@"Process error");
if (failure) { if (failure) {
failure(error); failure(error);
} }
} else if (result.isCancelled) { } else if (result.isCancelled) {
NSLog(@"Cancelled"); NSLogDebug(@"Cancelled");
if (failure) { if (failure) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Request cancelled", @"") code:kHTTPStatusCodeMethodNotAllowed userInfo:nil]; NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Request cancelled", @"") code:kHTTPStatusCodeMethodNotAllowed userInfo:nil];
failure(error); failure(error);
} }
} else { } else {
NSLog(@"Logged in"); NSLogDebug(@"Logged in");
[self socialUserFromViewController:viewController blockSuccess:success failure:failure]; [self socialUserFromViewController:viewController blockSuccess:success failure:failure];
} }
}]; }];
@ -374,16 +368,16 @@ static bool isFirstAccess = YES;
password:(NSString * _Nonnull) password password:(NSString * _Nonnull) password
withBlockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success withBlockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure { failure:(nullable void (^)(NSError * _Nonnull error))failure {
[[PNObjectConfig sharedInstance] refreshTokenForUserWithEmail:email password:password withBlockSuccess:^(BOOL refreshSuccess) { [[PNObjectConfig sharedInstance] refreshTokenForUserWithEmail:email password:password withBlockSuccess:^(BOOL refreshSuccess) {
if (refreshSuccess) { if (refreshSuccess) {
PNUser *user = [[self class] new]; PNUser *user = [[self class] new];
PNObjcPassword *objectPassword = [PNObjcPassword new]; PNObjcPassword *objectPassword = [PNObjcPassword new];
[objectPassword setPassword:password]; [objectPassword setPassword:password];
[objectPassword setConfirmPassword:password]; [objectPassword setConfirmPassword:password];
[user setAuthenticated:YES]; [user setAuthenticated:YES];
[user setEmail:email]; [user setEmail:email];
[user setPassword:objectPassword]; [user setPassword:objectPassword];
@ -391,22 +385,22 @@ static bool isFirstAccess = YES;
[user reloadFormServer]; [user reloadFormServer];
USER = user; USER = user;
if (success) { if (success) {
success(user); success(user);
} }
} }
} failure:failure]; } failure:failure];
} }
+ (void) uploadAvatar:(UIImage * _Nonnull) avatar + (void) uploadAvatar:(UIImage * _Nonnull) avatar
Progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress Progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
Success:(nullable void (^)(NSDictionary * _Nullable responseObject))success Success:(nullable void (^)(NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure { failure:(nullable void (^)(NSError * _Nonnull error))failure {
PNObjectFormData *formData = [PNObjectFormData formDataFromUIImage:avatar compression:1 name:@"file" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; PNObjectFormData *formData = [PNObjectFormData formDataFromUIImage:avatar compression:1 name:@"file" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
[self POSTWithEndpointAction:@"user/avatar" [self POSTWithEndpointAction:@"user/avatar"
formData:@[formData] formData:@[formData]
parameters:nil parameters:nil
@ -452,11 +446,11 @@ static bool isFirstAccess = YES;
- (UIImage* _Nonnull) userProfileImage:(BOOL) forceReload { - (UIImage* _Nonnull) userProfileImage:(BOOL) forceReload {
if (!_profileImage || forceReload) { if (!_profileImage || forceReload) {
if (_profileImageUrl) { if (_profileImageUrl) {
NSError* error = nil; NSError* error = nil;
NSData* data = [NSData dataWithContentsOfURL:_profileImageUrl options:NSDataReadingUncached error:&error]; NSData* data = [NSData dataWithContentsOfURL:_profileImageUrl options:NSDataReadingUncached error:&error];
if (error) { if (error) {
@ -468,18 +462,18 @@ static bool isFirstAccess = YES;
} }
} }
else { else {
return [UIImage imageNamed:@"userProfileAvatar"]; return [UIImage imageNamed:@"userProfileAvatar"];
} }
} }
else { else {
return _profileImage; return _profileImage;
} }
} }
#pragma mark PNObjectSubclassing Protocol #pragma mark PNObjectSubclassing Protocol
+ (NSDictionary *)objcetMapping { + (NSDictionary *)objcetMapping {
NSDictionary *mapping = @{ NSDictionary *mapping = @{
@"userId":@"uuid", @"userId":@"uuid",
@"firstName":@"firstName", @"firstName":@"firstName",