SDK 旨在轻松将 BLiP 聊天功能添加到您的 iOS 应用中。更多信息请参阅 BLiP 门户 和 BLiP 文档。
要使用 Blip SDK for iOS,您的目标必须为 iOS 10 或更高版本。
通过 CocoaPods 将 Blip SDK for iOS 导入到您的项目中
如果您尚未安装 CocoaPods,请通过运行以下命令安装它:
$ [sudo] gem install cocoapods
$ pod setup
在您的项目目录中创建一个名为 Podfile
的纯文本文件(不要任何文件扩展名)。将以下行添加到您的文件中,并用您的实际目标名称替换 YourTarget
。
target 'YourTarget' do
use_frameworks!
pod "BlipSDK"
end
运行以下命令。
$ pod install
使用 Xcode 打开 *.xcworkspace
并开始使用 SDK。
注意:请勿使用 *.xcodeproj
。如果您打开项目文件而不是工作区,将会收到错误。
在您的项目中添加了 SDK 引用后,您需要在 BLiP 门户 上获取您的 API 密钥。转到左侧菜单并访问 Publications > Blip Chat
。
要使用位置信息卡,在 info.plist 文件中设置位置服务的使用描述密钥。使用密钥 Privacy - Location When In Use Usage Description 并设置一条消息,请求用户允许使用位置信息。
导入 BlipSKD
Swift
import BlipSDK
Objective-C
#import "BlipSDK/BlipSDK-Swift.h"
打开一个新的帖子非常简单。使用 BlipClient 辅助类并调用 openBlipThread 方法。
Swift
BlipClient.openBlipThread(myView: self, apiKey: "your-api-key", options: nil)
Objective-C
[BlipClient openBlipThreadWithMyView:self apiKey:(NSString*) @"your-api-key" options:options error: nil];
注意:在 Objective-C 中,方法名称是 openBlipThreadWithMyView
例如,假设当您的 ViewController 加载时,您想在您的客户和您的聊天机器人之间建立一个新的对话。
Swift
import UIKit
import WebKit
import BlipSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
do {
try BlipClient.openBlipThread(myView: self, apiKey: "your-api-key", options: nil)
} catch {
print (error.localizedDescription)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Objective-C
#import "ViewController.h"
#import "BlipSDK/BlipSDK-Swift.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear: animated];
[BlipClient openBlipThreadWithMyView:self apiKey:(NSString*) @"your-api-key" options:nil error: nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
BLiP iOS SDK支持三种不同的用户身份验证类型。可以定义BLiP SDK将使用哪种身份验证方法来识别您的客户端。
userIdentifier
、userPassword
、userName
和userEmail
。UserIdentifier
和userPassword
是必需的
。在此模式下,用户有消息历史。要定义使用的用户身份验证类型,请在BlipOptions的authType属性中使用AuthTypeProvider.AuthType枚举。当使用Swift时,authType的可能值有:.Guest
、.Login
和.Dev
。当使用Objective-C时,可能的值有:AuthTypeGuest
、AuthTypeLogin
和AuthTypeDev
。
注意:如果您没有定义'authType',默认将使用访客类型。
Swift
let options = BlipOptions(authType: .Dev,
userIdentifier: "user-identifier",
userPassword: "user-password",
userName: "user-name",
userEmail: "user-email")
Objective-C
BlipOptions *options = [[BlipOptions alloc] init];
options.authType = AuthTypeDev;
options.userIdentifier = @"user-identifier";
options.userPassword = @"user-password";
options.userName = @"user-name";
options.userEmail = @"user-email";
在iOS中,您可以设置聊天视图的标题。此标题将在ModalView的顶部显示。
Swift
let options = BlipOptions()
options.windowTitle = "Window Title"
Objective-C
BlipOptions *options = [[BlipOptions alloc] init];
options.windowTitle = @"Window Title";
Swift
import UIKit
import WebKit
import BlipSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
let options = BlipOptions(authType: .Dev,
userIdentifier: "user-identifier",
userPassword: "user-password",
userName: "user-name",
userEmail: "user-email")
options.windowTitle = "window-title"
do {
try BlipClient.openBlipThread(myView: self, apiKey: "your-api-key", options: options)
} catch {
print (error.localizedDescription)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Objective-C
#import "ViewController.h"
#import "BlipSDK/BlipSDK-Swift.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear: animated];
BlipOptions *options = [[BlipOptions alloc] init];
options.authType = AuthTypeDev;
options.userIdentifier = @"user-identifier";
options.userPassword = @"user-password";
options.userName = @"user-name";
options.userEmail = @"user-email";
options.windowTitle = @"window-title";
[BlipClient openBlipThreadWithMyView:self apiKey:(NSString*) @"your-api-key" options:options error: nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
A -> dyld: 无法加载库:@rpath/libswiftCore.dylib / 没有找到图像
Objective-C项目可能会提示库加载错误。
如果出现这种情况,请按照以下步骤操作
@executable_path/Frameworks
your_target -> Build Settings -> Linking -> Runpath Search Paths
Yes
your_target -> Build Settings -> Build Options -> Always Embed Swift Standard Libraries
有关更详细的解释,请参阅stackoverflow上的这个主题:http://stackoverflow.com/questions/26104975/dyld-library-not-loaded-rpath-libswiftcore-dylib-image-not-found
B -> 应用传输安全已阻止HTTP (http://) 资源
如果出现这种情况,您有两个选择
只在聊天机器人中使用HTTPS资源
将密钥NSAppTransportSecurity添加到您的app info.plist中。
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.