基本上,我想設定一個 ArcGIS 帳戶來訪問我在 IOS 中的 API 密鑰。如果我沒有帳戶,它會免費為我注冊。到目前為止,我已經實作了以下目標:
我注冊了我的應用程式以生成客戶端 ID 并設定重定向 URI 以訪問安全服務。然后,我向名為 AppConfiguration 的 Xcode 專案添加了一個新的 swift 檔案。
我用它來定義一個結構來保存我的應用程式所需的配置常量。
然后我將以下結構添加到 AppConfiguration.swift。當我輸入此代碼時,將“YOUR-APP-CLIENT-ID”更改為我已經在上一步的應用程式定義中設定的客戶端 ID。
它將更新 URL 方案和路徑以匹配我上一步中的重定向 URI 條目。下面是我使用的代碼:
struct AppConfiguration {
static let trafficLayerURL = URL(string: "https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer")!
static let clientID: String = "YOUR-APP-CLIENT-ID"
static let urlScheme: String = "my-app"
static let urlAuthPath: String = "auth"
static let keychainIdentifier: String = "\(Bundle.main.bundleIdentifier!).keychainIdentifier"
}
之后,我用我的應用程式定義的身份驗證選項卡上顯示的值替換了 client_id 和 redirect_url 值。
最后,我打開 ViewController.swift 并更新現有的 setupMap() 方法以將交通影像層添加到地圖中。
畢竟,在這一點上,當我運行并測驗我的應用程式時。底圖應該加載,但交通層不會加載。任何想法出了什么問題或缺少什么?
uj5u.com熱心網友回復:
底圖應該加載,但流量層不會加載,直到您能夠使用 AGSAuthenticationManager 登錄用戶并獲得有效的訪問令牌。將 OAuth 2.0 集成到您的應用程式中
- 為您的應用配置重定向 URL 方案。在 Project Navigator 中右鍵單擊 info.plist 檔案,然后選擇 Open As > / Source Code。在打開的頂級標記之后編輯檔案并添加以下 XML:
請務必為您的應用使用確切的捆綁識別符號。
使用您在應用程式定義中配置的重定向 URI 的方案部分,但不包含路徑(:// 之前的所有內容)。
這就是 iOS 中的 OAuth 2.0 能夠將有關身份驗證程序的資訊回傳給您的應用程式的方式。請注意,這些字串必須完全匹配。
<dict>
<!-- *** ADD *** -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.esri.your-bundle-identifier</string>
<key>CFBundleURLSchemes</key>
<array>
<string>my-app</string>
</array>
</dict>
</array>
打開 AppDelegate.swift 在你的 AppDelegate 中設定 AGSAuthenticationManager。這樣做是因為一旦 OAuth 2.0 完成,iOS 將使用 UIApplication.OpenURLOptionsKey 將控制權委托給您。匯入 ArcGIS 庫:
匯入 UIKit /*** 添加 ***/ 匯入 ArcGIS
創建一個新函式來設定身份驗證管理器。此代碼使用您在 AppConfiguration 中分配給應用程式的引數創建配置,然后將該配置分配給 AGSAuthenticationManager。憑據也保存在設備的鑰匙串中。
private func setupOAuthManager() { let config = AGSOAuthConfiguration(portalURL: nil, clientID:AppConfiguration.clientID, redirectURL: "(AppConfiguration.urlScheme)://(AppConfiguration.urlAuthPath)")
AGSAuthenticationManager.shared().oAuthConfigurations.add(config) AGSAuthenticationManager.shared().credentialCache.enableAutoSyncToKeychain(withIdentifier:AppConfiguration.keychainIdentifier,accessGroup:nil,crossDevices:false)}
- OAuth 2.0 完成后,iOS 將使用重定向 URI 將控制權回傳給您的應用程式。在這里,您確保入站 URL 是您的應用程式的重定向 URL。使用 AGSApplicationDelegate 打開入站 OAuth 2.0 重定向。
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), AppConfiguration.urlScheme == urlComponents.scheme, AppConfiguration.urlAuthPath == urlComponents.host { AGSApplicationDelegate.shared().application(app, open: url, options: options) } return true }
- 從應用程式啟動添加對 setupOAuthManager() 的呼叫:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool { /*** ADD ***/ setupOAuthManager()
- 按 Command-R 在 iOS 模擬器中運行應用程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/336457.html
