← Back to Tutorials

15. Social Integration

Social Integration

Sharing with UIActivityViewController

Share content using the system share sheet:

let text = "Check out this awesome app!"
let image = UIImage(named: "screenshot")
let url = URL(string: "https://apps.apple.com/app/id12345")

let activityVC = UIActivityViewController(
    activityItems: [text, image, url].compactMap { $0 },
    applicationActivities: nil)

// For iPad (required for popover)
activityVC.popoverPresentationController?.barButtonItem = shareButton

present(activityVC, animated: true)

Sign in with Apple

Apple's privacy-focused authentication service:

import AuthenticationServices

let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]

let controller = ASAuthorizationController(authorizationRequests: [request])
controller.delegate = self
controller.presentationContextProvider = self
controller.performRequests()

// Delegate
func authorizationController(controller: ASAuthorizationController,
    didCompleteWithAuthorization authorization: ASAuthorization) {
    if let credential = authorization.credential as? ASAuthorizationAppleIDCredential {
        let userId = credential.user
        let fullName = credential.fullName
        let email = credential.email
        // Create user session
    }
}

Twitter, Facebook & LinkedIn

Use the native share sheet for social platforms. For deeper integration, use each platform's SDK:

// Twitter (X) URL scheme
if let url = URL(string: "twitter://post?message=Hello") {
    UIApplication.shared.open(url)
}

// Facebook URL scheme
if let url = URL(string: "fb://sharer?u=https://example.com") {
    UIApplication.shared.open(url)
}

// Social framework (deprecated - use UIActivityViewController)
// Modern approach: deep links or platform SDKs

Firebase Social Login

Integrate multiple social login providers via Firebase:

import FirebaseAuth
import GoogleSignIn

// Google sign-in
let config = GIDConfiguration(clientID: FirebaseApp.app()?.options.clientID ?? "")
GIDSignIn.sharedInstance.configuration = config

GIDSignIn.sharedInstance.signIn(withPresenting: self) {
    result, error in
    guard let result = result else { return }

    let credential = GoogleAuthProvider.credential(
        withIDToken: result.user.idToken?.tokenString ?? "",
        accessToken: result.user.accessToken.tokenString)

    Auth.auth().signIn(with: credential) { authResult, error in
        // Signed in with Firebase
    }
}
Practice Task: Create a "Social Profile" screen with Sign in with Apple and a Google sign-in button. After authentication, display the user's name and email. Add a share button that opens UIActivityViewController with app details. Test the share sheet with different item types (text, URL, image).