首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >苹果手表中的PubNub框架

苹果手表中的PubNub框架
EN

Stack Overflow用户
提问于 2015-10-15 22:23:22
回答 1查看 180关注 0票数 2

我一直试图手动将PubNub框架导入到我的Apple应用程序中。PubNub使用的许多依赖项和框架在手表上不可用(如SystemConfiguration、CFNetworking等)。苹果手表是否得到PubNub的支持?如何让它很好地导入到我的Apple Watch应用程序中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-21 21:32:03

只需要做几处修改。将UIKit替换为WatchKit替换:

TLDR:

  • UIDevice替换WKInterfaceDevice
  • [[UIDevice currentDevice] identifierForDevice]替换为存储在NSUserDefaults中的UUID
  • UIApplicationWillEnterForegroundNotificationUIApplicationDidEnterBackgroundNotification替换为NSExtensionHostWillEnterForegroundNotificationNSExtensionHostDidEnterBackgroundNotification
  • 删除GZIP引用

档案的详细说明:

PNConfiguration.m

代码语言:javascript
复制
    - (NSString *)uniqueDeviceIdentifier {
#if TARGET_OS_WATCH
        NSString *key = @"PNUserDefaultsUUIDKey";
        NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:key];
        if (!uuid) {
            uuid = [[NSUUID UUID] UUIDString];
            [[NSUserDefaults standardUserDefaults] setValue:uuid forKey:key];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        return uuid;
#elif __IPHONE_OS_VERSION_MIN_REQUIRED
        return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
        return ([self serialNumber]?: [self macAddress]);
#endif
    }

PNNetwork.m

代码语言:javascript
复制
    - (NSDictionary *)defaultHeaders {
        NSString *device = @"iPhone";
        NSString *osVersion = @"2.0";
#if TARGET_OS_WATCH
        osVersion = [[WKInterfaceDevice currentDevice] systemVersion];
        device = [[WKInterfaceDevice currentDevice] model];
#elif __IPHONE_OS_VERSION_MIN_REQUIRED
        device = [[UIDevice currentDevice] model];
        osVersion = [[UIDevice currentDevice] systemVersion];
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
        NSOperatingSystemVersion version = [[NSProcessInfo processInfo]operatingSystemVersion];
        NSMutableString *osVersion = [NSMutableString stringWithFormat:@"%@.%@",
                              @(version.majorVersion), @(version.minorVersion)];
        if (version.patchVersion > 0) {

            [osVersion appendFormat:@".%@", @(version.patchVersion)];
    }
#endif
        NSString *userAgent = [NSString stringWithFormat:@"iPhone; CPU %@ OS %@ Version",
                       device, osVersion];

         return @{@"Accept":@"*/*", @"Accept-Encoding":@"gzip,deflate", @"User-Agent":userAgent,
         @"Connection":@"keep-alive"};
    }

PubNub+Core.m

代码语言:javascript
复制
    - (instancetype)initWithConfiguration:(PNConfiguration *)configuration
                    callbackQueue:(dispatch_queue_t)callbackQueue {

// Check whether initialization has been successful or not
        if ((self = [super init])) {
#if DEBUG
            [PNLog dumpToFile:YES];
#else
            [PNLog dumpToFile:NO];
#endif

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> PubNub SDK %@ (%@)",
                    kPNLibraryVersion, kPNCommit);

            _configuration = [configuration copy];
            _callbackQueue = callbackQueue;
            [self prepareNetworkManagers];

            _subscriberManager = [PNSubscriber subscriberForClient:self];
            _clientStateManager = [PNClientState stateForClient:self];
            _listenersManager = [PNStateListener stateListenerForClient:self];
            _heartbeatManager = [PNHeartbeat heartbeatForClient:self];
            [self addListener:self];
            [self prepareReachability];
#if TARGET_OS_WATCH
            NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:NSExtensionHostWillEnterForegroundNotification object:nil];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:NSExtensionHostDidEnterBackgroundNotification object:nil];
#elif __IPHONE_OS_VERSION_MIN_REQUIRED
            NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:UIApplicationWillEnterForegroundNotification object:nil];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:UIApplicationDidEnterBackgroundNotification object:nil];
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
            NSNotificationCenter *notificationCenter = [[NSWorkspace sharedWorkspace] notifiertionCenter];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:NSWorkspaceWillSleepNotification object:nil];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:NSWorkspaceSessionDidResignActiveNotification object:nil];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:NSWorkspaceDidWakeNotification object:nil];
            [notificationCenter addObserver:self selector:@selector(handleContextTransition:)
                               name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];
#endif
        }

        return self;
    }

PubNub+Core.m

代码语言:javascript
复制
    - (void)handleContextTransition:(NSNotification *)notification {
#if TARGET_OS_WATCH
        if ([notification.name isEqualToString:NSExtensionHostDidEnterBackgroundNotification]) {

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> Did enter background execution context.");
        }
        else if ([notification.name isEqualToString:NSExtensionHostWillEnterForegroundNotification]) {

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> Will enter foreground execution context.");
        }
#elif __IPHONE_OS_VERSION_MIN_REQUIRED
        if ([notification.name isEqualToString:UIApplicationDidEnterBackgroundNotification]) {

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> Did enter background execution context.");
        }
        else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> Will enter foreground execution context.");
        }
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
        if ([notification.name isEqualToString:NSWorkspaceWillSleepNotification] ||
        [notification.name isEqualToString:NSWorkspaceSessionDidResignActiveNotification]) {

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> Workspace became inactive.");
        }
        else if ([notification.name isEqualToString:NSWorkspaceDidWakeNotification] ||
             [notification.name isEqualToString:NSWorkspaceSessionDidBecomeActiveNotification]) {

            DDLogClientInfo([[self class] ddLogLevel], @"<PubNub> Workspace became active.");
        }
#endif
    }

PubNub+Core.m

代码语言:javascript
复制
    - (void)dealloc {
#if TARGET_OS_WATCH
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter removeObserver:self name:NSExtensionHostWillEnterForegroundNotification
                            object:nil];
        [notificationCenter removeObserver:self name:NSExtensionHostDidEnterBackgroundNotification
                            object:nil];
#elif __IPHONE_OS_VERSION_MIN_REQUIRED
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter removeObserver:self name:UIApplicationWillEnterForegroundNotification
                            object:nil];
        [notificationCenter removeObserver:self name:UIApplicationDidEnterBackgroundNotification
                            object:nil];
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
        NSNotificationCenter *notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
        [notificationCenter removeObserver:self name:NSWorkspaceWillSleepNotification object:nil];
        [notificationCenter removeObserver:self name:NSWorkspaceSessionDidResignActiveNotification
                            object:nil];
        [notificationCenter removeObserver:self name:NSWorkspaceDidWakeNotification object:nil];
        [notificationCenter removeObserver:self name:NSWorkspaceSessionDidBecomeActiveNotification
                            object:nil];
#endif
    }

PubNub+Publish.m:使用PNGZIP的地方

代码语言:javascript
复制
    NSData *publishData = nil;
    if (compressed) {
#if !TARGET_OS_WATCH
        NSData *messageData = [messageForPublish dataUsingEncoding:NSUTF8StringEncoding];
        NSData *compressedBody = [PNGZIP GZIPDeflatedData:messageData];
        publishData = (compressedBody?: [@"" dataUsingEncoding:NSUTF8StringEncoding]);
#else
        NSLog(@"Tried to compress, but GZip is not available");
#endif
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33159536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档