© Jesse Feiler 2018
Jesse FeilerBeginning Reactive Programming with Swifthttps://doi.org/10.1007/978-1-4842-3621-5_7

7. Adding a Facebook Login to an iOS App

Jesse Feiler1 
(1)
Plattsburgh, New York, USA
 

With a Facebook developer account, you can add Facebook features (such as logins) to your iOS app. This chapter will walk you through that process. It is useful for many of the Facebook tools that you may want to integrate with an iOS app. Furthermore, the steps used to integrate Facebook tools are similar in some ways to the steps you would use to integrate other tools, such as Amazon Web Services (AWS), which is the topic of the next part of this book.

Also, it’s important to note that there are a number of ways to handle this integration. CocoaPods (described in Chapter 2) are a very common way of handling integration. If you look into CocoaPods, you’ll see that what you have is an automated tool for managing your Xcode project files along with having versions automatically downloaded from GitHub by CocoaPods.

The heart of the integration is Xcode, its files, and its frameworks. Although a CocoaPods interface is available for the Facebook interface, this chapter will show you what happens on the source code/Xcode side of things. Remember that regardless of the integration technique that you use, the same basic structure (updating and integrating your Xcode project) is what has to happen.

First, though, it’s the Facebook login.

Starting to Integrate the Facebook SDK with an iOS app

There are two components you need to get started:
  • You need a Facebook developer account (see the previous chapter).

  • You need Xcode and a basic familiarity with it.

Although you can work offline with Xcode as you develop an app, you cannot work offline to create apps for Facebook or iOS, because you need to interact with the Facebook and iOS environments. If you have anything other than an ordinary Internet connection (for example, if you are behind a firewall that limits the sites you can visit), check out the basic steps to get started to make certain that you don’t need permission from another part of the organization.

Note

Both the Facebook and iOS developer sites change from time to time, so you may have to search around to find sections that have moved.

Start by creating an Xcode project to use for testing. In this chapter, the Single View sample app that is part of Xcode will be used. Figure 7-1 shows how you can create it.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig1_HTML.jpg
Figure 7-1

Create a single-view app to test with Facebook

Make a note of the app bundle identifier that is created based on the data you enter for the app . As you can see in Figure 7-2, the bundle identifier for this app is com.champlainarts.MySingleViewApp.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig2_HTML.jpg
Figure 7-2

Make a note of the bundle identifier

As always when you create a new app from a template in Xcode, run the app either on a device or on a simulator to make certain that it works properly. Figure 7-3 shows what you should see if you run it on the simulator for iPhone 8.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig3_HTML.jpg
Figure 7-3

Test the app

Yes, a successful implementation of the single-view app shows nothing. As you will see later in this chapter, you can easily add a label. All you should be concerned with at this point is that the app doesn’t fail or crash when you run it on Xcode .

Download the Facebook SDK for Swift

Log in to your developer account on developers.facebook.com. You’ll see choices to download the Facebook SDK for iOS, Android, and PHP, as well as for other platforms. As of this writing, the basic iOS SDK is still written in Objective-C, but you can download the Swift version if you want (you’ll see how to do that in this section).

The Swift version of the iOS SDK is under documentation. Once you’ve logged in, use the Docs menu item in the top navigation bar to get to the Documentation page, as shown in Figure 7-4.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig4_HTML.jpg
Figure 7-4

Look in Documentation for the Facebook SDK for iOS/Swift

You may have to scroll down, as you can see in Figure 7-5, to find the Swift SDK.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig5_HTML.jpg
Figure 7-5

The Facebook Swift SDK is available with all of the others

Once you have located the “SDK for Swift” link, open it, as you can see in Figure 7-6.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig6_HTML.jpg
Figure 7-6

Download the Facebook SDK for Swift

The frameworks shown in Figure 7-6 are the heart of the Facebook SDK for Swift. All frameworks for iOS were originally written in Objective-C. Today, some of the new frameworks are written in Swift, but when you build a Swift-based app, it is not a problem that some (or all) of the frameworks are written in Swift. Thus, when you download the SDK for Swift, you’ll wind up with frameworks that are often written in Objective-C, and that doesn’t matter. When you download the SDK for Swift, as shown in Figure 7-6, some of the frameworks have enhancements specifically for Swift, so keep these files safe and just add them as needed to your app.

