{"id":2524,"date":"2017-07-12T12:32:44","date_gmt":"2017-07-12T07:02:44","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=2524"},"modified":"2017-07-12T12:32:44","modified_gmt":"2017-07-12T07:02:44","slug":"facebook-integration-in-swift-3-0","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/","title":{"rendered":"Facebook Integration in Swift 3.0"},"content":{"rendered":"<h2><strong>Introduction:\u00a0<\/strong><\/h2>\n<h2 class=\"p1\"><strong>There are two goals of any application: acquire as many customers as possible or deliver highest quality engagement within a target market. So integrating your app with any social media will fulfill your goal.<\/strong><\/h2>\n<p>Here we will know about how to authenticate a user with social media like Facebook\u00a0in his application.<\/p>\n<p>Various steps that you need to follow to do\u00a0Facebook Integration in Swift 3.0 in your app are:<\/p>\n<ul>\n<li>Open\u00a0Xcode and create a new Single View Application.<\/li>\n<li><span style=\"font-weight: 400;\">Go to<\/span> <a href=\"https:\/\/developers.facebook.com\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.facebook.com<\/a>\u00a0and login with your account. It will open Facebook dashboard.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration1.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-2544 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration1.png\" alt=\"FbIntegration1\" width=\"1163\" height=\"658\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration1.png 1163w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration1-300x170.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration1-1024x579.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration1-624x353.png 624w\" sizes=\"(max-width: 1163px) 100vw, 1163px\" \/><\/a><\/li>\n<li>Now click on <strong>&#8216;My Apps&#8217;<\/strong> from the top right view. A following page is opened.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration21.png\"><img decoding=\"async\" class=\"alignnone wp-image-2664 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration21.png\" alt=\"FbIntegration2\" width=\"1036\" height=\"148\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration21.png 1036w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration21-300x43.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration21-1024x146.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration21-624x89.png 624w\" sizes=\"(max-width: 1036px) 100vw, 1036px\" \/><\/a><\/li>\n<li>From here,\u00a0Click on <strong>+ Add a New App\u00a0<\/strong>if your application is not registered with the Facebook. When you do so, a form is opened.\u00a0<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration3.png\"><br \/>\n<\/a>Now fill application details:<\/p>\n<ul>\n<li><strong>Name<\/strong>&#8211; Display Name of your application<\/li>\n<li><strong>Contact<\/strong> <strong>Email<\/strong>&#8211; Enter the email that you want to register (it will automatically get filled with the account you have registered)<\/li>\n<li><strong>Category<\/strong>&#8211; select the category<br \/>\nwhen you fill all the details\u00a0your form will look like<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration31.png\"><br \/>\n<\/a><a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration31.png\"><img decoding=\"async\" class=\"alignnone wp-image-2665 \" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration31.png\" alt=\"FbIntegration3\" width=\"479\" height=\"241\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration31.png 718w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration31-300x151.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration31-624x314.png 624w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/a><\/li>\n<\/ul>\n<\/li>\n<li>Click on<strong> Create App ID.\u00a0<\/strong>It will register your app with the Facebook.<\/li>\n<li><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The next step is to enable the app login from iOS. Click on the <strong>Settings<\/strong> option in the left menu, and in the main area click on the <strong>+Add Platform<\/strong> big button.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration42.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2669 \" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration42.png\" alt=\"FbIntegration4\" width=\"479\" height=\"302\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration42.png 906w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration42-300x189.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration42-624x393.png 624w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/a><br \/>\n<\/span><\/span><\/span><\/li>\n<li><span style=\"font-size: 1rem;\"><span style=\"font-size: 1rem;\">In the new window, select the <strong>iOS<\/strong> platform. It will open the detailed view for iOS to enter the details of your application.<br \/>\n<\/span><\/span><a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration5.png\"><br \/>\n<\/a><a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-2562 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration5.png\" alt=\"FbIntegration5\" width=\"786\" height=\"390\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration5.png 786w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration5-300x149.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration5-624x310.png 624w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a>Now you need to fill all the details.<\/li>\n<li><span style=\"font-size: 1rem;\"><span style=\"font-size: 1rem;\"><span style=\"font-size: 1rem;\"><span style=\"font-size: 1rem;\">In the <strong>Bundle ID<\/strong> field, it\u2019s very important to enter the <em>exact same to the project\u2019s Bundle Identifier<\/em> value, otherwise the app users won\u2019t be able to be authorized.<br \/>\nSo, go back to your Xcode project and click on the project target on the <strong>Project Navigator<\/strong> pane, and under the <strong>General<\/strong> tab copy the value of the <strong>Bundle Identifier<\/strong> field.<\/span><\/span><\/span><\/span><a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration61.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2564 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration61.png\" alt=\"FbIntegration6\" width=\"912\" height=\"229\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration61.png 912w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration61-300x75.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration61-624x157.png 624w\" sizes=\"(max-width: 912px) 100vw, 912px\" \/><\/a><\/li>\n<li>Return on the Facebook dashboard, and paste or type the Bundle Identifier in the <strong>Bundle ID<\/strong> field<\/li>\n<li><span style=\"font-size: 1rem;\">Make sure to enable <strong>single sign-on<\/strong> and finally\u00a0<strong>save changes<\/strong>.<br \/>\nNote: <strong>App Id<\/strong> created here is later used to add into your project.<\/span><\/li>\n<li><span style=\"font-size: 1rem;\"><span style=\"font-size: 1rem;\">Return back to the dashboard from left navigation and check if the green circle besides your project name is unfilled. If it is so that means your app is in development mode yet and can only be used by app admins, developers and testers only. No other user can authenticate with Facebook from this app.<br \/>\nFor making it publically accessible to all users, click on the question mark below the app name and toggle the button for making it public.<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration71.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-2667 \" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration71.png\" alt=\"FbIntegration7\" width=\"562\" height=\"204\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration71.png 784w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration71-300x109.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration71-624x227.png 624w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/a><br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration7.png\"><br \/>\n<\/a><\/span><\/span><span style=\"font-size: 1rem;\">When you have enabled that button then it asks for the confirmation. So, Confirm it.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2571 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration8.png\" alt=\"FbIntegration8\" width=\"446\" height=\"151\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration8.png 446w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration8-300x102.png 300w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/a><br \/>\n<strong>Note:<\/strong>\u00a0If you&#8217;ll skip this step then you won&#8217;t be able to login with the account other than from which you have registered.<\/span><\/li>\n<\/ul>\n<p>This is all you have to do on the Facebook dashboard for registering your app with it.<\/p>\n<h2><span style=\"color: #333333;\">Add FBSDKLoginKit &amp; FBSDKCoreKit Frameworks<\/span><\/h2>\n<p>Next main step is to add Frameworks for Facebook.<br \/>\nThere are various versions available of Facebook SDK. Here, I would be using Facebook SDK version 4.17.0 for this. You can either download them and add to your project or you can add them via cocoa pods.<br \/>\nHere i am using cocoa pods for this. Make sure you have cocoa pods installed on your system.<br \/>\n<strong>Refer following steps to add framework:<\/strong><\/p>\n<ul>\n<li>Open your project&#8217;s root directory for the Pod file creation if it is not created yet. Now open <strong>Terminal<\/strong> and go upto your project root directory.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2576 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration9.png\" alt=\"FbIntegration9\" width=\"583\" height=\"362\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration9.png 583w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration9-300x186.png 300w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/a><br \/>\nRun command <strong>pod init. <\/strong>It will create a pod file for you in the root directory.<\/li>\n<li>Now open that\u00a0<strong>Podfile <\/strong>and write <strong>pod \u2018FBSDKCoreKit\u2019, \u20184.17.0\u2019<\/strong>\u00a0in the pod file you have just created above the line # Pods for FBIntegration.<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2577 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration10.png\" alt=\"FbIntegration10\" width=\"651\" height=\"418\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration10.png 651w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration10-300x193.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration10-624x401.png 624w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><\/li>\n<li>Go back to the terminal and Run command <strong>pod install<\/strong>\u00a0there. It will install all dependencies for Facebook Sdk.<\/li>\n<li>Similarly add other pods for Facebook like this and install them like above.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2578 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration11.png\" alt=\"FbIntegration11\" width=\"652\" height=\"425\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration11.png 652w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration11-300x196.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration11-624x407.png 624w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/a><\/li>\n<li>Now Close Terminal and open your xcode <b>.<\/b><b>workspace<\/b> file in your project directory.<br \/>\n<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2579 size-full\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration12.png\" alt=\"FbIntegration12\" width=\"475\" height=\"363\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration12.png 475w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/FbIntegration12-300x229.png 300w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/a><\/li>\n<\/ul>\n<p>Note: If you open <strong>.xcodeproj<\/strong> file then you won\u2019t get pod dependencies in your project. And, it will throw error that dependency files are not found.<\/p>\n<h2>Configure Xcode Project<\/h2>\n<p>There is one final step needed to be performed, and that is to add three new keys to the project\u2019s .plist file. For this:<\/p>\n<ul>\n<li>In Xcode, right-click your project&#8217;s .plist file and select <b>Open As<\/b>\u00a0<strong>-&gt;<\/strong> <b>Source Code<\/b>.<\/li>\n<li>Insert the following XML snippet into the body of your file just before the final <code>&lt;\/dict&gt;<\/code> element.\n<pre class=\"lang:xhtml decode:true\">&lt;key&gt;CFBundleURLTypes&lt;\/key&gt;\r\n&lt;array&gt;\r\n  &lt;dict&gt;\r\n    &lt;key&gt;CFBundleURLSchemes&lt;\/key&gt;\r\n    &lt;array&gt;\r\n      &lt;string&gt;fb{your-app-id}&lt;\/string&gt;\r\n    &lt;\/array&gt;\r\n  &lt;\/dict&gt;\r\n&lt;\/array&gt;\r\n&lt;key&gt;FacebookAppID&lt;\/key&gt;\r\n&lt;string&gt;{your-app-id}&lt;\/string&gt;\r\n&lt;key&gt;FacebookDisplayName&lt;\/key&gt;\r\n&lt;string&gt;{your-app-name}&lt;\/string&gt;\r\n&lt;key&gt;LSApplicationQueriesSchemes&lt;\/key&gt;\r\n&lt;array&gt;\r\n  &lt;string&gt;fbapi&lt;\/string&gt;\r\n  &lt;string&gt;fb-messenger-api&lt;\/string&gt;\r\n  &lt;string&gt;fbauth2&lt;\/string&gt;\r\n  &lt;string&gt;fbshareextension&lt;\/string&gt;\r\n&lt;\/array&gt;\r\n&lt;key&gt;NSPhotoLibraryUsageDescription&lt;\/key&gt;\r\n  &lt;string&gt;{human-readable reason for photo access}&lt;\/string&gt;<\/pre>\n<\/li>\n<li>Replace <strong>fb{your-app-id}<\/strong> with your Facebook app ID, prefixed with fb. For example, fb123456. You can find your <strong>App ID<\/strong> on the Facebook dashboard.<br \/>\nReplace <strong>{your-app-id}<\/strong> with your app ID.<br \/>\nReplace <strong>{your-app-name}<\/strong> with the <b>display name<\/b> you specified in the App Dashboard (DesignPatternDemo in this case)<\/li>\n<\/ul>\n<h2 id=\"app-delegate\">Connect App Delegate<\/h2>\n<p>To configure Facebook with your application, write the following line of code in <strong>didFinishLaunchingWithOptions<\/strong>.<\/p>\n<pre class=\"lang:swift decode:true \">func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {\r\n        \/\/ Override point for customization after application launch.\r\n        \r\n        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)\r\n\r\n        return true\r\n    }<\/pre>\n<p>Now it will throw error as it wont found FBSDKApplicationDelegate. For this you need to import Facebook frameworks.<\/p>\n<pre class=\"lang:swift decode:true\">import FBSDKLoginKit\r\nimport FBSDKCoreKit<\/pre>\n<p>Add these on the top of AppDelegate.swift file.<\/p>\n<p>Next, add method <strong>openURL<\/strong>, it is used to control flow once user successfully logged in through Facebook and returning back to application.<\/p>\n<pre class=\"lang:swift decode:true \">func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -&gt; Bool {\r\n        \r\n        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url as URL!, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: nil)\r\n        \r\n    }\r\n<\/pre>\n<h2><strong>Design User Interface<\/strong><\/h2>\n<p>Next, you will need to add the <strong>Facebook Login\u00a0Button<\/strong> so that the user can initiate the sign-in process. You can add it in two ways: via <strong>Custom Button<\/strong> or\u00a0<strong>FacebookSDK\u00a0Button<\/strong>. Make the following changes to the view controller that manages your app&#8217;s sign-in screen.<\/p>\n<h3>Default Button<\/h3>\n<ul>\n<li>It automatically displays the log in\/log out Facebook button depending on the connection state. For this you need to\u00a0add a button of type <strong><strong>FBSDKLoginButton <\/strong><\/strong>and configure it to read permissions. It\u2019s always necessary to specify the <strong><em>read permissions<\/em><\/strong> you want for your app when logging in with Facebook.<br \/>\nAdd the following code in your <strong>ViewController.swift<\/strong><\/p>\n<pre class=\"lang:swift decode:true\">let loginButton: FBSDKLoginButton = {\r\n        let button = FBSDKLoginButton()\r\n        button.readPermissions = [\"email\", \"user_friends\"]\r\n        return button\r\n    }()<\/pre>\n<p>Set the\u00a0button&#8217;s frame w.r.t view and add it on the super view. For example:<\/p>\n<pre class=\"lang:swift decode:true\">loginButton.center = (view.viewWithTag(0)?.center)!\r\n        view.viewWithTag(0)?.addSubview(loginButton)\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li>At this point you need to check if the SDK Token already exists. In other words you will check if you the user is logging in for the first time or the request has already been made.\n<pre class=\"lang:swift decode:true \">if FBSDKAccessToken.current() != nil{\r\n            fetchProfile()\r\n        }\r\n<\/pre>\n<p>In this if the token is already present then we can directly made the graph request for fetching user Info.<br \/>\nNow what is this Graph Api request ? How this is made ? This will be explained later.<\/li>\n<li>Now you need to set the delegate on this button so that when login will\u00a0successful you&#8217;ll get call back on those delegate methods. Delegate can be set as:<br \/>\n<strong><span class=\"s1\">loginButton<\/span><span class=\"s2\">.<\/span><span class=\"s1\">delegate<\/span><span class=\"s2\"> = self\u00a0<\/span><span class=\"s1\">\/\/Your view controller object<\/span><\/strong><\/li>\n<li>Now inherits your <strong>ViewController.swift<\/strong> that conforms to\u00a0<strong>FBSDKLoginButtonDelegate<\/strong>\u00a0<span style=\"font-size: 1rem;\">protocol as:<\/span>\n<pre class=\"lang:swift decode:true\">class ViewController: UIViewController, FBSDKLoginButtonDelegate{\r\n<\/pre>\n<p>It will throw error as UIViewController does not conform to protocol. So we need to implement its delegate methods.<\/p>\n<pre class=\"lang:swift decode:true\"> func loginButtonWillLogin(_ loginButton: FBSDKLoginButton!) -&gt; Bool {\r\n        \r\n        return true\r\n    }<\/pre>\n<pre class=\"lang:swift decode:true \">func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {\r\n        print(\"logout\")\r\n        \r\n        \/\/When you call the logOut, the user is logged out of your app. it will not logout you from your fb account. if you want to do so, go into the safari app, you can go Facebook.com and logout of your account.\r\n        let loginManager: FBSDKLoginManager = FBSDKLoginManager()\r\n        loginManager.logOut()\r\n    }<\/pre>\n<pre class=\"lang:swift decode:true\"> func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {\r\n        if error != nil {\r\n            print(error)\r\n        } else {\r\n            fetchProfile()\r\n        }\r\n    }<\/pre>\n<h3><strong>Custom Button<\/strong><\/h3>\n<\/li>\n<li>For this you need to add a <strong>UIButton<\/strong> on you view controller in the Interface Builder and create its <strong>IBAction<\/strong> <strong>method<\/strong>.<br \/>\nThen we need to configure our button to read permissions as we have done for default button above. In order to fetch the total count of friends we have to read permission for it separately. For this add the following code in this method.<\/p>\n<pre class=\"lang:swift decode:true\">if FBSDKAccessToken.current() != nil {\r\n            fetchProfile()\r\n        } else {\r\n            \r\n            let loginManager = FBSDKLoginManager()\r\n            \r\n            loginManager.logIn(withReadPermissions: [\"email\",\"public_profile\",\"user_friends\"], from: currentVC, handler: { (loginResults: FBSDKLoginManagerLoginResult?, error: Error?) -&gt; Void in\r\n                \r\n                if !(loginResults?.isCancelled)! {\r\n                    self.fetchProfile()\r\n                } else {    \/\/ Sign in request cancelled\r\n                    let err = NSError()\r\n                    \/\/ handle error object\r\n                }\r\n            })\r\n        }<\/pre>\n<p>It will first check for the access token. It will be nil when you have logged in for the first time and we need to read permissions otherwise we don&#8217;t need it.<br \/>\nNote: Make sure you have import the Facebook frameworks on the top of your class otherwise it will throw error.<\/li>\n<\/ul>\n<p><strong>Now you will somewhat confused about what the fetchProfile is ?\u00a0<\/strong><br \/>\nThis method is used to make the graph request to fetch the profile information for the person currently logged into your app.<\/p>\n<h3><strong>Configuring\u00a0Facebook Graph Api\u00a0<\/strong><\/h3>\n<p class=\"p1\">The SDK has a <b>startWithCompletionHandler<\/b>: method on <b>FBSDKGraphRequest<\/b> to implicitly create a <b>FBSDKGraphRequestConnection<\/b> for you. For this add the \u00a0following code in your class:<\/p>\n<pre class=\"lang:swift decode:true \">func fetchProfile() {\r\n        \r\n        FBSDKGraphRequest(graphPath: \"me\", parameters: parameters).start{ (connection,result,error)-&gt; Void in\r\n            \r\n            if error != nil {   \/\/ Error occured while logging in\r\n                \/\/ handle error\r\n                print(error)\r\n                return\r\n            }\r\n            \r\n            \/\/ Details received successfully\r\n            let dictionary = result as! [String: AnyObject]\r\n            \/\/ pass this dictionary object into your model class initialiser\r\n        }\r\n    }<\/pre>\n<p class=\"p1\">Also you need to pass the parameters that you want to fetch. So add the following line of code above this method.<\/p>\n<pre class=\"lang:swift decode:true\">let parameters = [\"fields\": \"email,picture.type(large),name,gender,age_range,cover,timezone,verified,updated_time,education,religion,friends\"]<\/pre>\n<p>You can add several other parameters if they can be accessed or skip anyone from them. As i have added &#8220;friends&#8221; above to fetch the total no of friends in the list.<br \/>\nWhen you have successfully executed with the above code\u00a0you are either carrying the user info or the error object that has occured.<\/p>\n<p>Now Run your project and you&#8217;ll have Facebook button on your view. Click on it and it will get navigate on to safari and asks you to enter your details. On signing in you will get back to your app with your account information fetched in success block.<\/p>\n<p>This is all about the Facebook Integration in Swift 3.0. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction:\u00a0 There are two goals of any application: acquire as many customers as possible or deliver highest quality engagement within a target market. So integrating your app with any social media will fulfill your goal. Here we will know about how to authenticate a user with social media like Facebook\u00a0in his application. Various steps that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3464,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-2524","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Facebook Integration in Swift 3.0 - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Facebook Integration in Swift 3.0 - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Introduction:\u00a0 There are two goals of any application: acquire as many customers as possible or deliver highest quality engagement within a target market. So integrating your app with any social media will fulfill your goal. Here we will know about how to authenticate a user with social media like Facebook\u00a0in his application. Various steps that [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-12T07:02:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/04\/Facebook-Integration.png\" \/>\n\t<meta property=\"og:image:width\" content=\"984\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Facebook Integration in Swift 3.0\",\"datePublished\":\"2017-07-12T07:02:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/\"},\"wordCount\":1469,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/Facebook-Integration.png\",\"articleSection\":[\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/\",\"name\":\"Facebook Integration in Swift 3.0 - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/Facebook-Integration.png\",\"datePublished\":\"2017-07-12T07:02:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/Facebook-Integration.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/Facebook-Integration.png\",\"width\":984,\"height\":563},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/facebook-integration-in-swift-3-0\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Facebook Integration in Swift 3.0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Facebook Integration in Swift 3.0 - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/","og_locale":"en_US","og_type":"article","og_title":"Facebook Integration in Swift 3.0 - InnovationM - Blog","og_description":"Introduction:\u00a0 There are two goals of any application: acquire as many customers as possible or deliver highest quality engagement within a target market. So integrating your app with any social media will fulfill your goal. Here we will know about how to authenticate a user with social media like Facebook\u00a0in his application. Various steps that [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/","og_site_name":"InnovationM - Blog","article_published_time":"2017-07-12T07:02:44+00:00","og_image":[{"width":984,"height":563,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/04\/Facebook-Integration.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Facebook Integration in Swift 3.0","datePublished":"2017-07-12T07:02:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/"},"wordCount":1469,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/04\/Facebook-Integration.png","articleSection":["Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/","url":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/","name":"Facebook Integration in Swift 3.0 - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/04\/Facebook-Integration.png","datePublished":"2017-07-12T07:02:44+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/04\/Facebook-Integration.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/04\/Facebook-Integration.png","width":984,"height":563},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/facebook-integration-in-swift-3-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Facebook Integration in Swift 3.0"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/2524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=2524"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/2524\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/3464"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=2524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=2524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=2524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}