iOS SDK integration

更新时间:
复制 MD 格式

This document describes how to integrate a Copilot application into an iOS app.

1. iOS SDK instructions

1. Example

Add dependency

Specify the Alibaba Cloud repository index.

source "https://github.com/aliyun/aliyun-specs.git"

Add the dependency.

pod 'AliyunMobiCopilot', '1.0.1'

Run pod install to apply the changes.

Use the SDK

Import the header file.

#import "AliyunMobiCopilot/AliyunMobiCopilot.h"

In the calling layer, declare that your class conforms to the AliyunMobiCopilotDelegate protocol.

@interface CopilotPage () <AliyunMobiCopilotDelegate>
    @property (nonatomic, strong) WKWebView *webView;
    @property (nonatomic, strong) AliyunMobiCopilot *aliyunMobiCopilot;
@end

Initialize the SDK.

self.aliyunMobiCopilot = [
    [AliyunMobiCopilot alloc]
    initWithWebView:self.webView
    url: @"https://runtime-stage.mobiapp.cloud/workspace171343386479015/apps/172854875547671/"
    accessToken:@""
    delegate:self
];

2. Detailed instructions

Initialization API

API definition: - (instancetype)initWithWebView:(WKWebView *)webView url:(NSString *)url accessToken:(NSString *)accessToken delegate:(id<AliyunMobiCopilotDelegate>)delegate;

Parameter definitions

Parameter

Type

Required

Description

webView

WKWebView *

Yes

The web view used to load and display content.

url

NSString *

Yes

The Copilot application URL.

accessToken

NSString *

Yes

Development environment application: You do not need to pass an accessToken.

Production environment application: Generate an accessToken by calling the GenerateNativeUserToken API and pass it to the SDK.

delegate

id<AliyunMobiCopilotDelegate>

Yes

The delegate object that handles events.

Load the Copilot application

API definition: - (void)loadCopilot;

Update the identity verification token

API definition:- (void)updateAccessToken:(NSString *)accessToken;

The accessToken has a time-to-live (TTL) of 300 seconds. Before it expires, you must obtain a new one by calling the RefreshNativeUserToken API. Then, set the new accessToken using [aliyunMobiCopilot updateAccessToken: @"nextAccessToken"];.

AliyunMobiCopilotDelegate protocol

Method name

Type

Description

-handlerMobiErrorWithCode:message:

NSString *code, NSString *message

Called when an error occurs in the Mobi environment.

-handlerMobiNavigateErrorWithRouterKey:code:message:

NSString *routerKey, NSString *code, NSString *message

Called when a navigation error occurs.

-handlerNavigateTo:routerParams:

NSString *routerKey, NSDictionary *routerParams

Called when a navigation event is triggered.

Error codes

Error code

Description

AccessTokenError

The AccessToken expired.

NavigateError

Navigation error, such as a parameter that cannot be serialized.

updateAccessTokenError

Failed to update the AccessToken.

2. iOS SDK demo project instructions

1. Create an application from a Copilot application template and publish it to the development or production environment.

2. Download the Demo repository to your local machine. The project folder is `mobi-copilot-ios-demo`. The project structure is as follows:

.
├── Podfile
├── README.md
└── mobi-copilot-ios-demo
   ├── CopilotPage
   ├── LeavePage
   ├── MeetingPage
   └── main.m

3. Obtain the application runtime information.

Obtain the application URL from the environment context.

4. Pass the application URL and accessToken as parameters to the SDK.

Configure the development environment access URL of the Copilot application in the mobi-copilot-ios-demo/CopilotPage/CopilotPage.m file of the project.

url&accessToken.jpg

Set the url and accessToken parameters.

  • Development environment application: You do not need to pass an accessToken.

    Production environment application: Generate an accessToken by calling the GenerateNativeUserToken API and pass it to the SDK.

    • The accessToken has a time-to-live (TTL) of 300 seconds. Before it expires, you must obtain a new one by calling the RefreshNativeUserToken API. Then, set the new accessToken using [aliyunMobiCopilot updateAccessToken: @"nextAccessToken"];.

5. Run the demo.

Install dependencies.

pod install

In Xcode, start the project by choosing Product > Run.

Start the application and log on to access the Copilot application in the development environment. For the production environment, use an accessToken for password-free logon.

After you access the Copilot application, you can express an intent, such as requesting leave or booking a meeting room. Copilot then sends a feature route to a native page and matches the relevant form items on that page.

6. Troubleshooting

If you encounter the following error when starting the project: Framework 'Pods_mobi_copilot_ios_demo' not found

To resolve this error, delete the Frameworks reference and start the project again.