This repository has been archived by the owner on May 6, 2024. It is now read-only.
/
OEXFBSocial.m
85 lines (70 loc) · 2.68 KB
/
OEXFBSocial.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// OEXFBSocial.m
// edXVideoLocker
//
// Created by Prashant Kurhade on 20/11/14.
// Copyright (c) 2014 edX. All rights reserved.
//
#import "OEXFBSocial.h"
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import "edX-Swift.h"
#import "Logger+OEXObjC.h"
#import "NSNotificationCenter+OEXSafeAccess.h"
#import "OEXConfig.h"
#import "OEXFacebookConfig.h"
#import "OEXSession.h"
@interface OEXFBSocial ()
@end
@implementation OEXFBSocial
- (id)init {
self = [super init];
if(self != nil) {
[[NSNotificationCenter defaultCenter] oex_addObserver:self notification:OEXSessionEndedNotification action:^(NSNotification *notification, OEXFBSocial* observer, id<OEXRemovable> removable) {
[observer logout];
}];
}
return self;
}
- (void)loginFromController:(UIViewController *)controller completion:(void (^)(NSString *, NSError *))completionHandler {
FBSDKLoginManager* fbLoginManager = [[FBSDKLoginManager alloc]init];
[fbLoginManager logOut];
[fbLoginManager logInWithReadPermissions:@[@"email", @"public_profile"] fromViewController:controller handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
FBSDKAccessToken* accessToken = [FBSDKAccessToken currentAccessToken];
if (error) {
completionHandler(nil, error);
} else if (result.isCancelled) {
completionHandler(nil, error); //Reflecting as an error for now, before further discussion
} else {
if (![result.grantedPermissions containsObject:@"email"]) {
OEXLogInfo(@"SOCIAL", @"Email permission is missing");
}
if (![result.grantedPermissions containsObject:@"public_profile"]) {
OEXLogInfo(@"SOCIAL", @"Public profile permission is missing");
}
completionHandler([accessToken tokenString],error);
}
}];
}
- (BOOL)isLogin {
OEXConfig* config = [OEXConfig sharedConfig];
OEXFacebookConfig* facebookConfig = [config facebookConfig];
if(facebookConfig.appId && facebookConfig.enabled) {
return [FBSDKAccessToken currentAccessToken] != nil;
}
return NO;
}
- (void)logout {
if([self isLogin]) {
FBSDKLoginManager* fbLoginManager = [[FBSDKLoginManager alloc]init];
[fbLoginManager logOut];
}
}
- (void)requestUserProfileInfoWithCompletion:(void (^)(NSDictionary*, NSError *))completion {
if([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:@{@"fields": @"name,email"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
completion(result, error);
}];
}
}
@end