Skip to content

Instantly share code, notes, and snippets.

@benck
Last active May 8, 2018 17:29
Show Gist options
  • Save benck/7514755 to your computer and use it in GitHub Desktop.
Save benck/7514755 to your computer and use it in GitHub Desktop.
// in appDelegate.m
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
// add app-specific handling code here
return wasHandled;
//return [self.session handleOpenURL:url];
}
// connect function
- (void)facebookConnect {
// Initialize a session object
FBSession *session = [[FBSession alloc] init];
// Set the active session
[FBSession setActiveSession:session];
// Open the session
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Respond to session state changes,
// ex: updating the view
if(status == FBSessionStateClosed){
NSLog(@"user click disconnect.");
return;
}
if(status == FBSessionStateClosedLoginFailed){
NSLog(@"user does not want login.");
return;
}else{
NSLog(@"response status: %d", status);
}
NSString *accessToken = nil;
// update from facebook api 3.2
if(session.accessTokenData)
accessToken = session.accessTokenData.accessToken;
if(accessToken){
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"fb_token":accessToken};
[manager POST:@"https://banck.tw/api/fb_token" parameters:parameters success:^(AFHTTPRequestOperation *operation, id JSON) {
// your server returns, save data to userDefaults
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// your server fails
}];
}
}];
}
// disconnect
- (void)disconnectFacebook {
[FBSession.activeSession closeAndClearTokenInformation];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment