Latest 0.4.0
Homepage https://github.com/iwheelbuy/SocialNetwork
License MIT
Platforms ios 9.0
Authors

Version
License
Platform

Usage

Install via CocoaPods:

pod 'SocialNetwork', '0.3.4'

You can open an official application of a social network or present SFSafariViewController for authorization:

switch SocialNetwork.facebook.appExists {
case true:
    UIApplication.shared.openURL(SocialNetwork.facebook.appUrl)
case false:
    let controller = SFSafariViewController(url: SocialNetwork.facebook.oauthUrl)
    UIApplication.shared.keyWindow?.rootViewController?.present(controller: controller)
}

Take into account that not each social network allows to authorize via its official application.

Info.plist setup

To be able to use appExists variable you should provide LSApplicationQueriesSchemes for desired social networks:

<key>LSApplicationQueriesSchemes</key>
<array>
    <!-- facebook.com -->
    <string>fb</string>
    <string>fbapi</string>
    <string>fbauth</string>
    <string>fbauth2</string>
    <!-- ok.ru -->
    <string>odnoklassniki</string>
    <string>okauth</string>
    <!-- vk.com -->
    <string>vk</string>
    <string>vk-share</string>
    <string>vkauthorize</string>
</array>

Required CFBundleURLTypessocialnetwork. Other CFBundleURLTypes are optional and should be provided only if you plan to use authorization via official applications:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>SocialNetwork</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>socialnetwork</string>
            <!-- facebook.com -->
            <string>fb0123456789</string>
            <!-- ok.ru -->
            <string>ok0123456789</string>
            <!-- vk.com -->
            <string>vk0123456789</string>
        </array>
    </dict>
</array>

Redirect URL setup

In each social network, which is planned to be used for authorization, you should provide a redirect URL:

https://iwheelbuy.github.io/SocialNetwork/simplified.html

The code of redirection html page can be found there:

https://github.com/iwheelbuy/SocialNetwork/blob/master/docs/simplified.html

Basic setup

Set the SocialNetworkDataSource and the SocialNetworkDelegate somewhere in your project:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    SocialNetwork.dataSource = self
    SocialNetwork.delegate = self
    return true
}

Proceed the URL this way:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if SocialNetwork.didProceed(url: url) {
        return true
    }
    return false
}

Conform to SocialNetworkDelegate to receive authorization data:

extension AppDelegate: SocialNetworkDelegate {

    func socialNetwork(socialNetwork: SocialNetwork, didCompleteWithParameters parameters: [String : String]) {
        // hide authorization controllers if there are some
        if let token = socialNetwork.getToken(parameters: parameters) {
            // do something with token
        }
    }
}

Conform to SocialNetworkDataSource and provide information for required social networks:

extension AppDelegate: SocialNetworkDataSource {

    func socialNetworkClientIdentifier(socialNetwork: SocialNetwork) -> String? {
        switch socialNetwork {
        case .facebook:
            return "0123456789"
        default:
            return nil
        }
    }
}

There are some additional optional methods, where you can provide client secret for code flow authorization or change the default permissions:

extension AppDelegate: SocialNetworkDataSource {

    func socialNetworkClientSecret(socialNetwork: SocialNetwork) -> String? {
        switch socialNetwork {
        case .facebook:
            return "987654321"
        default:
            return nil
        }
    }

    func socialNetworkPermissions(socialNetwork: SocialNetwork) -> String? {
        switch socialNetwork {
        case .facebook:
            return "public_profile,email"
        default:
            return nil
        }
    }
}

Author

iwheelbuy, [email protected]

License

SocialNetwork is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SocialNetwork",
    "version": "0.4.0",
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "Sources/**/*.swift",
    "homepage": "https://github.com/iwheelbuy/SocialNetwork",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "iWheelBuy": "[email protected]"
    },
    "source": {
        "git": "https://github.com/iwheelbuy/SocialNetwork.git",
        "tag": "0.4.0"
    },
    "summary": "SocialNetwork",
    "description": "SocialNetwork handles OAuth for various social networks",
    "cocoapods_version": ">= 1.5.3"
}

Pin It on Pinterest

Share This