I need a Facebook login bassed app. My app should have 3, not necesary, view con
ID: 3842309 • Letter: I
Question
I need a Facebook login bassed app.
My app should have 3, not necesary, view controllers.
1- welcome screen with a button to conect using facebook.
2- the facebook button an a button to go back to the main page.
3- the viewcontroller that will shouw up as as the loggin went successful.
How can redirect the user to another window after loggin went ok?
This mis my code in the viewController.swift file
import UIKit
import FBSDKLoginKit //my Code
import FBSDKCoreKit //my Code
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let logInButton = FBSDKLoginButton()
view.addSubview(logInButton)
logInButton.center = view.center
if (FBSDKAccessToken.current() != nil) {
// User is already logged in, do work such as go to next view controller.
// I need help here. How to send the user to another window.
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Everything works like it should, I just help redirecting the user to "Show after successful login" after login with Facebook happens
Right now it only shows the Facebook logout button and stays in the same page...
Explanation / Answer
I am using java code for this,
after login from facebook, Facebook will validate the credentialsand revert back to the browser and then it will request for redirect_url, then redirect_url will call facebook again for the access_token.
if validation is successful, facebook will respond back with the access_token. now redirect_url will again request facebook for user data by sending the same acces_token, after the user_data is validated it will show successfull login on the page.
package com.java.social.facebook;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class FBConnection {
public static final String FB_APP_ID = "234231262685378";
public static final String FB_APP_SECRET = "y2a73dede3n49uid13502f5ab8cdt390";
public static final String REDIRECT_URI = "http://localhost:8080/Facebook_Login/fbhome";
static String accessToken = "";
public String getFBAuthUrl() {
String fbLoginUrl = "";
try {
fbLoginUrl = "http://www.facebook.com/dialog/oauth?" + "client_id="
+ FBConnection.FB_APP_ID + "&redirect_uri="
+ URLEncoder.encode(FBConnection.REDIRECT_URI, "UTF-8")
+ "&scope=email";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fbLoginUrl;
}
public String getFBGraphUrl(String code) {
String fbGraphUrl = "";
try {
fbGraphUrl = "https://graph.facebook.com/oauth/access_token?"
+ "client_id=" + FBConnection.FB_APP_ID + "&redirect_uri="
+ URLEncoder.encode(FBConnection.REDIRECT_URI, "UTF-8")
+ "&client_secret=" + FB_APP_SECRET + "&code=" + code;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fbGraphUrl;
}
public String getAccessToken(String code) {
if ("".equals(accessToken)) {
URL fbGraphURL;
try {
fbGraphURL = new URL(getFBGraphUrl(code));
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Invalid code received " + e);
}
URLConnection fbConnection;
StringBuffer b = null;
try {
fbConnection = fbGraphURL.openConnection();
BufferedReader in;
in = new BufferedReader(new InputStreamReader(
fbConnection.getInputStream()));
String inputLine;
b = new StringBuffer();
while ((inputLine = in.readLine()) != null)
b.append(inputLine + " ");
in.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Unable to connect with Facebook "
+ e);
}
accessToken = b.toString();
if (accessToken.startsWith("{")) {
throw new RuntimeException("ERROR: Access Token Invalid: "
+ accessToken);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.