Flutter企业级安全管理插件workspaceone_sdk_flutter的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter企业级安全管理插件workspaceone_sdk_flutter的使用

使用本文档安装用于Flutter的Workspace One SDK插件。该插件帮助企业应用开发者为移动应用程序添加企业级安全、条件访问和合规性功能。

包安装

将插件作为依赖项添加到应用的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
    
  workspaceone_sdk_flutter:^24.12.0

在终端中运行以下命令以获取依赖项:

$  dart pub get

支持的组件

该插件适用于以下组件版本:

  • Workspace ONE UEM控制台 2306+(具体功能可能需要更高的版本)
  • Android 7.0+(对于Android SDK组件)/ API级别 24及以上/ Android Studio 7.4+ 或更高版本/ Workspace ONE Intelligent Hub for Android 版本 24.11 或更高版本
  • iOS 和 iPadOS 15+ 或更高版本(对于iOS SDK组件)/ Xcode 15.0.1+ 或更高版本

初始设置

额外设置

iOS

  1. 通过Swift包管理器添加AWSDK。 点击这里了解如何通过Swift包管理器集成AWSDK框架。

  2. 在AppDelegate中添加以下代码:

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  // 添加以下代码以发布URL通知
  NSNotification *info = [[NSNotification alloc]initWithName:@"UIApplicationOpenURLOptionsSourceApplicationKey" object:url userInfo:options];
  [[NSNotificationCenter defaultCenter] postNotification:info];
  
  return YES;
}
  1. 在Podfile中添加安装脚本:
post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
  end
  # 添加此行以从SPM获取AWSDK Swift包
  $workspaceone_sdk_flutter.post_install(installer)
end

Android

  1. 修改AndroidManifest.xml文件以设置主启动器:
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
    </activity>
    <activity
        android:name="com.airwatch.login.ui.activity.SDKSplashActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter>
    </activity>
  1. 更新你的主活动类:
import com.workspaceone_sdk_flutter.WorkspaceOneSdkActivity
class MainActivity: WorkspaceOneSdkActivity() {
}
  1. 添加WS1EventImpl类:
import android.content.Context
import android.os.Bundle
import android.util.Log
import com.airwatch.sdk.profile.AnchorAppStatus
import com.airwatch.sdk.profile.ApplicationProfile
import com.airwatch.sdk.shareddevice.ClearReasonCode
import com.airwatch.event.WS1AnchorEvents
import org.koin.core.component.KoinComponent

class WS1EventImpl : WS1AnchorEvents, KoinComponent {
    override fun onApplicationConfigurationChange(bundle: Bundle?, context: Context) {}

    override fun onApplicationProfileReceived(context: Context, s: String, applicationProfile: ApplicationProfile) {
        Log.d("SDK Init", "onApplicationProfileReceived")
    }

    override fun onClearAppDataCommandReceived(context: Context, clearReasonCode: ClearReasonCode) {
        Log.d("SDK Init", "onClearAppDataCommandReceived")
    }

    override fun onAnchorAppStatusReceived(context: Context, anchorAppStatus: AnchorAppStatus) {}

    override fun onAnchorAppUpgrade(context: Context, b: Boolean) {}
}
  1. 更新你的Android应用子类如下:
    • 声明该类实现WorkspaceOneSDKApplication接口。
    • onCreate()方法中的代码移到onPostCreate()方法中。
    • 覆写getMainActivityIntent()方法以返回指向应用程序主活动的Intent。
    • 覆写以下Android应用方法:
      • attachBaseContext
import com.workspaceone_sdk_flutter.WorkspaceOneSdkApplication
class MainApplication : WorkspaceOneSdkApplication() {

    // 应用程序特定的覆盖:注释掉onCreate()并将代码移至onPostCreate()

    // @Override
    // public void onCreate() {
    //    super.onCreate();
    // }

    // 应用程序特定的覆盖:将所有onCreate()中的代码复制到onPostCreate()
    override fun onPostCreate() {
        super.onPostCreate()
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        attachBaseContext(this)
    }

    override fun getMainActivityIntent(): Intent {
        return Intent(this, MainActivity::class.java)
    }

