Facebook Swift SDK updated to Swift 4

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.

Kristof Kocsis Written by:

8 Comments

  1. Calvin Usiri
    September 25, 2017
    Reply

    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
      Reply

      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’

  2. Seth Rininger
    October 9, 2017
    Reply

    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!!!

  3. arsalanwahid
    October 22, 2017
    Reply

    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.

  4. hou
    November 2, 2017
    Reply

    Thanks for sharing your knowledge

  5. Carlos
    November 7, 2017
    Reply

    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)) })

  6. Anonymous
    November 19, 2017
    Reply

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

    • Kristof Kocsis
      November 20, 2017
      Reply

      I’m glad I could help!

Leave a Reply

Your email address will not be published.