MBurger
MBurger 是一个客户端库,使用 Objective-C 编写,可以用于与 MBurger API 交互。库的最小部署目标为 iOS 10.0。
虽然这是一个使用 Objective-C 编写的库,但它也可以在 Swift 项目中集成和使用。下面提供的示例代码将在两种语言中显示。
安装
使用 CocoaPods 安装
CocoaPods 是一个用于 iOS 的依赖关系管理器,它自动化和简化了在项目中使用第三方库的过程。您可以运行以下命令来安装 CocoaPods
$ gem install cocoapods
要使用 CocoaPods 将 MBurger 集成到您的 Xcode 项目中,在 Podfile 中指定它
platform :ios, '10.0'
target 'TargetName' do
pod 'MBurger', git: 'https://github.com/Mumble-SRL/MBurger-iOS'
end
如果您使用 Swift,请在 pod 声明之前添加 use_frameworks!
。
然后,运行以下命令
$ pod install
CocoaPods 是安装库的首选方法。
手动安装
要手动安装库,将文件夹 MBurger
拖到 XCode 项目结构中。
请注意,MBurger
依赖于 AFNetworking (3.0)
和 SAMKeychain (1.5)
,因此您也必须安装这些库。
初始化
要初始化SDK,您必须通过仪表板创建一个令牌。点击右上角的设置图标,创建一个API密钥并指定权限。
然后,在您的AppDelegate
中的application:didFinishLaunchingWithOptions:
初始化SDK的MBManager
,并设置一个令牌,如下所示
objective-c:
#import "AppDelegate.h"
#import "MBurger.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[MBManager sharedManager].apiToken = @"YOUR_API_TOKEN";
return YES;
}
swift:
import MBurger
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
MBManager.shared().apiToken = "YOUR_API_TOKEN"
return true
}
如果您不使用正确的令牌/密钥初始化,将无法与SDK交互。
使用
使用MBClient
类发送所有请求到API以从MBurger获取数据。所有API调用都有一个复数形式和其单数对应形式。例如,您可以获取项目块的列表,也可以通过ID获取单个块。
项目
您可以通过以下方式检索项目信息
objective-c:
[MBClient getProjectWithSuccess:^(MBProject *project) {
} Failure:^(NSError *error) {
NSLog(@"There was an error: %@", error.localizedDescription);
}];
swift:
MBClient.getProjectWithSuccess({ (project) in
}, failure: { (error) in
})
块
您可以使用函数getBlocksWithParameters:Success:Failure
获取项目的块,如下所示
objective-c:
[MBClient getBlocksWithParameters:nil Success:^(NSArray<MBBlock *> *blocks, MBPaginationInfo *pagintaionInfo) {
} Failure:^(NSError *error) {
}];
swift:
MBClient.getBlocksWith(nil, success: { (blocks, paginationInfos) in
}, failure: { (error) in
})
parameters
参数是一个可选的对象数组,该对象符合MBParameter
协议,并将其作为参数传递给MBurger API。大多数可以传递给API的参数已经在SDK中定义,并在初始化后使用
MBSortParameter
MBPaginationParameter
MBFilterParameter
MBGeofenceParameter
如果您想传递另一种类型的参数,可以使用MBGeneralParameter
类,该类可以通过一个键和一个值初始化,该值将作为参数传递给API。
因此,如果要包含分页参数,可以这样做
objective-c:
MBPaginationParameter *paginationParam = [[MBPaginationParameter alloc] initWithSkip:0 Take:10];
[MBClient getBlocksWithParameters:@[paginationParam] Success:^(NSArray<MBBlock *> *blocks, MBPaginationInfo *pagintaionInfo) {
} Failure:^(NSError *error) {
}];
swift:
let paginationParam = MBPaginationParameter(skip: 0, take: 10)
MBClient.getBlocksWith([paginationParam], success: { (blocks, paginationInfos) in
}, failure: { (error) in
})
存在两种其他版本的 getBlocksWithParameters:Success:Failure
,一种接受一个额外的参数 includingSections
(一个布尔值,指示是否包含每个块中的部分),另一种接受 includingSections
和 includeElements
(一个布尔值,对部分元素做同样的事情)。
因此,您可以通过此调用检索所有块的信息,所有块的章节以及章节的所有元素
objective-c:
[MBClient getBlocksWithParameters:nil IncludingSections:TRUE AndElements:TRUE Success:^(NSArray<MBBlock *> *blocks, MBPaginationInfo *pagintaionInfo) {
} Failure:^(NSError *error) {
}];
swift:
MBClient.getBlocksWith(nil, includingSections: true, andElements: true, success: { (blocks, paginationInfos) in
}, failure: { (error) in
})
章节
您可以使用函数 getBlocksWithParameters:Success:Failure
通过给定的 id 来检索一个块的 所有章节,如下所示
objective-c:
[MBClient getSectionsWithBlockId:THE_BLOCK_ID Parameters:nil Success:^(NSArray<MBSection *> *sections, MBPaginationInfo *pagintaionInfo) {
} Failure:^(NSError *error) {
}];
swift:
MBClient.getSectionsWithBlockId(THE_BLOCK_ID, parameters: nil, success: { (sections, paginationInfos) in
}, failure: { (error) in
})
类似于块,此函数有一个接受布尔 includeElements
的版本,该布尔值指示是否包括章节的元素,因此如果要检索块的章节及其元素,可以调用:
objective-c:
[MBClient getSectionsWithBlockId:THE_BLOCK_ID IncludeElement:TRUE Parameters:nil Success:^(NSArray<MBSection *> *sections, MBPaginationInfo *pagintaionInfo) {
} Failure:^(NSError *error) {
}];
swift:
MBClient.getSectionsWithBlockId(THE_BLOCK_ID, parameters: nil, includeElements: true, success: { (sections, paginationInfos) in
}, failure: { (error) in
})
对象映射
MBSection
类有一个商用函数,可以用来将章节的元素映射到您自己创建的定制对象。例如,如果您有一个 News
对象,如下所示:
#import <Foundation/Foundation.h>
@interface News : NSObject
@property NSString *title;
@property NSString *content;
@property NSURL *imageUrl;
@property NSString *link;
@end
并且 MBurger 中的一个块代表新闻源,您可以为新闻对象创建并填充数组
NSInteger newsBlockId = 12;
NSDictionary *mappingDictionary = @{@"title" : @"title",
@"content" : @"content",
@"image.firstImage.url" : @"imageUrl",
@"link" : @"link"};
NSMutableArray *newsArray = [[NSMutableArray alloc] init];
[MBClient getBlockWithBlockId:newsBlockId Parameters:nil IncludingSections:YES AndElements:YES Success:^(MBBlock *block) {
NSMutableArray *newsArray = [[NSMutableArray alloc] init];
for (MBSection *section in block.sections){
News *n = [[News alloc] init];
[section mapElementsToObject:n withMapping:mappingDictionary];
[newsArray addObject:n];
}
} Failure:^(NSError *error) {
[self showError:error];
}];
如示例所示,您可以使用点记法指向对象的属性。如果没有定义任何属性,SDK 将使用元素对象的值(调用值函数)。
示例项目中有完整的示例。
序列化
所有模型对象都实现了 NSCoding
和 NSSecureCoding
协议,因此您可以在不实现它的情况下对其进行序列化和反序列化。以下是实现这些协议的对象列表:
MBProject
MBBlock
MBSection
MBElement
MBTextElement
MBImagesElement
MBImage
MBMediaElement
MBFile
MBCheckboxElement
MBWysiwygElement
MBDateElement
MBAddressElement
MBDropdownElement
MBPollElement
MBColorElement
MBRelationElement
MBGeneralElement
MBUser
相等
所有模型对象均根据相应的id实现了isEqual:
函数。因此,例如,当两个MBSection对象的sectionId
相同时,一个MBSection对象将等于另一个MBSection对象。
管理员
请在此处阅读完整的管理员文档API:https://github.com/Mumble-SRL/MBurger-iOS/tree/master/MBurger/MBAdmin。
认证
请在此处阅读完整的认证文档API:https://github.com/Mumble-SRL/MBurger-iOS/tree/master/MB Burger/MBAuth。
推送通知
要将在您的MBurger项目中集成推送通知,请查看Aur推送框架MPushSwift。
插件
您可以通过插件为MBurger添加更多功能,插件是符合MPPlugin
协议的类,可以扩展MBurger的功能。一个插件的例子是MPPayments,一个插件可以让您向用户收取单次支付或订阅费用。
文档
如需更多信息,请在/docs文件夹中查看完整的SDK参考。
联系方式
您可以通过以下邮箱联系我们:[email protected]。
许可协议
MBurger采用Apache 2.0许可证发布。详细信息请参阅LICENSE文件。