The download link in Figure 7-6 takes you to GitHub, as you can see in Figure 7-7. You can download the source code as .zip or tar.gz.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig7_HTML.jpg
Figure 7-7

Download the latest Facebook SDK for Swift

The files downloaded from GitHub will vary over time both in content and in name. The downloaded files at the time of this book’s writing look like Figure 7-8.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig8_HTML.jpg
Figure 7-8

Downloaded Facebook files from GitHub

Adding Frameworks and Functionality to Your Facebook App

You can work around the issue of what file is where by using CocoaPods, but if you prefer to work with the actual files themselves, one technique that works for many people on many projects is to look for a sample file or app. In this case, there’s a Samples folder containing a SwiftCatalog app. Many times (but not always!) the sample app will be updated for any given project before the documentation is updated. If that is the case, build the app. If you look at the project navigator, as you can see in Figure 7-9, you’ll note intermediate files such as the frameworks are all built in the sample app.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig9_HTML.jpg
Figure 7-9

Build a sample to get frameworks

If you drag the needed frameworks into your own app, Xcode will put them in the right place in your project.

You can add new frameworks and functionality to your Facebook app either at the beginning or at any time as you work with the app. The My Apps menu in the navigator at the top of the screen (seen in Figure 7-10) lets you both add and reconfigure your app’s components.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig10_HTML.jpg
Figure 7-10

Add and modify your Facebook app frameworks and features

From your app dashboard (Figure 7-10) you can choose new products (features), as shown in Figure 7-11. This display shows you available products and links to specific documentation so you can decide what you want to work on.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig11_HTML.jpg
Figure 7-11

Add products to your Facebook app

Enhancing Your App

If you have followed along with this chapter, you have produced the app shown previously in Figure 7-3. It runs and displays its storyboard, which happens to be blank. In order to move forward, it makes sense to add something to your storyboard.

Go to main.storyboard in your app and add a label to it, as you can see in Figure 7-12.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig12_HTML.jpg
Figure 7-12

Add a label to your app

Next, add a Facebook login button to your app. If you have followed the sequence shown previously in which you built the SwitchUserSample example, you have the Facebook Login framework in that app and you can drag it into your new app. Alternatively, add Facebook Login from the products shown in Figure 7-11 by modifying one of your apps (shown in My Apps in Figure 7-10).

Once Facebook Login is added to your app, the code to add the button is simple. Add it to the viewDidLoad method of your app. (If it’s built on the SingleViewController template as described in this chapter, the app has one view controller called ViewController.

The code is shown in Figure 7-13 and in Listing  7-1.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig13_HTML.jpg
Figure 7-13

Add the Facebook login button code to viewDidLoad

Note

The basic viewDidLoad method with a stub is already part of the SingleViewApp template.

import UIKit
import FBSDKLoginKit
class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,
    // typically from a nib.
    let loginButton = FBSDKLoginButton()
    loginButton.center = view.center
    view.addSubview(loginButton)
  }
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
}
Listing 7-1

Add the Facebook Login Button

Rebuild your app (clean the build files first to make certain that you don’t have some experiments lying around). Now, when you run it, you should see your label and the button, as shown in Figure 7-14.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig14_HTML.jpg
Figure 7-14

Try your app with the label and login button

Remember that you didn’t just add the image of a button: you added the functionality of the login button, so when you run your app you’ll be asked for permission, as you can see in Figure 7-15.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig15_HTML.jpg
Figure 7-15

Test Facebook login integration

The login button should work now. Try it, as shown in Figure 7-16.
../images/459488_1_En_7_Chapter/459488_1_En_7_Fig16_HTML.jpg
Figure 7-16

Test the Facebook login button

You can also test with your own Facebook account. In addition, you can search developers.facebook.com to get test accounts you can use so that you don’t create fake accounts or interfere with your own account.

Note

Some developers test logging in with their own account but use the Facebook test accounts for adding information.

Summary

This has been an overview of integrating Facebook and iOS. The details may change from time to time, but the overview remains basically the same. And, in fact, it’s the same for all components of Facebook and other frameworks that you may want to integrate with your app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.154.161