Skip to content

Commit ab0f423

Browse files
committed
Add location permissions not available alert dialog
* Inform the user when using preview mode that location permissions are not configured
1 parent c6e6654 commit ab0f423

10 files changed

+71
-29
lines changed

iOS_SDK/OneSignalSDK/Source/OSInAppMessageLocationPrompt.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
@interface OneSignal ()
3232

33-
+ (void)promptLocation:(void (^)(BOOL accepted))completionHandler;
33+
+ (void)promptLocation:(void (^)(PromptActionResult result))completionHandler;
3434

3535
@end
3636

@@ -45,7 +45,7 @@ - (instancetype)init
4545
return self;
4646
}
4747

48-
- (void)handlePrompt:(void (^)(BOOL accepted))completionHandler {
48+
- (void)handlePrompt:(void (^)(PromptActionResult result))completionHandler {
4949
[OneSignal promptLocation:completionHandler];
5050
}
5151

iOS_SDK/OneSignalSDK/Source/OSInAppMessagePrompt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
#define OSInAppMessagingPrompt_h
3030

3131
#import "OneSignal.h"
32+
#import "OneSignalCommonDefines.h"
3233

3334
#import <Foundation/Foundation.h>
3435

3536
@protocol OSInAppMessagePrompt <NSObject>
3637

3738
@property (nonatomic) BOOL hasPrompted;
3839

39-
- (void)handlePrompt:(void (^)(BOOL accepted))completionHandler;
40+
- (void)handlePrompt:(void (^)(PromptActionResult result))completionHandler;
4041

4142
@end
4243

iOS_SDK/OneSignalSDK/Source/OSInAppMessagePushPrompt.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#import "OSInAppMessagePushPrompt.h"
2929
#import "OneSignal.h"
30+
#import "OneSignalHelper.h"
3031

3132
@implementation OSInAppMessagePushPrompt
3233

@@ -39,8 +40,12 @@ - (instancetype)init
3940
return self;
4041
}
4142

