InteractiveTools 0.1.4

InteractiveTools 0.1.4

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2019年2月

Sebastian WestemeyerTim Christmann 维护。



  • 作者:
  • Sebastian Westemeyer

InteractiveTools - 一个用于 ORT Interactive 项目的各种解决方案的工具箱。

如果您需要示例,只需要克隆仓库并运行应用程序(最好是在支持 3D touch 的设备上运行,因为模拟器不支持 3D touch)

安装

安装 InteractiveTools 的推荐方法是通过 CocoaPods 包管理器,因为它提供了灵活的依赖管理以及简单的安装过程。

当前特性

  • 用于与 UIApplicationShortcutItem 一起使用的 3D touch 处理器类
  • UIApplication 生命周期包装器类
  • UIColor 类别,用于从十六进制值生成颜色
  • 消息框实用工具,作为 UIAlertAction 的包装器
  • 推送消息注册助手

如果没有安装 CocoaPods,请先安装

$ [sudo] gem install cocoapods
$ pod setup

切换到 Xcode 项目的目录,并创建和编辑您的 Podfile,添加 RestKit

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
platform :ios, '8.0'

pod 'InteractiveTools'

将安装到您的项目中

$ pod install

从 .xcworkspace 文件打开您的项目,而不是常规的项目文件

$ open MyProject.xcworkspace

示例

3D触摸

您可能需要实现一个类似这样的IATouchDelegate

// content of file IATouchHandlerDelegate.h
#import "IATouchHandler.h"

@interface IATouchHandlerDelegate : NSObject<IATouchDelegate>

@end

// content of file IATouchHandlerDelegate.m
#import "IATouchHandlerDelegate.h"
#import "UIAlertController+IAAdditions.h"

@implementation IATouchHandlerDelegate

- (NSArray<UIApplicationShortcutItem *>*) handlerItems {
    // configure shortcuts
    return @[ [[UIApplicationShortcutItem alloc] initWithType:@"type1"
                                               localizedTitle:@"Play"
                                            localizedSubtitle:@"Start playing"
                                                         icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypePlay]
                                                     userInfo:nil],
              [[UIApplicationShortcutItem alloc] initWithType:@"type2"
                                               localizedTitle:@"Pause"
                                            localizedSubtitle:@"Pause playback"
                                                         icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypePause]
                                                     userInfo:nil] ];
}

- (BOOL) handleItem: (UIApplicationShortcutItem *) item atIndex: (NSUInteger) idx {
    // handle shortcuts
    switch (idx){
        case 0:
            [UIAlertController showMessage:@"Play shortcut handled" title:@"Shortcut handler" once:nil];
            break;
        case 1:
            [UIAlertController showMessage:@"Pause shortcut handled" title:@"Shortcut handler" once:nil];
            break;
        default:
            // return NO or implement a default action and return YES
            return NO;
    }
    return YES;
}

@end

按照这种方式设置AppDelegate

// bind to language keys
#import "AppDelegate.h"
#import "IATouchHandler.h"
#import "IATouchHandlerDelegate.h"

@interface AppDelegate ()
@property(nonatomic, strong) IATouchHandler* touchHandler;
@end

@implementation AppDelegate

- (instancetype) init{
    self = [super init];
    if (self != nil){
        self.touchHandler = [[IATouchHandler alloc] initWithDelegate:[[IATouchHandlerDelegate alloc] init]];
    }
    return self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return [self.touchHandler checkLaunchOptions:launchOptions forApplication:application];
}

// 3D Touch handling
- (void) application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    completionHandler([self.touchHandler handleShortcutItem:shortcutItem]);
}

@end

推送注册

按照这种方式设置AppDelegate

// bind to language keys
#import "AppDelegate.h"
#import "IAPushHandler.h"

@interface AppDelegate ()
@property (nonatomic, strong) IAPushHandler *pushHandler;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.pushHandler = [IAPushHandler initWithLaunchOptions:launchOptions
        urlPlistKey:@"WEB_BASE_URL"
        handleNotificationReceived:^(IANotification *notification, BOOL hasBeenOpened) {
            NSLog(@"Notification received");
        }
        settings:@{
            kIASettingsKeyPushTokenParameterName : @"identifier",
            kIASettingsKeyDeviceTypeParameterName : @"os_type",
            kIASettingsKeyAutoPrompt : @NO
        }];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    // register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [self.pushHandler application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [self.pushHandler application:application didReceiveRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error!");
}

@end

在您的Info.plist文件中将WEB_BASE_URL设置为$(WEB_BASE_URL),并定义一个名为WEB_BASE_URL的新用户定义构建设置。可以针对每个构建配置设置不同的Web服务器URL。

推送注册过程通过在项目任何地方调用静态方法[IAPushHandler registerForPushNotifications]来触发。如果应在应用启动后立即开始注册,可以在AppDelegate中省略kIASettingsKeyAutoPrompt设置。

致谢

许可证

代码可供github上的项目下载,并遵循MIT许可证