Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions GoogleUtilitiesMulticastAppDelegate.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Pod::Spec.new do |s|
watchos_deployment_target = '6.0'

s.ios.deployment_target = ios_deployment_target
# s.osx.deployment_target = osx_deployment_target
# s.tvos.deployment_target = tvos_deployment_target
# s.watchos.deployment_target = watchos_deployment_target
s.osx.deployment_target = osx_deployment_target
s.tvos.deployment_target = tvos_deployment_target
s.watchos.deployment_target = watchos_deployment_target

s.cocoapods_version = '>= 1.4.0'
s.prefix_header_file = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GoogleUtilitiesMulticastAppDelegate/Sources/Public/GoogleUtilities/GULMulticastAppDelegate.h"
#import <GoogleUtilitiesMulticastAppDelegate/GULMulticastAppDelegate.h>
//#import <GoogleUtilities/GULAppDelegateSwizzler.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete


@interface GULMulticastAppDelegate () <GULMulticastAppDelegateProtocol> {
NSMutableArray<id> *_interceptors;
Expand Down Expand Up @@ -40,7 +41,7 @@ - (instancetype)initWithAppDelegate:(id<GULApplicationDelegate>)delegate {
}

+ (id<GULMulticastAppDelegateProtocol>)multicastDelegate {
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
id<UIApplicationDelegate> appDelegate = [GULAppDelegateSwizzler sharedApplication].delegate;
if (!appDelegate) {
return nil;
}
Expand All @@ -64,13 +65,19 @@ - (instancetype)initWithAppDelegate:(id<GULApplicationDelegate>)delegate {

- (void)addInterceptorWithInterceptor:(id<GULApplicationDelegate>)interceptor {
[_interceptors addObject:interceptor];
if (!_defaultAppDelegate) {
_defaultAppDelegate = interceptor;
}
}

- (void)removeInterceptorWithInterceptor:(id<GULApplicationDelegate>)interceptor {
[_interceptors removeObject:interceptor];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
if (_defaultAppDelegate && [_defaultAppDelegate respondsToSelector:aSelector]) {
return YES;
}
if ([[self class] instancesRespondToSelector:aSelector]) {
return YES;
}
Expand All @@ -79,6 +86,7 @@ - (BOOL)respondsToSelector:(SEL)aSelector {
return YES;
}
}

return NO;
}

Expand Down Expand Up @@ -126,7 +134,17 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
#endif // !TARGET_OS_WATCH

#if TARGET_OS_IOS || TARGET_OS_TV
#if TARGET_OS_WATCH

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
for (id<GULApplicationDelegate> interceptor in _interceptors) {
if ([interceptor
respondsToSelector:@selector(didFailToRegisterForRemoteNotificationsWithError:)]) {
[interceptor didFailToRegisterForRemoteNotificationsWithError:error];
}
}
}
#elif TARGET_OS_IOS || TARGET_OS_TV
- (void)application:(GULApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
for (id<GULApplicationDelegate> interceptor in _interceptors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <GoogleUtilitiesMulticastAppDelegate/GULMulticastUserNotificationCenterDelegate.h>

API_AVAILABLE(ios(10.0))
@interface GULMulticastUserNotificationCenterDelegate () <GULMulticastAppDelegateProtocol> {
NSMutableArray<id> *_interceptors;
id<UNUserNotificationCenterDelegate> _defaultAppDelegate;
}
@end

@implementation GULMulticastUserNotificationCenterDelegate

- (instancetype)init API_AVAILABLE(ios(10.0)) {
self = [super init];
if (self) {
_interceptors = [NSMutableArray array];
}
return self;
}

+ (id<GULMulticastNotificationProtocol>)multicastDelegate {
if (@available(iOS 10.0, *)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not an API_AVAILABLE(ios(10.0) on the api instead?

id<UNUserNotificationCenterDelegate> appDelegate =
[UNUserNotificationCenter currentNotificationCenter].delegate;

if (!appDelegate) {
return nil;
}
if ([appDelegate conformsToProtocol:@protocol(GULMulticastNotificationProtocol)]) {
id<GULMulticastNotificationProtocol> multicastAppDelegate =
(id<GULMulticastNotificationProtocol>)appDelegate;
return multicastAppDelegate;
}
if ([appDelegate respondsToSelector:@selector(getMulticastDelegate)]) {
id<GULMulticastNotificationProtocol> multicastDelegate =
[appDelegate performSelector:@selector(getMulticastDelegate)];
CFRetain((__bridge CFTypeRef)(multicastDelegate));
return multicastDelegate;
}
} else {
// Fallback on earlier versions
}
return nil;
}

- (id<GULMulticastAppDelegateProtocol>)getMulticastDelegate {
return self;
}

- (void)addInterceptorWithInterceptor:(id<UNUserNotificationCenterDelegate>)interceptor
API_AVAILABLE(ios(10.0)) {
[_interceptors addObject:interceptor];
}

- (void)removeInterceptorWithInterceptor:(id<UNUserNotificationCenterDelegate>)interceptor
API_AVAILABLE(ios(10.0)) {
[_interceptors removeObject:interceptor];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
if ([[self class] instancesRespondToSelector:aSelector]) {
return YES;
}
for (id<UNUserNotificationCenterDelegate> interceptor in _interceptors) {
if (interceptor && [interceptor respondsToSelector:aSelector]) {
return YES;
}
}
return NO;
}

- (void)setDefaultAppDelegate:(id<UNUserNotificationCenterDelegate>)defaultAppDelegate
API_AVAILABLE(ios(10.0)) {
[_interceptors addObject:defaultAppDelegate];
_defaultAppDelegate = defaultAppDelegate;
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
return _defaultAppDelegate;
}

#pragma mark - UNUserNotificationCenterDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
API_AVAILABLE(ios(10.0)) {
for (id<UNUserNotificationCenterDelegate> interceptor in _interceptors) {
if ([interceptor respondsToSelector:@selector(userNotificationCenter:
willPresentNotification:withCompletionHandler:)]) {
[interceptor userNotificationCenter:center
willPresentNotification:notification
withCompletionHandler:completionHandler];
}
}
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)) {
for (id<UNUserNotificationCenterDelegate> interceptor in _interceptors) {
if ([interceptor
respondsToSelector:@selector(userNotificationCenter:
didReceiveNotificationResponse:withCompletionHandler:)]) {
[interceptor userNotificationCenter:center
didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#import <Foundation/Foundation.h>

#import <GoogleUtilities/GULApplication.h>
#import <GoogleUtilities/GULAppDelegateSwizzler.h>

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -32,8 +32,6 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype)initWithAppDelegate:(id<GULApplicationDelegate>)delegate;

- (void)addInterceptorWithInterceptor:(id<GULApplicationDelegate>)delegate;

+ (id<GULMulticastAppDelegateProtocol>)multicastDelegate;
@end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
__MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14 || __TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || \
__WATCH_OS_VERSION_MAX_ALLOWED >= __WATCHOS_3_0 || TARGET_OS_MACCATALYST
#import <UserNotifications/UserNotifications.h>
#endif
#import <GoogleUtilitiesMulticastAppDelegate/GULMulticastAppDelegate.h>

NS_ASSUME_NONNULL_BEGIN

API_AVAILABLE(ios(10.0))
@protocol GULMulticastNotificationProtocol <NSObject>

- (void)addInterceptorWithInterceptor:(id<UNUserNotificationCenterDelegate>)interceptor;

- (void)removeInterceptorWithInterceptor:(id<UNUserNotificationCenterDelegate>)interceptor;

@end

API_AVAILABLE(ios(10.0))
@interface GULMulticastUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>

@property(nonatomic, copy) id<UNUserNotificationCenterDelegate> defaultAppDelegate;

+ (id<GULMulticastNotificationProtocol>)multicastDelegate;
@end

NS_ASSUME_NONNULL_END