42-
- (void)handlePrompt:(void (^)(BOOL accepted))completionHandler {
43-
[OneSignal promptForPushNotificationsWithUserResponse:completionHandler fallbackToSettings:YES];
43+
- (void)handlePrompt:(void (^)(PromptActionResult result))completionHandler {
44+
let acceptedCompletionHandler = ^(BOOL accepted) {
45+
let result = accepted ? PERMISSION_GRANTED : PERMISSION_DENIED;
46+
completionHandler(result);
47+
};
48+
[OneSignal promptForPushNotificationsWithUserResponse:acceptedCompletionHandler fallbackToSettings:YES];
4449
}
4550

4651
- (NSString *)description {

iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ - (instancetype _Nonnull)initWithMessage:(OSInAppMessage *)inAppMessage withScri
5656
- (void)loadedHtmlContent:(NSString *)html withBaseURL:(NSURL *)url {
5757
// UI Update must be done on the main thread
5858
NSLog(@"11111 [self.webView loadHTMLString:html baseURL:url];");
59-
dispatch_sync(dispatch_get_main_queue(), ^{
60-
NSLog(@"222222 [self.webView loadHTMLString:html baseURL:url];");
61-
[self.webView loadHTMLString:html baseURL:url];
62-
});
59+
dispatch_sync(dispatch_get_main_queue(), ^{
60+
NSLog(@"222222 [self.webView loadHTMLString:html baseURL:url];");
61+
[self.webView loadHTMLString:html baseURL:url];
62+
});
6363
}
6464

6565
- (void)setupWebviewWithMessageHandler:(id<WKScriptMessageHandler>)handler {

iOS_SDK/OneSignalSDK/Source/OSMessagingController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
3939

4040
@end
4141

42-
@interface OSMessagingController : NSObject <OSInAppMessageViewControllerDelegate, OSTriggerControllerDelegate, OSMessagingControllerDelegate>
42+
@interface OSMessagingController : NSObject <OSInAppMessageViewControllerDelegate, OSTriggerControllerDelegate, OSMessagingControllerDelegate, UIAlertViewDelegate>
4343

4444
@property (class, readonly) BOOL isInAppMessagingPaused;
4545

iOS_SDK/OneSignalSDK/Source/OSMessagingController.m

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#import "OSInAppMessageAction.h"
3535
#import "OSInAppMessageController.h"
3636
#import "OSInAppMessagePrompt.h"
37+
#import "OneSignalCommonDefines.h"
3738

3839
@interface OneSignal ()
3940

@@ -72,6 +73,10 @@ @interface OSMessagingController ()
7273

7374
@property (nonatomic, nullable) NSObject<OSInAppMessagePrompt>*currentPromptAction;
7475

76+
@property (nonatomic, nullable) NSArray<NSObject<OSInAppMessagePrompt> *> *currentPromptActions;
77+
78+
@property (nonatomic, nullable) OSInAppMessage *currentInAppMessage;
79+
7580
@property (nonatomic) BOOL isAppInactive;
7681

7782
@end
@@ -509,7 +514,8 @@ - (void)persistInAppMessageForRedisplay:(OSInAppMessage *)message {
509514
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"persistInAppMessageForRedisplay saved redisplayedInAppMessages: %@", [redisplayedInAppMessages description]]];
510515
}
511516

512-
- (void)handlePromptActions:(NSArray<NSObject<OSInAppMessagePrompt> *> *)promptActions {
517+
- (void)handlePromptActions:(NSArray<NSObject<OSInAppMessagePrompt> *> *)promptActions withMessage:(OSInAppMessage *)inAppMessage {
518+
_currentPromptAction = nil;
513519
for (NSObject<OSInAppMessagePrompt> *promptAction in promptActions) {
514520
// Don't show prompt twice
515521
if (!promptAction.hasPrompted) {
@@ -521,17 +527,40 @@ - (void)handlePromptActions:(NSArray<NSObject<OSInAppMessagePrompt> *> *)promptA
521527
if (_currentPromptAction) {
522528
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"IAM prompt to handle: %@", [_currentPromptAction description]]];
523529
_currentPromptAction.hasPrompted = YES;
524-
[_currentPromptAction handlePrompt:^(BOOL accepted) {
525-
_currentPromptAction = nil;
526-
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"IAM prompt to handle finished accepted: %@", accepted ? @"YES" : @"NO"]];
527-
[self handlePromptActions:promptActions];
530+
[_currentPromptAction handlePrompt:^(PromptActionResult result) {
531+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"IAM prompt to handle finished accepted: %u", result]];
532+
if (inAppMessage.isPreview && result == LOCATION_PERMISSIONS_MISSING_INFO_PLIST) {
533+
[self showAlertDialogMessage:inAppMessage promptActions:promptActions];
534+
} else {
535+
[self handlePromptActions:promptActions withMessage:inAppMessage];
536+
}
528537
}];
529538
} else if (!_viewController) { // IAM dismissed by action
530539
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"IAM with prompt dismissed from actionTaken"];
540+
_currentInAppMessage = nil;
541+
_currentPromptActions = nil;
531542
[self evaluateMessageDisplayQueue];
532543
}
533544
}
534545

