iOS 中未出现 GNSPermission 警报对话框
GNSPermission Alert Dialog not appearing in iOS
#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
处理程序 class 的对象,然后将其传递给另一个视图控制器,该对象将在其中扫描或广播,即 startScanning
/startPublishingDefaultData
方法在传递的视图控制器的 viewDidAppear
方法中被调用。但是我附近的权限对话框永远不会出现,因此不会发生扫描/发布。你能告诉我代码中存在问题的地方吗?
P.S: 我以为我的密钥无效但是当我在附近的演示项目中应用相同的密钥时 (https://github.com/googlesamples/ios-nearby),它工作正常。
附近消息中的政策是仅当 iOS 需要对某些内容(例如麦克风或背景 BLE)的许可时才请求许可。在您的情况下,您仅在前台使用 BLE,并且此模式不需要 iOS 权限,因此 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
处理程序 class 的对象,然后将其传递给另一个视图控制器,该对象将在其中扫描或广播,即 startScanning
/startPublishingDefaultData
方法在传递的视图控制器的 viewDidAppear
方法中被调用。但是我附近的权限对话框永远不会出现,因此不会发生扫描/发布。你能告诉我代码中存在问题的地方吗?
P.S: 我以为我的密钥无效但是当我在附近的演示项目中应用相同的密钥时 (https://github.com/googlesamples/ios-nearby),它工作正常。
附近消息中的政策是仅当 iOS 需要对某些内容(例如麦克风或背景 BLE)的许可时才请求许可。在您的情况下,您仅在前台使用 BLE,并且此模式不需要 iOS 权限,因此 Nearby 不会请求权限。