For a while now Facebook’s Swift SDK hasn’t been updated and some deprecated code was left inside. Then Swift 4 came along and it made things even worse. I believe it is time for a guide on how to set up the SDK in Xcode 9 / Swift 4. Alternatively, you could use the iOS SDK but I’ll leave it up to you.

Multiple issues have been created regarding this. (issue1 issue2)

For starters according to the official Facebook documentation you set up the SDK with CocoaPods by using the following pods:

pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'

This is all fine and well but if you try to compile your project now it won’t work. In the next three steps I’ll show you the errors and the fixes.


1) Get-only properties

Source: Brigadier @ StackOverflow

This error normally would show up only when you fix the 2nd and 3rd error but you need to fix this before theme, not to mention it is convenient this way.
I don’t know how this happened and why was this left in the SDK, regardless here is the fix.

Go inside the Podfile

Replace this:

pod 'FacebookShare'

With this:

pod 'FacebookShare', :git => 'https://github.com/1amageek/facebook-sdk-swift'

2) ‘toUIntMax()’ obsoleted

Source: zach-fuller @ GitHub

This error is a result of updating to Swift 4, but it is very easy to fix.

There are two instances of this error, in both cases:

Replace this:

$0.toUIntMax() as UInt64

With this:

UInt64($0)

3) Cannot subscript a value…

Source: carlhung @ Github

The full error message:

Cannot subscript a value of type 'String.UnicodeScalarView' with an index of type 'CountableRange' (aka 'CountableRange')

This fix yet again is replacing the code.

Replace this:

let range = 0 ..< min(10, cleaned.count)
let characters = cleaned[range].map(Character.init)
return String(characters)

With this:

let substr = String(cleaned)
if substr.characters.count > 10 {
  let endIndex = substr.index(substr.startIndex, offsetBy: 9)
  return String(substr[substr.startIndex...endIndex])
} else {
  return substr
}

Conclusion
Let’s hope that Facebook will get back to this SDK and update it. It would be nice if everything would be updated to Swift even the Facebook app. Until then we will have to live with this SDK and the iOS SDK.

Categories: Tutorial

11 Comments

Calvin Usiri · September 25, 2017 at 5:03 am

Invalid `Podfile` file: syntax error, unexpected ‘=’, expecting keyword_end.
Is the error that I get when I try to update the podfile with pod ‘FacebookShare’, :git => ‘https://github.com/1amageek/facebook-sdk-swift’

    Kristof Kocsis · September 25, 2017 at 4:57 pm

    Yes, thank you for the observation! One of the charaters wasn’t encoded in HTML properly. I have updated the post, it should work now.
    The line:

    pod ‘FacebookShare’, :git => ‘https://github.com/1amageek/facebook-sdk-swift’

Seth Rininger · October 9, 2017 at 2:26 pm

Hey, thanks a ton for the article. I found it very helpful. Prior to reading this, I had just gone back to previous releases of the Swift SDK and base sdk version that correlated. If you could let me know when this article is out of date and there is a better method, that would be amazing!!!

arsalanwahid · October 22, 2017 at 8:01 am

Hey I’m using xcode 9 with swift 4

Thanks for sharing your knowledge

Your git file reduced errors in my project from 28 to just 7 Cheers ,but still getting a lot of errors ! from this cocoapod sadly.

hou · November 2, 2017 at 7:30 am

Thanks for sharing your knowledge

Carlos · November 7, 2017 at 7:31 pm

im update my pods and framework but after im try compiling but xcode show this errors in AppEventBuiltin swift (Ambiguous use init)

maxRatingValue.onSome({ parameters[.maxRatingValue] = NSNumber(value: UInt64($0)) })

itemCount.onSome({ parameters[.itemCount] = NSNumber(value: UInt64($0)) })

Anonymous · November 19, 2017 at 1:55 am

Thanks a lot for posting this. It saved me hours.

    Kristof Kocsis · November 20, 2017 at 1:10 pm

    I’m glad I could help!

Nawaaz · December 22, 2017 at 6:12 am

Hi, I have been following another tutorial when adding facebook login to my app, and I get the app to run. However 2 errors keep on showing up,
1 is canOpenURL: failed for URL: “fbauth2:/” – error: “The operation couldn’t be completed. (OSStatus error -10814.)”
2. When I click the login button I’m left with (Invalid scope: public_profile)
Any work around regarding this error would be appreciated

Timothynop · February 8, 2018 at 6:37 pm

Oh my goodness! a tremendous article dude. Thank you However I’m experiencing subject with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss problem? Anyone who is aware of kindly respond.

    Kristof Kocsis · March 24, 2018 at 2:44 pm

    Just type /feed at the end of the URL of the blog page.

Leave a Reply

Your email address will not be published.