ANAChat iOS
强大的 ANAChat 允许您将 ANA 聊天机器人集成到您的应用中。根据您应用的色调自定义 UI,然后您就准备好了。就这么简单!
入门指南
ANAChat 可以通过 CocoaPods 直接安装到您的应用中,或者直接导入源代码文件。请注意,ANAChat 直接依赖于 FCM,这是构建组件所必需的。
如果您还没有 Xcode 项目,请现在创建一个。
CocoaPods 安装
我们建议使用 CocoaPods 安装库。您可以通过遵循 安装说明 来安装 Cocoapods。
-
现在,在项目目录的根目录中创建一个
Podfile
文件,并添加以下内容$ cd your-project directory $ pod init
-
添加您想要安装的 pods。您可以在 Podfile 中像这样包含一个 Pod
use_frameworks! pod 'ANAChat' pod 'Firebase/Messaging'
-
上一步将FCM文件下载到应用中,这些文件应使用FCM进行配置。请遵循以下帮助文档中提到的所有步骤,除了“添加SDK”部分。 FCM文档
-
在FCM配置修改完成后,请在
AppDelegate
中修改以下方法
Swift:在新创建的AppDelegate
中导入ANAChat模块
import ANAChat
修改以下方法的实现
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
......
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
return true
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
AppLauncherManager.didReceiveFcmToken(withToken: fcmToken, baseAPIUrl: "#baseUrl", businessId: "businessID")
}
func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
Messaging.messaging().appDidReceiveMessage(userInfo)
AppLauncherManager.didReceiveRemoteNotification(userInfo)
completionHandler(.newData)
}
public func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage){
AppLauncherManager.didReceiveRemoteNotification(remoteMessage.appData)
}
Objective-C
在AppDelegate
中导入ANAChat模块
@import ANAChat;
修改以下方法的实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
......
[FIRMessaging messaging].delegate = self;
[FIRMessaging messaging].shouldEstablishDirectChannel = YES;
}
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
if (fcmToken.length > 0){
[AppLauncherManager didReceiveFcmTokenWithToken:fcmToken baseAPIUrl:@"#baseUrl" businessId:@"#businessID"];
}
}
- (void)messaging:(nonnull FIRMessaging *)messaging didReceiveMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage{
[AppLauncherManager didReceiveRemoteNotification:remoteMessage.appData];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{
[AppLauncherManager didReceiveRemoteNotification:userInfo];
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[FIRMessaging messaging].APNSToken = deviceToken;
}
-
通过以
源代码
方式打开,将以下权限添加到Target的info.plist
文件中<key>NSMicrophoneUsageDescription</key> <string>This app would like to use microphone</string> <key>NSAppleMusicUsageDescription</key> <string>This app would like to use music</string> <key>NSCameraUsageDescription</key> <string>This app would like to use camera</string> <key>NSLocationAlwaysUsageDescription</key> <string>Will you allow this app to always know your location?</string> <key>NSLocationWhenInUseUsageDescription</key> <string>Do you allow this app to know your current location?</string> <key>NSPhotoLibraryUsageDescription</key> <string>This app would like to use photo</string>
如果您尚未添加NSAppTransportSecurity,请添加以下权限
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
- 您现在可以导入ANAChat并在任何地方使用以下代码使用ANA Chat SDK
Swift
let storyboard = UIStoryboard(name: "SDKMain", bundle: CommonUtility.getFrameworkBundle())
let controller = storyboard.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
controller.businessId = "#businessID"
controller.flowId = "#flowID";
controller.baseAPIUrl = "#baseUrl"
controller.headerTitle = "Chatty"
controller.baseThemeColor = UIColor.init(red: 0.549, green: 0.784, blue: 0.235, alpha: 1.0)
controller.headerLogoImageName = "chatty"
self.navigationController?.pushViewController(controller, animated: true)
Objective-C
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SDKMain" bundle:[CommonUtility getFrameworkBundle]];
ChatViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"ChatViewController"];
controller.businessId = @"#businessID";
controller.flowId = @"landingpage";
controller.baseAPIUrl = @"#baseUrl";
controller.headerTitle = @"Chatty";
controller.baseThemeColor = [UIColor colorWithRed:0.549 green:0.784 blue:0.235 alpha:1.0];
controller.headerLogoImageName = @"chatty";
[self.navigationController pushViewController:controller animated:YES];
- 如果您想支持位置输入类型,可以在项目中包含一个
GooglePlacePicker
,并参考Google Places文档(遵循链接直至第4步)。将地点集成到应用程序后,按照以下步骤完成安装。
Swift:在您初始化SDK代码的模块中导入GooglePlacePicker
import GooglePlacePicker
连接到ChatViewControllerDelegate
并实现以下方法
controller.delegate = self
将以下代码复制到您的模块
func presentLocationPopupOnViewController(_ vc: UIViewController){
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePicker(config: config)
placePicker.pickPlace(callback: {(place, error) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let latitude = place?.coordinate.latitude, let longitude = place?.coordinate.longitude{
let alertController: UIAlertController = UIAlertController(title: "Alert", message: "Do you want to share the selected location?", preferredStyle: .alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
}
alertController.addAction(cancelAction)
let okAction: UIAlertAction = UIAlertAction(title: "Ok", style: .default) { action -> Void in
let locationInfo:[String: String] = ["latitude": String(latitude), "longitude" : String(longitude)]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kLocationReceivedNotification"), object: nil, userInfo: locationInfo)
}
alertController.addAction(okAction)
vc.present(alertController, animated: true, completion: nil)
}
})
}
Objective-C:在初始化SDK代码的模块中导入GooglePlacePicker
@import GooglePlacePicker;
连接到ChatViewControllerDelegate
并实现以下方法
controller.delegate = self
将以下代码复制到您的模块
- (void)presentLocationPopupOnViewController:(UIViewController * _Nonnull)vc {
GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:nil];
GMSPlacePicker *placePicker = [[GMSPlacePicker alloc] initWithConfig:config];
[placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(@"Pick Place error %@", [error localizedDescription]);
return;
}
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Alert"
message:@"Do you want to share the selected location?"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if(place.coordinate.latitude && place.coordinate.longitude){
[[NSNotificationCenter defaultCenter] postNotificationName:
@"kLocationReceivedNotification" object:nil userInfo:
@{@"latitude" : [NSString stringWithFormat:@"%f",place.coordinate.latitude],
@"longitude" : [NSString stringWithFormat:@"%f",place.coordinate.longitude]}];
}
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[vc presentViewController:alert animated:YES completion:nil];
}];
}
源代码安装
如果您希望从源代码直接将ANAChat安装到应用程序中,则需克隆存储库并将代码和资源添加到您的应用程序
-
克隆存储库并浏览到ANAChat-iOS-master/ANAChat/,然后将在项目中拖放到类文件夹,并指示Xcode将其复制到目标组的文件夹中。如果您正在将文件导入Objective-C项目,请在添加文件后运行项目。
-
要使用此SDK需要配置FCM,请查看以下文档进行安装和配置:这里
-
按照上述步骤从4到8完成安装。
如果您正在导入Objective-C项目,将import语句替换为"Your-Project-Name-Swift.h"。如果您正在导入Swift项目,则不需要导入语句。
注意
- 使用上述代码时,请确保使用有效的businessID和baseAPIUrl。
- 上述代码用于推送ChatViewController,您可以根据需求使用ChatViewController。
- 在fcm-plugin上配置FCM服务器密钥以将推送通知发送到应用。请按照以下步骤获取FCM服务器密钥。
- 在FCM控制台
- 点击与项目名称旁边的设置图标/齿轮在新的Firebase控制台顶部
- 点击项目设置
- 点击云消息选项卡
- 密钥在服务器密钥下面
- 只有当您想支持输入类型位置时,才遵循第7步。
许可证
ANAChat 可在 GNU GPLv3 许可证 下使用。