Skip to content

oyvinddd/twitch-ios-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twitch banner

CI Status Version License Platform

Information

⚠️ THIS SOFTWARE IS UNOFFICIAL AND IS IN NO WAY ENDORSED BY TWITCH.TV ⚠️

This SDK is a modern, lightweight wrapper around the official Twitch API. Its main purpose is to make your life easier when building apps that needs to integrate with the various Twitch services. The code is 100% Swift and does not rely on any external dependencies. Deployment target has been set to iOS 12.

Installation

Twitch is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Twitch'

Prerequisites

Before you can start using the SDK you need to register your app on the Twitch developer site.

Usage

1. Initialization

Once you have registered your app and obtained a client ID you are good to go. To initialize the SDK put the following in your application's application(_:didFinishLaunchingWithOptions:) (or in the related scene delegate method):

// A configuration with redirect uri and scope(s) needs to be specified
let config = TWConfig(redirectUri: "REDIRECT_URI", scopes: [TWConfig.Scope.openid])
// Initialize the Twitch object with client ID and configuration
Twitch.initialize(clientId: "CLIENT_ID", config: config)

2. Getting an access token

2.1 OAuth Token (implicit flow)

The simplest way to retrieve an OAuth 2.0 access token is by using the included TWAuthViewController:

final class MyViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // This presents a web view where the user can login to his/her Twitch account.
        // After a successful login, the access token is stored automatically by the SDK
        // and you can proceed to calling the Twitch API.
        present(TWAuthViewController(delegate: self), animated: true, completion: nil)
    }
}

// If you want a callback when the access token has been successfully retrieved,
// you can implement the (optional) TWOAuthDelegate method below.
extension MyViewController: TWOAuthDelegate {
    
    func didFetchToken(_ accessToken: String) {
        print("Got an OAuth 2.0 token: \(accessToken)")
    }
}

3. Calling the API

The example below fetches all the top games (by number of current viewers) on Twitch.

Twitch.Games.getTopGames { result in
    // Note! It's safe to call UI updates from this block
    switch result {
    case .success(let container):
        for game in container.data {
            print(game)
        }
        break
    case .failure(let error):
        print(error.localizedDescription)
    }
}

// This prints the following:
// TWGame(id: "509658", name: "Just Chatting", boxArtUrl: "...")
// TWGame(id: "21779", name: "League of Legends", boxArtUrl: "...")
// TWGame(id: "33214", name: "Fortnite", boxArtUrl: "...")
// ...

Available API Calls

API Method Swift Function Supported?
Get Cheermotes Twitch.Bits.getCheermotes
Get Bits Leaderboard Twitch.Bits.getBitsLeaderboard
Get Game Analytics Twitch.Analytics.getGameAnalytics
Get Extension Transactions Twitch.Extensions.getTransactions
Create Clip Twitch.Clips.createClip
Get Clips Twitch.Clips.getClips
Create Entitlement Grants Upload URL Twitch.Entitlements.createGrantsUploadUrl
Get Code Status Twitch.Entitlements.getCodeStatus
Redeem Code Twitch.Entitlements.redeemCode
Get Top Games Twitch.Games.getTopGames
Get Games Twitch.Games.getGames
Check Automod Status Twitch.Moderation.checkAutomodStatus
Get Banned Users Twitch.Moderation.getBannedUsers
Get Banned Events Twitch.Moderation.getBannedEvents
Get Moderators Twitch.Moderation.getModerators
Get Moderator Events Twitch.Moderation.getModeratorEvents
Search Categories Twitch.Search.searchCategories
Search Channels Twitch.Search.searchChannels
Get Stream Key Twitch.Streams.getStreamKey
Get Streams Twitch.Streams.getStreams
Create Stream Marker Twitch.Streams.createStreamMarker
Get Stream Markers Twitch.Streams.getStreamMarkers
Get Channel Information Twitch.Streams.getChannelInfo
Modify Channel Information Twitch.Streams.modifyChannelInfo
Get Broadcaster Subscriptions Twitch.Subscriptions.getBroadcasterSubscriptions
Get All Stream Tags Twitch.Tags.getAllStreamTags
Get Stream Tags Twitch.Tags.getStreamTags
Replace Stream Tags Twitch.Tags.replaceStreamTags
Create User Follows Twitch.Users.createUserFollows
Delete User Follows Twitch.Users.deleteUserFollows
Get Users Twitch.Clips.getUsers
Get Users Follows Twitch.Users.getFollows
Update User Twitch.Clips.updateUser
Get User Extensions Twitch.Users.getExtensions
Get User Active Extensions Twitch.Users.getUserActiveExtensions
Update User Extensions Twitch.Users.updateUserExtensions
Get Videos Twitch.Videos.getVideos
Get Webhook Subscription Twitch.Subscriptions.getWebhookSubscriptions
Get Hype Train Events Twitch.HypeTrain.getHypeTrainEvents

License

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