    override fun getEventHandler(): WS1AnchorEvents {
        return WS1EventImpl()
    }
}

更多关于Flutter企业级安全管理插件workspaceone_sdk_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter企业级安全管理插件workspaceone_sdk_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用workspaceone_sdk_flutter插件进行企业级安全管理的代码案例。这个插件通常用于集成VMware Workspace ONE进行设备管理、身份验证等。

1. 添加依赖

首先,你需要在pubspec.yaml文件中添加workspaceone_sdk_flutter依赖:

dependencies:
  flutter:
    sdk: flutter
  workspaceone_sdk_flutter: ^最新版本号 # 请替换为最新的版本号

然后运行flutter pub get来安装依赖。

2. 导入插件

在你的Dart文件中导入插件:

import 'package:workspaceone_sdk_flutter/workspaceone_sdk_flutter.dart';

3. 初始化SDK

在你的应用启动时(通常在main.dart文件的MyApp类中),初始化Workspace ONE SDK:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _initializeWorkspaceOne();
  }

  Future<void> _initializeWorkspaceOne() async {
    try {
      // 初始化Workspace ONE SDK,这里可能需要提供配置参数,具体参考官方文档
      await WorkspaceOneSdk.initialize(
        config: WorkspaceOneConfig(
          // 示例配置,请根据实际情况替换
          sdkUrl: 'https://your-workspace-one-sdk-url',
          clientId: 'your-client-id',
          clientSecret: 'your-client-secret',
        ),
      );
      print('Workspace ONE SDK initialized successfully');
    } catch (e) {
      print('Failed to initialize Workspace ONE SDK: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Workspace ONE Integration'),
        ),
        body: Center(
          child: Text('Checking Workspace ONE integration...'),
        ),
      ),
    );
  }
}

4. 进行身份验证

你可以使用Workspace ONE SDK进行身份验证。以下是一个简单的示例:

Future<void> _authenticateWithWorkspaceOne() async {
  try {
    // 发起身份验证请求
    var authResult = await WorkspaceOneSdk.authenticate();
    if (authResult.isSuccess) {
      print('Authenticated successfully: ${authResult.userInfo}');
      // 处理成功认证后的逻辑
    } else {
      print('Authentication failed: ${authResult.errorMessage}');
      // 处理认证失败后的逻辑
    }
  } catch (e) {
    print('An error occurred during authentication: $e');
  }
}

你可以在按钮点击事件中调用这个方法:

body: Center(
  child: ElevatedButton(
    onPressed: _authenticateWithWorkspaceOne,
    child: Text('Authenticate with Workspace ONE'),
  ),
),

5. 处理设备管理和策略

Workspace ONE SDK还提供了设备管理和策略功能,你可以根据需求调用相应的API。例如,检查设备是否符合公司策略:

Future<void> _checkCompliance() async {
  try {
    var complianceResult = await WorkspaceOneSdk.checkCompliance();
    if (complianceResult.isCompliant) {
      print('Device is compliant');
      // 处理设备合规的逻辑
    } else {
      print('Device is not compliant: ${complianceResult.nonComplianceReasons}');
      // 处理设备不合规的逻辑
    }
  } catch (e) {
    print('An error occurred during compliance check: $e');
  }
}

同样,你可以在UI中添加一个按钮来触发这个检查:

body: Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    ElevatedButton(
      onPressed: _authenticateWithWorkspaceOne,
      child: Text('Authenticate with Workspace ONE'),
    ),
    SizedBox(height: 20),
    ElevatedButton(
      onPressed: _checkCompliance,
      child: Text('Check Device Compliance'),
    ),
  ],
),

注意

  1. 实际配置:上面的代码中WorkspaceOneConfig的参数需要根据你的实际Workspace ONE配置进行替换。
  2. 错误处理:在生产环境中,你需要更细致地处理错误和异常情况。
  3. 文档参考:具体API和功能请参考Workspace ONE SDK for Flutter的官方文档

这个示例展示了如何在Flutter应用中使用workspaceone_sdk_flutter插件进行基本的初始化、身份验证和设备合规性检查。根据你的具体需求,你可以进一步扩展这些功能。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!