测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可协议 | Apache 2 |
发布最后发布 | 2016年7月 |
SPM支持 SPM | ✗ |
由 John Nilsen,Tapglue 维护。
这将帮助您逐步开始使用 iOS 上的 Tapglue Elements。
更多详细文档可以在我们的 文档 网站找到。
创建用户档案、关注者和被关注者列表、通知流和用户搜索。
要开始使用 Tapglue API,您需要一个 APP_TOKEN
。请访问我们的 仪表盘 并使用您的凭据登录或创建一个新账户。
在本节中,我们将解释如何将 Tapglue Elements 集成到您的 iOS 应用中。Tapglue Elements 在 SDK 的基础上更进一步,包括一套完整的屏幕和导航。
首先,按照 ios sdk 文档中的说明安装 tapglue,然后将 elements 添加为子模块到您的项目中,通过执行以下命令
git submodule add https://github.com/tapglue/elements-ios.git externals/elements
然后在您的项目中将 elements 文件夹添加到项目中,并检查您需要的 目标。
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
我们希望让 Elements 尽可能灵活,并允许您对其进行自定义。您需要在您的应用程序中设置 Tapglue,其外观可能如下所示
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let tgConfig = TGConfiguration.defaultConfiguration()
Tapglue.setUpWithAppToken(YOUR_TOKEN, andConfig: tgConfig)
}
有关如何配置 Tapglue SDK 的更多详细信息,请参阅 iOS 指南中的 安装部分。这也是您应自定义 Tapglue Elements 外观的地方。要自定义它,您需要向 Elements 提供一个配置 JSON
。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let tgConfig = TGConfiguration.defaultConfiguration()
Tapglue.setUpWithAppToken(YOUR_TOKEN, andConfig: tgConfig)
if let path = NSBundle.mainBundle().pathForResource("config", ofType: "json"){
do {
let data = try NSData(contentsOfURL: NSURL(fileURLWithPath: path), options: NSDataReadingOptions.DataReadingMappedIfSafe)
try TapglueUI.setConfig(data)
} catch let error as NSError {
print("could not load config file! Error: \(error)")
}
}
return true
}
使用一行代码整合元素,以获得完整的体验。在您的应用程序中使用元素的简单方法是通过简单地过渡到我们的视图控制器。
您执行两种切换,其中一个切换到个人资料屏幕
import UIKit
import elements
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
TapglueUI.performSegueToProfile(self)
}
}
另一个切换是切换到通知推送
import UIKit
import elements
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
TapglueUI.performSegueToNotificationFeed(self)
}
}
这种方法中,我们在您的视图控制器之上推送导航控制器,从而用元素自己的导航替换了您的导航。
将 Elements 整合到现有的体验中,让我们处理社交部分。
要完全控制 Elements 内发生的事件,我们建议将我们的视图控制器用作您自己的视图控制器的子视图控制器。
这样,元素在用户端更加自然地融入您的应用程序。
import UIKit
import elements
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.profileViewController()
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
}
当以这种方式使用我们的视图控制器时,默认行为是整合我们自己的导航。这意味着当用户点击不同的操作项时,我们将导航到我们的不同屏幕。
如果您需要更多的灵活性,我们还提供可以实施以对视图控制器中可能发生的事件有更深入控制的代理。例如,通知屏幕有以下代理协议。
public protocol NotificationFeedViewDelegate{
func defaultNavigationEnabledInNotificationFeedViewController(notificationFeedViewController: NotificationFeedViewController) -> Bool
func notificationFeedViewController(noticationFeedViewController: NotificationFeedViewController, didSelectEvent event: TGEvent)
}
defaultNavigationEnabledInNotificationFeedViewController(notificationFeedViewController: NotificationFeedViewController) -> Bool
允许您决定是否让元素为您导航。您会在所有我们的视图控制器代理中找到类似的方法。
一个实用示例可能如下。
import UIKit
import elements
import Tapglue
class ViewController: UIViewController, NotificationFeedViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.notificationFeedViewController()
vc.delegate = self
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
self.view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
func defaultNavigationEnabledInNotificationFeedViewController(notificationFeedViewController: NotificationFeedViewController) -> Bool {
return false
}
func notificationFeedViewController(noticationFeedViewController: NotificationFeedViewController, didSelectEvent event: TGEvent) {
//handle event tap
}
}
在这里,元素中的导航被关闭,因此当用户点击事件单元格时,您理想地希望导航到某个特定的事件屏幕。
要使用个人资料视图控制器,将其作为子视图控制器和子视图添加到您的视图控制器中。
let vc = TapglueUI.profileViewController()
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
如果您想使用代理,请确保分配个人资料视图控制器的代理。
import UIKit
import elements
import Tapglue
class ViewController: UIViewController, ProfileViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.profileViewController()
vc.delegate = self
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
func defaultNavigationEnabledInProfileViewController(profileViewController: ProfileViewController) -> Bool {
//answer if you want to override navigation or not
return false
}
func referenceUserInProfileViewController(profileViewController: ProfileViewController) -> TGUser {
//return user whos profile is to be displayed
return TGUser.currentUser()
}
func profileViewController(profileViewController: ProfileViewController, didSelectConnectionsOfType connectionType: ConnectionType, forUser user: TGUser) {
//add handling for when a connection type was selected for the displayed user
}
func profileViewController(profileViewcontroller: ProfileViewController, didSelectEvent event: TGEvent) {
//add handling for when an event was selected on the user profile
}
}
要使用联系人视图控制器,将其作为子视图控制器和子视图添加到您的视图控制器中。
import UIKit
import elements
import Tapglue
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.connectionsViewController()
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
}
如果您想使用代理,请确保分配联系人视图控制器的代理。
import UIKit
import elements
import Tapglue
class ViewController: UIViewController, ConnectionsViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.connectionsViewController()
vc.delegate = self
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
func defaultNavigationEnabledInConnectionsViewController(connectionsViewController: ConnectionsViewController) -> Bool {
//answer if you want to override navigation or not
return false
}
func referenceUserInConnectionsViewController(connectionsViewController: ConnectionsViewController) -> TGUser {
//return user whos connections is to be displayed
return TGUser.currentUser()
}
func connectionTypeInConnectionsViewController(connectionsViewController: ConnectionsViewController) -> ConnectionType {
//return the type of connections to be displayed, can be either
// ConnectionType.Followers or ConnectionType.Following
return ConnectionType.Followers
}
func connectionsViewController(connectionsViewController: ConnectionsViewController, didSelectUser user: TGUser) {
//handle event when a connection is tapped
}
}
要使用通知推送视图控制器,将其作为子视图控制器和子视图添加到您的视图控制器中。
import UIKit
import elements
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.notificationFeedViewController()
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
}
如果您想使用代理,请确保分配通知推送视图控制器的代理。对于通知屏幕来说,这特别有趣,因为我们没有提供新闻推送,这是您可以链接回帖子的地方。
import UIKit
import elements
import Tapglue
class ViewController: UIViewController, NotificationFeedViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.notificationFeedViewController()
vc.delegate = self
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
func defaultNavigationEnabledInNotificationFeedViewController(notificationFeedViewController: NotificationFeedViewController) -> Bool {
//answer if you want to override navigation or not
return false
}
func notificationFeedViewController(noticationFeedViewController: NotificationFeedViewController, didSelectEvent event: TGEvent) {
//add handling for when an event was selected on the user profile
}
}
要使用用户搜索视图控制器,将其作为子视图控制器和子视图添加到您的视图控制器中。
import UIKit
import elements
import Tapglue
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.userSearchViewController()
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
}
如果您想使用代理,请确保分配用户搜索视图控制器的代理。
import UIKit
import elements
import Tapglue
class ViewController2: UIViewController, UserSearchViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let vc = TapglueUI.userSearchViewController()
vc.delegate = self
vc.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
self.addChildViewController(vc)
view.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
func defaultNavigationEnabledInUserSearchViewController(userSearchViewController: UserSearchViewController) -> Bool {
//answer if you want to override navigation or not
return false
}
func userSearchViewController(userSearchViewController: UserSearchViewController, didSelectUser user: TGUser) {
//handle event when a user is tapped
}
func didTapAddressBookInUserSearchViewController(userSearchViewController: UserSearchViewController) {
//handle event when address book search is requested
}
}