Skip to content

Commit

Permalink
ARCore iOS SDK 1.37.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Dibble committed May 10, 2023
1 parent 03978f8 commit 774f726
Show file tree
Hide file tree
Showing 15 changed files with 1,456 additions and 478 deletions.
2 changes: 1 addition & 1 deletion Examples/AugmentedFacesExample/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target 'AugmentedFacesExample'
use_frameworks!
platform :ios, '11.0'
pod 'ARCore/AugmentedFaces', '~> 1.36.0'
pod 'ARCore/AugmentedFaces', '~> 1.37.0'
34 changes: 21 additions & 13 deletions Examples/CloudAnchorExample/CloudAnchorExample/CloudAnchorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
@class CloudAnchorManager;
@class GARAnchor;
@class GARFrame;
@class GARHostCloudAnchorFuture;
@class GARResolveCloudAnchorFuture;
@class GARSession;
@protocol GARSessionDelegate;

typedef NS_ENUM(NSInteger, GARCloudAnchorState);

NS_ASSUME_NONNULL_BEGIN

/**
* Delegate for receiving callbacks from a CloudAnchorManager and the underlying GARSession. All
* methods are optional.
*/
@protocol CloudAnchorManagerDelegate <GARSessionDelegate>
@protocol CloudAnchorManagerDelegate

/**
* Invoked after the ARSessionDelegate:session:didUpdateFrame: method was called on the
Expand Down Expand Up @@ -61,19 +64,21 @@ NS_ASSUME_NONNULL_BEGIN
failedToCreateRoomWithError:(NSError *)error;

/**
* Invoked after a call to GARSession:resolveCloudAnchorWithIdentifier failed.
* Invoked after a call to GARSession:resolveCloudAnchorWithIdentifier:completionHandler:error.
*
* @param manager The CloudAnchorManager which received CloudAnchorManager#createRoom.
* @param error An NSError with information about what failed.
* @param future The future for the async resolve operation, or nil if there was an error.
* @param error An NSError with information about what failed, or nil if there was no error.
*/
- (void)cloudAnchorManager:(CloudAnchorManager *)manager
resolveCloudAnchorReturnNilWithError:(NSError *)error;
startedResolvingCloudAnchor:(nullable GARResolveCloudAnchorFuture *)future
error:(nullable NSError *)error;

@end

@interface CloudAnchorManager : NSObject <ARSessionDelegate>

@property (nonatomic, weak, setter=setDelegate:) id<CloudAnchorManagerDelegate> delegate;
@property(nonatomic, weak) id<CloudAnchorManagerDelegate> delegate;

- (instancetype)initWithARSceneView:(ARSCNView *)scnView;

Expand All @@ -90,18 +95,18 @@ NS_ASSUME_NONNULL_BEGIN
* @param roomCode A room code used to store information about cloud anchors in firebase.
* @param anchor The anchor to be mapped to the given room code in firebase.
*/
- (void)updateRoom:(NSString *)roomCode withAnchor:(GARAnchor *)anchor;
- (void)updateRoom:(NSString *)roomCode withAnchorId:(NSString *)anchorId;

/**
* Attempt to resolve the last hosted anchor for the given room code. If there is a cloud anchor
* id for the room code, the completion block will be called with a stub while the cloud anchor is
* resolved.
*
* @param roomCode A room code used to store information about cloud anchors in firebase.
* @param completion Callback to invoke if the room has an anchor associated with it and a resolve
* attempt is in progress.
* @param completion Callback to invoke when the anchor has finished resolving.
*/
- (void)resolveAnchorWithRoomCode:(NSString *)roomCode completion:(void (^)(GARAnchor *))completion;
- (void)resolveAnchorWithRoomCode:(NSString *)roomCode
completion:(void (^)(GARAnchor *_Nullable, GARCloudAnchorState))completion;

/**
* Stop observing firebase messages related to the given room code.
Expand All @@ -114,12 +119,15 @@ NS_ASSUME_NONNULL_BEGIN
* Host a Cloud Anchor corresponding to the given ARAnchor.
*
* @param arAnchor An ARAnchor object to be hosted within the hotspot generated by the session.
* @param completion Callback to invoke when the anchor has finished hosting.
* @param error A return address which an error will be written to, if one occurs.
*
* @return The new GARAnchor, or `nil` if there is an error.
* @return The hosting future, or `nil` if there is an error.
*/
- (GARAnchor *)hostCloudAnchor:(ARAnchor *)arAnchor error:(NSError **)error;

- (GARHostCloudAnchorFuture *)hostCloudAnchor:(ARAnchor *)arAnchor
completion:(void (^)(NSString *_Nullable,
GARCloudAnchorState))completion
error:(NSError **)error;