546+
- (void)showAlertDialogMessage:(OSInAppMessage *)inAppMessage
547+
promptActions:(NSArray<NSObject<OSInAppMessagePrompt> *> *)promptActions {
548+
_currentInAppMessage = inAppMessage;
549+
_currentPromptActions = promptActions;
550+
let messageTitle = @"Location Not Available";
551+
let message = @"Looks like this app doesn't have location services configured. Please see OneSignal docs for more information.";
552+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:messageTitle
553+
message:message
554+
delegate:self
555+
cancelButtonTitle:nil
556+
otherButtonTitles:@"OK", nil];
557+
[alert show];
558+
}
559+
560+
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
561+
[self handlePromptActions:_currentPromptActions withMessage:_currentInAppMessage];
562+
}
563+
535564
- (void)messageViewDidSelectAction:(OSInAppMessage *)message withAction:(OSInAppMessageAction *)action {
536565
// Assign firstClick BOOL based on message being clicked previously or not
537566
action.firstClick = [message takeActionAsUnique];
@@ -540,7 +569,7 @@ - (void)messageViewDidSelectAction:(OSInAppMessage *)message withAction:(OSInApp
540569
[self handleMessageActionWithURL:action];
541570

542571
if (action.promptActions && action.promptActions.count > 0)
543-
[self handlePromptActions:action.promptActions];
572+
[self handlePromptActions:action.promptActions withMessage:message];
544573

545574
if (self.actionClickBlock)
546575
self.actionClickBlock(action);

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ + (void)promptLocation {
13971397
[self promptLocation:nil];
13981398
}
13991399

1400-
+ (void)promptLocation:(void (^)(BOOL accepted))completionHandler {
1400+
+ (void)promptLocation:(void (^)(PromptActionResult result))completionHandler {
14011401
// return if the user has not granted privacy permissions
14021402
if ([self shouldLogMissingPrivacyConsentErrorWithMethodName:@"promptLocation"])
14031403
return;

iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@
145145
#define OS_SESSION_TO_STRING(enum) [OS_SESSION_STRINGS objectAtIndex:enum]
146146
#define OS_SESSION_FROM_STRING(string) [OS_SESSION_STRINGS indexOfObject:string]
147147

148+
// OneSignal Prompt Action Result
149+
typedef enum {PERMISSION_GRANTED, PERMISSION_DENIED, LOCATION_PERMISSIONS_MISSING_INFO_PLIST, ERROR} PromptActionResult;
150+
148151
// OneSignal App Entry Action Types
149152
typedef enum {NOTIFICATION_CLICK, APP_OPEN, APP_CLOSE} AppEntryAction;
150153

iOS_SDK/OneSignalSDK/Source/OneSignalLocation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define OneSignalLocation_h
3030

3131
#import <Foundation/Foundation.h>
32+
#import "OneSignalCommonDefines.h"
3233

3334
typedef struct os_location_coordinate {
3435
double latitude;
@@ -47,7 +48,7 @@ typedef struct os_last_location {
4748
+ (bool)started;
4849
+ (void)internalGetLocation:(bool)prompt;
4950
- (void)locationManager:(id)manager didUpdateLocations:(NSArray *)locations;
50-
+ (void)getLocation:(bool)prompt withCompletionHandler:(void (^)(BOOL accepted))completionHandler;
51+
+ (void)getLocation:(bool)prompt withCompletionHandler:(void (^)(PromptActionResult result))completionHandler;
5152
+ (void)sendLocation;
5253
+ (os_last_location*)lastLocation;
5354
+ (void)clearLastLocation;

iOS_SDK/OneSignalSDK/Source/OneSignalLocation.m

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ + (void)clearLastLocation {
104104
}
105105
}
106106

107-
+ (void)getLocation:(bool)prompt withCompletionHandler:(void (^)(BOOL accepted))completionHandler {
107+
+ (void)getLocation:(bool)prompt withCompletionHandler:(void (^)(PromptActionResult result))completionHandler {
108108
if (completionHandler)
109109
[OneSignalLocation.locationListeners addObject:completionHandler];
110110

@@ -173,10 +173,10 @@ + (void)endTask {
173173
fcTask = UIBackgroundTaskInvalid;
174174
}
175175

176-
+ (void)sendAndClearLocationListener:(BOOL)accept {
176+
+ (void)sendAndClearLocationListener:(PromptActionResult)result {
177177
onesignal_Log(ONE_S_LL_DEBUG, [NSString stringWithFormat:@"OneSignalLocation sendAndClearLocationListener listeners: %@", OneSignalLocation.locationListeners]);
178178
for (int i = 0; i < OneSignalLocation.locationListeners.count; i++) {
179-
((void (^)(BOOL accepted))[OneSignalLocation.locationListeners objectAtIndex:i])(accept);
179+
((void (^)(PromptActionResult result))[OneSignalLocation.locationListeners objectAtIndex:i])(result);
180180
}
181181
// We only call the listeners once
182182
[OneSignalLocation.locationListeners removeAllObjects];
@@ -190,7 +190,7 @@ + (void)sendCurrentAuthStatusToListeners {
190190

191191
// If already given or denied the permission, listeners should have the response
192192
let denied = permissionStatus == kCLAuthorizationStatusRestricted || permissionStatus == kCLAuthorizationStatusDenied;
193-
[self sendAndClearLocationListener:!denied];
193+
[self sendAndClearLocationListener:denied ? PERMISSION_DENIED : PERMISSION_GRANTED];
194194
}
195195

196196
+ (void)internalGetLocation:(bool)prompt {
@@ -203,7 +203,7 @@ + (void)internalGetLocation:(bool)prompt {
203203

204204
// Check for location in plist
205205
if (![clLocationManagerClass performSelector:@selector(locationServicesEnabled)]) {
206-
[self sendAndClearLocationListener:false];
206+
[self sendAndClearLocationListener:ERROR];
207207
return;
208208
}
209209

@@ -229,13 +229,16 @@ + (void)internalGetLocation:(bool)prompt {
229229
if ([OneSignalHelper isIOSVersionGreaterThanOrEqual:@"9.0"])
230230
[locationManager setValue:@YES forKey:@"allowsBackgroundLocationUpdates"];
231231
}
232-
232+
233233
else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
234234
if (permissionStatus == kCLAuthorizationStatusNotDetermined)
235235
[locationManager performSelector:@selector(requestWhenInUseAuthorization)];
236236
}
237-
238-
else onesignal_Log(ONE_S_LL_ERROR, @"Include a privacy NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in your info.plist to request location permissions.");
237+
238+
else {
239+
onesignal_Log(ONE_S_LL_ERROR, @"Include a privacy NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in your info.plist to request location permissions.");
240+
[self sendAndClearLocationListener:LOCATION_PERMISSIONS_MISSING_INFO_PLIST];
241+
}
239242
}
240243

241244
// For iOS 6 and 7, location services are prompted here
@@ -251,7 +254,7 @@ + (void)internalGetLocation:(bool)prompt {
251254
- (void)locationManager:(id)manager didUpdateLocations:(NSArray *)locations {
252255
// return if the user has not granted privacy permissions or location shared is false
253256
if ([OneSignal requiresUserPrivacyConsent] || ![OneSignal isLocationShared]) {
254-
[OneSignalLocation sendAndClearLocationListener:false];
257+
[OneSignalLocation sendAndClearLocationListener:PERMISSION_DENIED];
255258
return;
256259
}
257260

@@ -283,12 +286,12 @@ - (void)locationManager:(id)manager didUpdateLocations:(NSArray *)locations {
283286
if (!initialLocationSent)
284287
[OneSignalLocation sendLocation];
285288

286-
[OneSignalLocation sendAndClearLocationListener:true];
289+
[OneSignalLocation sendAndClearLocationListener:PERMISSION_GRANTED];
287290
}
288291

289292
- (void)locationManager:(id)manager didFailWithError:(NSError *)error {
290293
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"CLLocationManager did fail with error: %@", error]];
291-
[OneSignalLocation sendAndClearLocationListener:false];
294+
[OneSignalLocation sendAndClearLocationListener:ERROR];
292295
}
293296

294297
+ (void)resetSendTimer {

0 commit comments

Comments
 (0)