Skip to content

jackrwright/PlexAPI

Repository files navigation

PlexAPI

This Swift package provides code to authentic with a user's Plex.tv account to retrieve the user's access token. This token can be used with Plex Personal Media Server API calls.

For secure token storage, it uses the KeychainWrapper class, written by Jason Rendel. For simplicity, its source is manually included in this package.

Example Usage

This follows the authetication procedure for an app as recommended by this Plex article.

Start the sign-in process:

var isSigningIn: Bool = false {
    didSet {
        if !isSigningIn {
	        // stop polling
	        self.timer?.invalidate()
        }
    }
}

@IBAction func signInTapped(_ sender: UIButton) {
	...
	
	isSigningIn = true
	
	// The following call requests a time-sensistive pin that's used to construct a Plex auth app URL.
	// The pin is saved for use later to verify sign-in.
	PlexAPI.requestToken { (url, error) in
	    
	    if let error = error {
	    
	       // handle the error...
	          
	    } else {
	        
	        if let url = url {
	            
	            // open the url returned in a browser.
	            // The browser is presented on top of your app where the user
	            // has the opportunity to sign into their Plex.tv account.
	            
	            DispatchQueue.main.async {
	                
	                self.safariVC = SFSafariViewController(url: url)
	                
	                self.present(self.safariVC!, animated: true, completion: nil)
	            }
	        }
	    }
	}
}	

When the browser is dismissed by the user, your app needs to poll to verify the pin has been claimed. Since the browser was presented modally on top of your app, this can be done in viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
    
    super.viewDidAppear(animated)
    
    if isSigningIn {
        
        self.pollForAuthToken()
    }
}

private var timer: Timer?
    
/// Poll periodically for a valid auth token to show up
private func pollForAuthToken() {
    
    timer =  Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
        
        PlexAPI.checkForAuthToken()
    }
}

When PlexAPI.checkForAuthToken() gets a valid auth token, it posts a notification that your app should listen for. This can be set up in viewDidLoad:

override func viewDidLoad() {
	    
	super.viewDidLoad()
	...
	// Register for a notification of when we received an auth token
	Foundation.NotificationCenter.default.addObserver(self,
	                                                  selector: #selector(didSignIn(_:)),
	                                                  name: NSNotification.Name(rawValue: PlexAPI.signedIntoPlex),
	                                                  object: nil)
	...
}   
                                                  
/// This method is called in response to the PlexAPI.signedIntoPlex notification
@objc private func didSignIn(_ notification: Notification) {
    
    DispatchQueue.main.async {
        
        // stop polling
        self.isSigningIn = false

        // The token has been saved securely in the user's keychain,
        // and can be retrieved with this call:
        print(PlexAPI.savedToken ?? "no token saved")
    }
}
  

An example iPhone project can be found here that demonstrates the usage.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages