Polar iOS SDK 允许您在自己的 iOS 应用中使用 Polar Polls。(了解如何集成 Polar Polls。)。
初始化 Polar 的实例时,它会创建一个加载嵌入式 Polar 投票的 UIWebView。您可以通过 pollView
属性访问此 UIWebView。只需按您喜欢的方式将此 UIWebView 添加到您的应用中即可。注意,pollView
UIWebView 的最小推荐大小为 300x300。
或者,简单地将 Polar.h
和 Polar.m
拷贝到您的项目中:[Polar.h](https://raw.github.com/input-factory/polar-ios-sdk/0.1.0/Classes/Polar.h) [Polar.m](https://raw.github.com/input-factory/polar-ios-sdk/0.1.0/Classes/Polar.m)
要运行示例项目,请克隆仓库,并首先从示例目录中运行 pod install
。然后,通过打开文件 Example/PolarExample.xcworkspace 来打开项目。
示例代码
// replace this with the username for your Polar account
NSString *polarUserName = @"jcole";
// instantiate polar instance and add to view
Polar *polar = [[Polar alloc] init:polarUserName environment:PolarEnvironmentProduction];
[self.view addSubview:polar.pollsView];
// set the frame for your embed: 300x300 minimum recommended
[polar.pollsView setFrame:CGRectMake(0, 30, 320, 320)];
// load your polls
[polar loadPollSet:@926]; // example of a poll set
// [polar loadPoll:@98779]; // example of a single poll
// act as delegate for Polar activity (e.g. polls loaded, user voted, polls navigated)
// note: make sure in your controller interface, your controller implements PolarDelegate, e.g:
// @interface PolarExampleViewController : UIViewController <PolarDelegate>
polar.delegate = self;
初始化 Polar 的实例。用户名参数来自您代表发布的 Polar 网站账户。当您作为此用户登录时,所有嵌入的分析和结果将在网站上显示。应该指定环境为测试或生产。
- (id)init:(NSString *)username environment:(PolarEnvironment)polarEnvironment;
参数 | 描述 |
---|---|
username | 用户的 Polar 用户名或电子邮件。区分大小写。 |
polarEnvironment | 是否使用生产环境(PolarEnvironmentProduction )或测试环境 PolarEnvironmentTest 。 |
- (void)loadPoll:(NSNumber*)pollID;
参数 | 描述 |
---|---|
pollID | 要加载的投票的 ID。 |
- (void)loadPollSet:(NSNumber*)pollSetID;
参数 | 描述 |
---|---|
pollSetID | 要加载的投票集的ID。 |
该标识符为每个设备唯一生成。它存储在NSUserDefaults中,因此将在会话之间持续存在。
+ (NSString *)viewerID;
如果您想重置为该用户生成的唯一标识符(例如,用于测试或当用户退出您的应用程序时)。
+ (void)resetViewerID;
获取当前正在显示的投票的ID。
- (NSString *)getCurrentPollID;
此UIWebView加载了一个包含自包含嵌入投票组件的iFrame。
- (UIWebView *)pollsView;
Polar将在投票加载完成、用户投票或用户导航到新的投票时通知您的应用程序。要注册这些,请让您的控制器或其他对象实现PolarDelegate方法。例如
@interface PolarExampleViewController : UIViewController <PolarDelegate>
然后,将您的对象设置为polar实例的代理。例如
polar.delegate = self;
然后,让您的对象实现以下任一回调委托方法
- (void)pollsLoaded {
NSLog(@"polls loaded");
}
- (void)userVoted {
NSLog(@"user voted");
}
- (void)userNavigated {
NSLog(@"user navigated");
}
您可以使用环境PolarEnvironmentTest
或PolarEnvironmentProduction
来运行Polar。
我们的生产网站
https://polarb.com
用于测试,使用我们的预发布服务器
https://polar-rails-staging.herokuapp.com
应使用各自环境目标在每个网站上使用时提供所有内容。将PolarEnvironmentTest视为您可以玩耍的沙盒。
您需要在每个希望使用的环境中注册一个polar账户
创建生产账户:http://polarb.com/join
创建测试账户:https://polar-rails-staging.herokuapp.com/join
Polar-iOS-SDK在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。