Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesort committed Jan 21, 2020
0 parents commit eeb69ef
Show file tree
Hide file tree
Showing 23 changed files with 1,801 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store

## User settings
xcuserdata/

## Schemes
xcschemes/

## CocoaPods
Pods/
6 changes: 6 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
platform :ios, '13.0'
use_frameworks!

target 'end-user-api-demo' do
pod 'OAuthSwift', '~> 2.1.0'
end
16 changes: 16 additions & 0 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PODS:
- OAuthSwift (2.1.0)

DEPENDENCIES:
- OAuthSwift (~> 2.1.0)

SPEC REPOS:
trunk:
- OAuthSwift

SPEC CHECKSUMS:
OAuthSwift: ab1675598273e04355e0282459c328e65c9ddacd

PODFILE CHECKSUM: 0b0ca8e1954f2da6c291ff1fe6d8382630b9986d

COCOAPODS: 1.8.4
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# UserVoice End User API Demo (iOS)

A simple iOS app to demonstrate the OAuth authentication_code flow and basic functionality of the UserVoice [End User API](https://developer.uservoice.com/).

> NOTE: This app should only serve as an example for how to use the UserVoice End User API. It is not intended to highlight iOS best practices.
## Project Dependencies
* [CocoaPods](https://cocoapods.org/): Dependency manager for Swift
* [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift): Swift library for handling the OAuth2 flow

## Installation (requires Xcode)
1. Clone the repository: `git clone [email protected]:uservoice/end-user-api-ios-demo.git`.
2. Ensure CocoaPods dependency manager is installed: `sudo gem install cocoapods`.
3. Navigate into project directory and install dependencies: `cd end-user-api-ios-demo && pod install`.
4. Create an API key from the UserVoice admin console and set the "Callback URL" to `com.uv.Demo:/oauth2Callback`.
* For more information on creating an API key and implementing the OAuth flow, see [this guide](https://developer.uservoice.com/docs/end-user-api/auth/).
5. Open `end-user-api-demo.xcworkspace` in Xcode.
6. Set the environment variables referenced at the top of [UvApi.swift](https://github.com/uservoice/end-user-api-ios-demo/blob/master/end-user-api-demo/UvApi.swift) by configuring the Xcode Scheme.
7. Build and run the app.

## Resources
* [UserVoice End User API Reference](https://developer.uservoice.com/docs/end-user-api/reference/)
* [OAuth Authentication Code Flow with PKCE](https://developer.uservoice.com/docs/end-user-api/auth/)
* [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift)
682 changes: 682 additions & 0 deletions end-user-api-demo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FILEHEADER</key>
<string> ___FILENAME___
// ___PROJECTNAME___</string>
</dict>
</plist>
10 changes: 10 additions & 0 deletions end-user-api-demo.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
71 changes: 71 additions & 0 deletions end-user-api-demo/Config/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.uv.Demo</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
31 changes: 31 additions & 0 deletions end-user-api-demo/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// AppDelegate.swift
// end-user-api-demo


import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


}

56 changes: 56 additions & 0 deletions end-user-api-demo/Delegates/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SceneDelegate.swift
// end-user-api-demo

import UIKit
import OAuthSwift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
for urlContext in URLContexts {
print(urlContext.url)

OAuthSwift.handle(url: urlContext.url)
}
}

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}

func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}

func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}

func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}

func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}

func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}


}

65 changes: 65 additions & 0 deletions end-user-api-demo/Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Extensions.swift
// end-user-api-demo

import Foundation
import CommonCrypto

// PKCE flow string helpers
extension String {
func urlBase64EncodedString() -> String {
return Data(self.utf8)
.base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
}

func sha256() -> String {
if let stringData = self.data(using: String.Encoding.utf8) {
return hexStringFromData(input: digest(input: stringData as NSData))
}
return ""
}

private func digest(input : NSData) -> NSData {
let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
var hash = [UInt8](repeating: 0, count: digestLength)
CC_SHA256(input.bytes, UInt32(input.length), &hash)
return NSData(bytes: hash, length: digestLength)
}

private func hexStringFromData(input: NSData) -> String {
var bytes = [UInt8](repeating: 0, count: input.length)
input.getBytes(&bytes, length: input.length)

var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}

return hexString
}
}

// Param helpers
extension Dictionary {
func percentEncoded() -> Data? {
return map { key, value in
let escapedKey = "\(key)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
let escapedValue = "\(value)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
return escapedKey + "=" + escapedValue
}
.joined(separator: "&")
.data(using: .utf8)
}
}
extension CharacterSet {
static let urlQueryValueAllowed: CharacterSet = {
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="

var allowed = CharacterSet.urlQueryAllowed
allowed.remove(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")
return allowed
}()
}
Loading

0 comments on commit eeb69ef

Please sign in to comment.