/**
* Remove the passed GARAnchor from the underlying GARSession
Expand Down
104 changes: 43 additions & 61 deletions Examples/CloudAnchorExample/CloudAnchorExample/CloudAnchorManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,10 @@ - (instancetype)initWithARSceneView:(id)sceneView {
[self popupAlertWindowOnError:alertWindowTitle alertMessage:alertMessage];
return nil;
}

_gSession.delegateQueue = dispatch_get_main_queue();
}
return self;
}

- (void)setDelegate:(id<CloudAnchorManagerDelegate>)delegate {
_delegate = delegate;
self.gSession.delegate = delegate;
}

#pragma mark - ARSessionDelegate

- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame {
Expand Down Expand Up @@ -147,79 +140,68 @@ - (void)createRoom {
}];
}

- (void)updateRoom:(NSString *)roomCode withAnchor:(GARAnchor *)anchor {
- (void)updateRoom:(NSString *)roomCode withAnchorId:(NSString *)anchorId {
[[[[self.firebaseReference child:@"hotspot_list"] child:roomCode] child:@"hosted_anchor_id"]
setValue:anchor.cloudIdentifier];
setValue:anchorId];
long long timestampInteger = (long long)([[NSDate date] timeIntervalSince1970] * 1000);
NSNumber *timestamp = [NSNumber numberWithLongLong:timestampInteger];
[[[[self.firebaseReference child:@"hotspot_list"] child:roomCode] child:@"updated_at_timestamp"]
setValue:timestamp];
}

- (void)doResolveAnchor:(FIRDataSnapshot *)snapshot
roomCode:(NSString *)roomCode
completion:(void (^)(GARAnchor *, GARCloudAnchorState))completion {
NSString *anchorId = nil;
if ([snapshot.value isKindOfClass:[NSDictionary class]]) {
NSDictionary<NSString *, NSObject *> *value = (NSDictionary *)snapshot.value;
anchorId = (NSString *)value[@"hosted_anchor_id"];
}

if (anchorId) {
[[[self.firebaseReference child:@"hotspot_list"] child:roomCode] removeAllObservers];

// Now that we have the anchor ID from firebase, we resolve the anchor.
NSError *error = nil;
GARResolveCloudAnchorFuture *garFuture =
[self.gSession resolveCloudAnchorWithIdentifier:anchorId
completionHandler:completion
error:&error];

// Synchronous failure.
if (garFuture == nil) {
NSString *alertWindowTitle = @"An error occurred";
NSString *alertMessage = [NSString
stringWithFormat:@"Error resolving cloud anchor: %@", [error localizedDescription]];
[self popupAlertWindowOnError:alertWindowTitle alertMessage:alertMessage];
}

// Pass message to delegate for state management
[self.delegate cloudAnchorManager:self startedResolvingCloudAnchor:garFuture error:error];
}
}

- (void)resolveAnchorWithRoomCode:(NSString *)roomCode
completion:(void (^)(GARAnchor *))completion {
completion:(void (^)(GARAnchor *, GARCloudAnchorState))completion {
__weak CloudAnchorManager *weakSelf = self;
[[[self.firebaseReference child:@"hotspot_list"] child:roomCode]
observeEventType:FIRDataEventTypeValue
withBlock:^(FIRDataSnapshot *_Nonnull snapshot) {
CloudAnchorManager *strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}

NSString *anchorId = nil;
if ([snapshot.value isKindOfClass:[NSDictionary class]]) {
NSDictionary<NSString *, NSObject *> *value = (NSDictionary *)snapshot.value;
anchorId = (NSString *)value[@"hosted_anchor_id"];
}

if (anchorId) {
[[[strongSelf.firebaseReference child:@"hotspot_list"] child:roomCode]
removeAllObservers];

// Now that we have the anchor ID from firebase, we resolve the anchor.
// Synchronous failures will return nil. The causes may be invalid arguments, etc.
// Asynchronous failures (garAnchor is returned as a nonnull) is handled by
// session:didFailToResolveAnchor. Success is handled by the delegate methods
// session:didResolveAnchor. When garAnchor is returned as a nil, it means
// synchronous failures happened where no delegate is called. When garAnchor is
// returned as a nonnull, while some asynchronous failure happened, it is handled
// by session:didFailToResolveAnchor.
NSError *error = nil;
GARAnchor *garAnchor =
[strongSelf.gSession resolveCloudAnchorWithIdentifier:anchorId error:&error];

// Synchronous failure. Refer to the code
if (garAnchor == nil) {
NSString *alertWindowTitle = @"An error occurred";
NSString *alertMessage = [NSString
stringWithFormat:
@"GARAnchor is returned as a nil in "
@"CloudAnchorManager:resolveAnchorWithRoomCode. Error description: %@",
[error localizedDescription]];
[self popupAlertWindowOnError:alertWindowTitle alertMessage:alertMessage];

// Synchronous error in GARSession:resolveCloudAnchorWithIdentifier.
// Pass message to delegate for state management
[self.delegate cloudAnchorManager:self
resolveCloudAnchorReturnNilWithError:error];

return;
}

completion(garAnchor);
}
[weakSelf doResolveAnchor:snapshot roomCode:roomCode completion:completion];
}];
}

- (void)stopResolvingAnchorWithRoomCode:(NSString *)roomCode {
[[[self.firebaseReference child:@"hotspot_list"] child:roomCode] removeAllObservers];
}

- (GARAnchor *)hostCloudAnchor:(ARAnchor *)arAnchor error:(NSError **)error {
// To share an anchor, we call host anchor here on the ARCore session.
// session:didHostAnchor: session:didFailToHostAnchor: will get called appropriately.
return [self.gSession hostCloudAnchor:arAnchor error:error];
- (GARHostCloudAnchorFuture *)hostCloudAnchor:(ARAnchor *)arAnchor
completion:(void (^)(NSString *, GARCloudAnchorState))completion
error:(NSError **)error {
return [self.gSession hostCloudAnchor:arAnchor
TTLDays:1
completionHandler:completion
error:error];
}

- (void)removeAnchor:(GARAnchor *)anchor {
Expand Down
Loading

0 comments on commit 774f726

Please sign in to comment.