SmartcarAuth 5.4.1

SmartcarAuth 5.4.1

Smartcar 维护。



  • Smartcar Inc.

Smartcar iOS Auth SDK

CI Status Version License Platform

SmartcarAuth iOS SDK 使得从 iOS 集成 Smartcar Connect 更加简单。

SDK 遵循在 OAuth 2.0 for Native Apps 中规定的最佳实践,包括根据 iOS 版本来使用 SFSafariViewControllerSFAuthenticationSessionASWebAuthenticationSession 进行授权请求。出于可用性和安全原因,显式不支持 UIWebView

要求

SmartcarAuth 支持 iOS 10 及以上版本。

iOS 10 使用内置浏览器选项卡模式(通过 SFSafariViewController),iOS 11 使用 SFAuthenticationSession 在 Safari 中展示授权网页,iOS 12 及以上版本使用安全的嵌入式网页视图(通过 ASWebAuthenticationSession)。

安装

SmartcarAuth 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:

pod "SmartcarAuth"

入门

首先,您需要在您的 AppDelegate 中创建一个全局 SmartcarAuth 对象来持有会话,以便从重定向继续授权流程。

// global variable in the app's AppDelegate
var smartcarSdk: SmartcarAuth? = nil

接下来,您需要配置您的重定向 URI。您的重定向 URI 必须遵循以下格式:"sc" + clientId + "://" + hostname

如果您的应用支持iOS 10,您还需要在您的 Info.plist 中注册自定义URL方案(sc<clientId>):Info.plist

然后,在 UIViewController 中初始化 SmartcarAuth 对象。

let appDelegate = UIApplication.shared.delegate as! AppDelegate

func completionHandler(code: String?, state: String?, err: AuthorizationError?,) -> Void {
 // Receive authorization code
}

appDelegate.smartcar = SmartcarAuth(
  clientId: "afb0b7d3-807f-4c61-9b04-352e91fe3134",
  redirectUri: "scafb0b7d3-807f-4c61-9b04-352e91fe3134://exchange",
  scope: ["read_vin", "read_vehicle_info", "read_odometer"],
  completion: completionHandler
)
let smartcar = appDelegate.smartcar

// Generate a Connect URL
let authUrl = smartcar.authUrlBuilder().build()

// If you are developing for iOS 11 and above only, you can launch Connect without passing in a viewController
smartcar.launchAuthFlow(url: authUrl)

// If you are developing for iOS 10 and above, you will need to pass in a viewController
smartcar.launchAuthFlow(url: authUrl, viewController: viewController)

处理重定向

对于iOS 11及更高版本,回调URL(包含认证码或错误)由会话传递给应用,URL传送给完成处理程序,无需进一步拦截回调。

对于iOS 10,Connect响应通过iOS的openURL应用程序代理方法返回给应用,因此需要通过当前授权会话传递这一响应。

/**
	Intercepts callback from OAuth SafariView determined by the custom URI
 */
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    // Close the SFSafariViewController
    window!.rootViewController?.presentedViewController?.dismiss(animated: true , completion: nil)

    // Sends the URL to the current SmartcarAuth object which will
    // process it and then call the completion handler.
    if let sdk = smartcarSdk {
        sdk.handleCallback(url: url)
    }

    // Your additional URL handling (if any) goes here.

    return true
}

SDK参考

有关参数和可用方法的详细文档,请参阅SDK参考

作者

Smartcar Inc.,[email protected]

许可

SmartcarAuth可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。