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.

Packages

 
 
 

Contributors