测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可协议 | Apache 2 |
发布日期最新发布 | Jul 2015 |
由 Josh Gavant,Miguel Ángel Pérez Martínez,Microsoft Graph 维护。
使用这些 Objective-C 库轻松将 Office 365 的服务和数据集成到原生 iOS 应用中。
注意:此代码及其关联的二进制文件作为开发者的 预览 发布。您可以根据包含的 许可协议 使用此库,并在此仓库中提出问题以获得非官方支持。
有关官方微软支持的详细信息,请此处。
这些库使用 Vipr 和 Vipr-T4TemplateWriter 从 API 元数据生成,并使用一个 共享客户端堆栈。
当前服务、服务版本和 SDK 版本
API | 服务版本 | 最新 SDK 版本 | Pod 名称 | 伞形头文件 |
---|---|---|---|---|
邮件/日历/联系人 | 1.0 | 0.9.0 | Office365/Outlook | office365_exchange_sdk.h |
文件 | 1.0 | 0.9.0 | Office365/Files | office365_files_sdk.h |
发现 | 1.0 | 0.9.0 | Office365/Discovery | office365_discovery_sdk.h |
AAD Graph | 1.5 | 0.9.0 | Office365/AADGraph | office365_directory_sdk.h |
OneNote | 1.0 | 0.9.0 | Office365/OneNote | office365_onenote_sdk.h |
统一 API | beta | 0.1.0 | Office365/Graph | office365_graph_sdk.h |
SharePoint 列表 | 1.0 | 0.9.0 | Office365/Lists | office365-lists-sdk.h |
要在项目中使用这些库,请遵循以下通用步骤,详细说明如下:
从 Xcode 启动屏幕创建新的 Xcode 应用程序项目。在对话框中,选择 iOS > 单视图应用程序。根据您的需要命名应用程序;此处我们假设名称为 O365QuickStart。
向项目添加文件。在对话框中,选择 iOS > 其他 > 空的,并将文件命名为 Podfile
。
将这些行添加到 Podfile 中以导入上面列出的所有库。
source 'https://github.com/CocoaPods/Specs.git'
xcodeproj 'O365QuickStart'
pod 'Office365', '=0.9.0'
pod 'ADALiOS', '=1.2.2'
注意:有关CocoaPods的详细信息以及Podfile的最佳实践,请参阅使用CocoaPods指南。
关闭Xcode项目。
在命令行中,切换到您的项目目录。然后运行pod install
。
注意:当然首先安装CocoaPods。请在此处获取说明这里。
在终端中的同一位置,执行open O365QuickStart.xcworkspace
以在Xcode中打开包含导入pods的原项目的工作空间。
您的项目准备就绪后,下一步是初始化依赖项管理器和API客户端。
如果您尚未在Azure AD中注册您的应用程序,您需要按照这些说明完成此步骤。
右键单击O365QuickStart文件夹,选择“新建文件”。在对话框中,选择iOS > 资源 > 属性列表。将文件命名为adal_settings.plist
。将以下键添加到列表,并将它们的值设置为应用程序注册中的值。这些只是示例;请确保使用自己的值。
键 | 值 |
---|---|
ClientId | 示例:e59f95f8-7957-4c2e-8922-c1f27e1f14e0 |
RedirectUri | 示例:[https://my.client.app/](https://my.client.app/) |
ResourceId | 示例:[https://graph.microsoft.com/](https://graph.microsoft.com/) |
AuthorityUrl | https://login.microsoftonline.com/common/ |
右键单击O365QuickStart文件夹,选择“新建文件”。选择iOS > 源 > Cocoa Touch类并使用以下名称
转到您的应用中的Main.storyboard。从对象列表(右下角)添加一个Table View Controller,将入口点拖到新控制器上,并删除现有的View Controller。可选地,从项目中删除ViewController.h和ViewController.m,因为我们不会使用它们。
选择新的Table View Controller后,打开Identity检查器(右上角)。在自定义类部分,选择MessagesViewController作为类。MessageViewController现在与storyboard场景相关联。
从左侧的文档大纲中选择Messages视图控制器 > 表视图 > 表视图单元格。在属性检查器(右上角)中,将样式设置为副标题并标识为MessageCell。
注意:在iOS的最新版本中,视图扩展到整个屏幕,包括状态栏下方。处理此问题的方法之一是将视图嵌入在Navigation Controllers中。为此,选择Messages视图控制器,然后选择编辑 > 嵌套 > Navigation Controller。
从O365QuickStart文件夹打开MessagesViewController.m。将API或API的umbrella header添加到文件的顶部
#import <office365_exchange_sdk/office365_exchange_sdk.h>
在MessagesViewController.m的类扩展部分添加ADALDependencyResolver和MSOutlookServicesClient的属性。
@interface MessagesViewController ()
@property (strong, nonatomic) ADALDependencyResolver *resolver;
@property (strong, nonatomic) MSOutlookServicesClient *outlookClient;
@end
在MessagesViewController.m文件的viewDidLoad方法中初始化解析器和客户端。
- (void)viewDidLoad {
[super viewDidLoad];
self.resolver = [[ADALDependencyResolver alloc] initWithPlist];
self.outlookClient = [[MSOutlookServicesClient alloc] initWithUrl:@"https://outlook.office365.com/api/v1.0" dependencyResolver:self.resolver];
}
在可以使用客户端之前,您必须确保用户至少进行过一次交互式登录。您可以使用interactiveLogon
或interactiveLogonWithCallback:
来启动登录序列。在此练习中,将以下内容添加到最后一步的viewDidLoad方法中
[self.resolver interactiveLogonWithCallback:^(ADAuthenticationResult *result) {
if (result.status == AD_SUCCEEDED) {
[self refreshMessages];
} else {
[self.resolver.logger logMessage:@"Authentication failed." withLevel:LOG_LEVEL_ERROR];
}
}];
现在您可以安全地使用API客户端。首先,将另一个属性添加到类中,用于存储消息
@interface MessagesViewController ()
@property (strong, nonatomic) ADALDependencyResolver *resolver;
@property (strong, nonatomic) MSOutlookServicesClient *outlookClient;
@property (strong, nonatomic) NSArray<MSOutlookServicesMessage> *messages;
@end
然后,将此方法添加到MessagesViewController.m文件中,以刷新存储的消息
- (void)refreshMessages {
[[[[[self.outlookClient getMe] getMessages ] top:20] readWithCallback:^(NSArray<MSOutlookServicesMessage> *messages, MSODataException *exception) {
if (exception) {
[self.resolver.logger logMessage:@"Message retrieval failed." withLevel:LOG_LEVEL_ERROR];
} else {
self.messages = messages;
[(UITableView *)self.view reloadData];
}
[self.refreshControl endRefreshing];
}] resume];
}
最后,通过修改文件的Table View数据源部分,将缓存的邮件集合连接到MessagesViewController作为数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.messages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];
MSOutlookServicesMessage *message = self.messages[indexPath.row];
cell.textLabel.text = message.subject;
cell.detailTextLabel.text = [NSString stringWithFormat:@"From: %@", message.from.emailAddress.address];
return cell;
}
在提交拉取请求之前,您需要签署一个 贡献者许可协议。要完成贡献者许可协议(CLA),您需要通过表单提交请求,并在收到包含文档链接的电子邮件时电子签名贡献者许可协议。针对任何微软开源技术 OSS 项目,这只需要做一次。
版权所有(c)微软开源技术公司。保留所有权利。在Apache许可证版2.0下许可。