GNSPermission警报对话框未出现在iOS中

时间:2017-05-15 05:49:10

标签: ios google-nearby

#import "NearbyHandler.h"
#import <GNSMessages.h>

static NSString * const kMyAPIKey = @"********";

@interface NearbyHandler ()
@property(nonatomic) GNSPermission *nearbyPermission;
@property(nonatomic) GNSMessageManager *messageMgr;
@property(nonatomic) id<GNSPublication> publication;
@property(nonatomic) id<GNSSubscription> subscription;
@end

@implementation NearbyHandler


- (instancetype)init
{
    if (self = [super init])
    {
        [self configureNearbyFramework];
    }
    return self;
}

- (void)configureNearbyFramework
{
    __weak __typeof__(self) weakSelf = self;
    _nearbyPermission = [[GNSPermission alloc] initWithChangedHandler:^(BOOL granted) {

        __strong __typeof(weakSelf) strongSelf = weakSelf;

        if(strongSelf)
        {
            NSLog(@"Nearby Permission:%@", granted ? @"YES" : @"NO");
        }

    }];

    [GNSMessageManager setDebugLoggingEnabled:YES];

}

#pragma mark - START_SCAN_PUBLISH_METHODS
- (void)startPublishingDefaultData
{

    NSDictionary *dataDict = @{@"device_name":@"cefgrstd"};
    NSError *error = nil;
    NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dataDict
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error];

    if(!error)
    {
        [self pusblishData:dataFromDict];
    }

}

- (void)publishSpecificData:(NSString *)stringData
{
    if(stringData.length)
    {
        NSData *pubData = [stringData dataUsingEncoding:NSUTF8StringEncoding];

        [self pusblishData:pubData];
    }
}

- (void)pusblishData:(NSData *)data
{
    if(!_publication)
    {
        // This means that the data was not sent

        if(![GNSPermission isGranted])
        {
            [self configureNearbyFramework];
        }

        __weak __typeof__(self) weakSelf = self;
        GNSMessage *dataForPublish = [GNSMessage messageWithContent:data];
        _publication = [self.messageMgr publicationWithMessage:dataForPublish
                                                   paramsBlock:^(GNSPublicationParams *params) {
                                                       __strong __typeof(weakSelf) strongSelf = weakSelf;

                                                       if(strongSelf)
                                                       {
                                                           params.strategy = [strongSelf getStrategy];
                                                       }
                                                   }];

        // The reason why we have to scan after publishing is to check if there is a faliure of data transmission

        [self startScanning];
    }

}

- (void)startScanning
{
    if(![GNSPermission isGranted])
    {
        [self configureNearbyFramework];
    }

    __weak __typeof__(self) weakSelf = self;
    _subscription = [_messageMgr
                     subscriptionWithMessageFoundHandler:^(GNSMessage *message) {
                         __strong __typeof(weakSelf) strongSelf = weakSelf;

                         // Here 
                         }
                     }
                     messageLostHandler:^(GNSMessage *message) {

                         // should we dealloc the _publication here again?
                         __strong __typeof(weakSelf) strongSelf = weakSelf;

                         if(strongSelf)
                         {
                            // Here we loose the message
                         }
                     }
                     paramsBlock:^(GNSSubscriptionParams *params) {

                         __strong __typeof(weakSelf) strongSelf = weakSelf;

                         if(strongSelf)
                         {
                             params.strategy = [strongSelf getStrategy];
                         }

                     }];
}



/// Stops sharing and scanning.
- (void)stopSharingAndScanning {

    if(_publication)
        _publication = nil;

    if(_subscription)
        _subscription = nil;

    if(_messageMgr)
        _messageMgr = nil;

}

#pragma mark - PERMISSION
/// Toggles the permission state of Nearby.
- (void)changeNearbyPermission {
    [GNSPermission setGranted:![GNSPermission isGranted]];
}

#pragma mark - GETTERS
- (GNSMessageManager *)messageMgr
{
    if(!_messageMgr)
    {
        _messageMgr = [[GNSMessageManager alloc] initWithAPIKey:kMyAPIKey paramsBlock:^(GNSMessageManagerParams *params) {
            params.microphonePermissionErrorHandler = ^(BOOL hasError) {
                NSLog(@"Microphone Permission Error:%@", hasError ? @"YES" : @"NO");

            };
            params.bluetoothPowerErrorHandler = ^(BOOL hasError) {
                NSLog(@"Bluetooth Power Error:%@", hasError ? @"YES" : @"NO");

            };
            params.bluetoothPermissionErrorHandler = ^(BOOL hasError) {
                NSLog(@"Bluetooth Permission Error:%@", hasError ? @"YES" : @"NO");
            };
        }];
    }
    return _messageMgr;
}
- (GNSStrategy *)getStrategy
{
    return [GNSStrategy strategyWithParamsBlock:^(GNSStrategyParams *params) {
        params.allowInBackground = NO;
        params.discoveryMediums = kGNSDiscoveryMediumsBLE;
        params.discoveryMode = (_shouldScanForPeers) ? kGNSDiscoveryModeScan : kGNSDiscoveryModeDefault;
    }];
}

我在视图控制器中创建此Nearby处理程序类的对象,然后将其传递到另一个视图控制器,此对象将扫描或广播,即startScanning / {在传递的View Controller的startPublishingDefaultData方法中调用{1}}方法。但我的附近许可对话框从未出现,因此扫描/发布不会发生。你能告诉我代码中存在问题的地方吗?

P.S:我认为我的密钥无效但是当我在附近(https://github.com/googlesamples/ios-nearby)的演示项目中应用相同的密钥时,它可以正常工作。

1 个答案:

答案 0 :(得分:0)

“附近邮件”中的政策是仅在iOS需要某些权限(例如麦克风或后台BLE)时才要求获得许可。在您的情况下,您只在前台使用BLE,并且此模式不需要iOS权限,因此Nearby不会请求许可。