66// Released under the MIT license
77// http://opensource.org/licenses/mit-license.php
88//
9-
9+ # import < UserNotifications/UserNotifications.h >
1010#import < Photos/Photos.h>
1111#import < MobileCoreServices/UTCoreTypes.h>
1212#import " DPHostCameraRecorder.h"
1313#import " DPHostRecorder.h"
1414#import " DPHostUtils.h"
1515#import " DPHostRecorderUtils.h"
16- @interface DPHostCameraRecorder ()
16+
17+ static NSString *const kDPHostStartPreviewNotificationId = @" kDPHostStartPreviewNotificationId" ;
18+ @interface DPHostCameraRecorder ()<UNUserNotificationCenterDelegate >
1719@property (nonatomic ) DPHostSimpleHttpServer *httpServer;
1820// / Preview APIでプレビュー画像URIの配送を行うかどうか。
1921@property (nonatomic ) BOOL sendPreview;
@@ -117,14 +119,15 @@ - (void)takePhotoWithSuccessCompletion:(void (^)(NSURL *assetURL))successComplet
117119 }
118120 [weakSelf saveFileWithCompletionHandler: ^(NSURL *assetURL, NSError *error) {
119121
120- if ([weakSelf.session isRunning ]) {
122+ if ([weakSelf.session isRunning ] && !weakSelf. sendPreview ) {
121123 [weakSelf.session stopRunning ];
122124 }
123125 if (error) {
124126 failCompletion (error.localizedDescription );
125127 } else {
126128 successCompletion (assetURL);
127129 }
130+
128131 }];
129132 }];
130133}
@@ -179,6 +182,7 @@ - (void)startWebServerWithSuccessCompletion:(void (^)(NSString *uri))successComp
179182 failCompletion (@" MJPEG Server cannot running." );
180183 return ;
181184 }
185+ [self showPreviewNotification ];
182186 successCompletion (url);
183187}
184188
@@ -193,7 +197,7 @@ - (void)stopWebServer
193197 self.sendPreview = NO ;
194198 // 次回プレビュー開始時に影響を与えない為に、初期値(無効値)を設定する。
195199 self.lastPreviewTimestamp = kCMTimeInvalid ;
196-
200+ [ self hidePreviewNotification ];
197201}
198202
199203#pragma mark - Private Method
@@ -420,4 +424,60 @@ - (void) sendPreviewDataWithSampleBuffer:(CMSampleBufferRef)sampleBuffer
420424 }
421425
422426}
427+
428+ #pragma mark - Notification Control
429+ - (void )showPreviewNotification
430+ {
431+ UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc ] init ];
432+ content.title = @" カメラ撮影中(iOS Host Camera Preview)" ;
433+ content.body = @" タップして撮影を停止します。" ;
434+ // Deliver the notification in five seconds.
435+ UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger
436+ triggerWithTimeInterval: 1
437+ repeats: NO ];
438+ UNNotificationRequest * nRequest = [UNNotificationRequest
439+ requestWithIdentifier: kDPHostStartPreviewNotificationId
440+ content: content
441+ trigger: trigger];
442+
443+ // Schedule the notification.
444+ dispatch_async (dispatch_get_main_queue (), ^{
445+ UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter ];
446+ center.delegate = self;
447+ [center addNotificationRequest: nRequest
448+ withCompletionHandler: ^(NSError * _Nullable error) {
449+ }];
450+
451+ });
452+ }
453+
454+ - (void )hidePreviewNotification
455+ {
456+ UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter ];
457+ [center getDeliveredNotificationsWithCompletionHandler: ^(NSArray <UNNotification *> * _Nonnull notifications) {
458+ for (UNNotification *notification in notifications) {
459+ NSString *currentId = notification.request .identifier ;
460+ if ([currentId isEqualToString: kDPHostStartPreviewNotificationId ]) {
461+ [center removeDeliveredNotificationsWithIdentifiers: @[kDPHostStartPreviewNotificationId ]];
462+ return ;
463+ }
464+ }
465+ }];
466+
467+ }
468+ #pragma mark - Notification Delegate
469+ - (void )userNotificationCenter : (UNUserNotificationCenter *)center
470+ didReceiveNotificationResponse : (UNNotificationResponse *)response
471+ withCompletionHandler : (void (^)(void ))completionHandler {
472+ completionHandler ();
473+ [self stopWebServer ];
474+ }
475+
476+ - (void )userNotificationCenter : (UNUserNotificationCenter *)center
477+ willPresentNotification : (UNNotification *)notification
478+ withCompletionHandler : (void (^)(UNNotificationPresentationOptions ))completionHandler {
479+ completionHandler (UNNotificationPresentationOptionBadge |
480+ UNNotificationPresentationOptionSound |
481+ UNNotificationPresentationOptionAlert );
482+ };
423483@end
0 commit comments