首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSSharingService performWithItems挂起

NSSharingService performWithItems挂起
EN

Stack Overflow用户
提问于 2018-05-07 07:40:41
回答 1查看 326关注 0票数 1

我有一个工作的共享例程,但现在它坏了。我已经有一段时间没有检查或修改过它了,现在发现它无法操作。当我打电话的时候

代码语言:javascript
复制
[sharingService performWithItems:[NSArray arrayWithObject:itemProvider]];

我得到了一个共享工作表。它显示共享的当前成员。该表单不可操作,不接受任何输入或点击。我无法添加、删除或完全停止共享。当我关闭窗体时,我的应用程序挂起,不会响应或获得焦点。我必须关闭应用程序并重新打开它,才能让它再次工作。

在过去的几个月里,这个方法运行得很好。我没有改变任何东西,所以我对新的问题感到非常惊讶。

我在这里添加了创建共享的代码:

代码语言:javascript
复制
NSString *shareOption = [[NSUserDefaults standardUserDefaults] objectForKey:kSet_CLOUD_SERVICE_USER_DEFAULT];
if ([shareOption isEqualToString:TTICloudKitShareOwnerService]) {
    CDEZipCloudFileSystem *zipFile = (CDEZipCloudFileSystem *)_cloudFileSystem;
    CDECloudKitFileSystem *fileSystem = (CDECloudKitFileSystem *)zipFile.cloudFileSystem;

    NSItemProvider *itemProvider = [[NSItemProvider alloc] init];
    [itemProvider registerCloudKitShare:fileSystem.share container:fileSystem.container];

    NSSharingService *sharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameCloudSharing];
    sharingService.subject = @"Share Workforce Data";
    sharingService.delegate = self;
    if ([sharingService canPerformWithItems:[NSArray arrayWithObject:itemProvider]]) {
        [sharingService performWithItems:[NSArray arrayWithObject:itemProvider]];

    // This is the point at which the Apple UI is presented but inoperable.
    // No changes can be made to the share.
    // The only way to dismiss the dialog is to quit or press escape.
    // Upon dismissal the app is either crashed or hung up.
    // Quitting the app and restart is only option to use the app again.
    // If not run from Xcode, requires force quit.

    }
} else {
    NSLog(@"Is Shared Ensemble");
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"Stop Share"];
    [alert addButtonWithTitle:@"Cancel"];
    [alert setMessageText:@"Shared Data Options"];
    [alert setInformativeText:@"You are participating in a shared file. Stop sharing will remove your participation and reset your data. You will no longer participate or have access to the shared information."];
    [alert setAlertStyle:NSAlertStyleWarning];

    if ([alert runModal] == NSAlertFirstButtonReturn) {
        [alert setInformativeText:@"Are you sure? You will no longer have access to shared data. You will need the owner of the share to resend an invitation to join the share."];
        if ([alert runModal] == NSAlertFirstButtonReturn) {
            // This actually does not remove user from sharing as intended.
            // I am sure that is my own implementation incomplete though.
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setNilValueForKey:kSet_CLOUDKIT_SHARE_OWNER_DEFAULT];
            [defaults setObject:TTICloudKitShareOwnerService forKey:kSet_CLOUD_SERVICE_USER_DEFAULT];
            [defaults synchronize];
            [self disconnectFromSyncServiceWithCompletion:^{
                // TODO: Need to wipe the existing Core Data info here. Leave them with no access to shared data.
                //      Also need to remove self from the share?
                [self reset];
                [self setupEnsemble];
            }];
        }
    }
}

创建分享和发送功能运行得天衣无缝,我一直在开发应用程序并进行现场测试。目前,我的测试与另外两个用户共享,并且仍然有效。事实上,我似乎找不到一种方法来停止与这些用户的分享,或者以任何方式改变当前的分享。

这是NSCloudSharingServiceDelegate代码:

代码语言:javascript
复制
-(NSCloudKitSharingServiceOptions)optionsForSharingService:(NSSharingService *)cloudKitSharingService shareProvider:(NSItemProvider *)provider
{
  return NSCloudKitSharingServiceAllowPrivate | NSCloudKitSharingServiceAllowReadWrite;
}

-(void)sharingService:(NSSharingService *)sharingService willShareItems:(NSArray *)items
{
  DLog(@"Will Share Called with items:%@",items);
}

-(void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items
{
  DLog(@"Did share called");
}

-(void)sharingService:(NSSharingService *)sharingService didFailToShareItems:(NSArray *)items error:(NSError *)error
{
  DLog(@"Sharing service failed to share items, %@-", error);
  if (error.code == NSUserCancelledError) return;
  DLog(@"Failed to share, error- %@", error.userInfo);
  [self disconnectFromSyncServiceWithCompletion:^{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:kSet_CLOUDKIT_SHARE_OWNER_DEFAULT forKey:kSet_CLOUD_SERVICE_USER_DEFAULT];
    [defaults setNilValueForKey:kSet_CLOUDKIT_SHARE_OWNER_DEFAULT];
    [defaults synchronize];
  }];
}

显然,我是少数几个发现这一点很重要的人之一,因为我在网上搜索过,发现几乎没有人讨论它。苹果的文档几乎是零。

这是无法工作的Apple UI的屏幕截图:

EN

回答 1

Stack Overflow用户

发布于 2018-07-11 21:21:37

我张贴这篇文章作为一个答案,更像是我设法得到的一项工作。对于为什么Apple UI没有响应,仍然没有答案。

请参阅在没有Apple UI的情况下邀请参与者的代码。

代码语言:javascript
复制
-(void)addParticipantWithEmail:(NSString *)email toShare:(CKShare *)share inContainer:(CKContainer *)container
{
    [container discoverUserIdentityWithEmailAddress:(email) completionHandler:^(CKUserIdentity * _Nullable userInfo, NSError * _Nullable error) {
        if (!userInfo || error) {
            NSLog(@"Participant was not found for email %@", email);
            if (error) {
                NSLog(@"Error: %@", error.userInfo);
            } else {
                NSLog(@"No error was provided");
            }

            // abort
            return;
        }
        CKFetchShareMetadataOperation *fetchMetaDataOperation = [[CKFetchShareMetadataOperation alloc] initWithShareURLs:[NSArray arrayWithObject:share.URL]];
        fetchMetaDataOperation.shouldFetchRootRecord = YES;
        [fetchMetaDataOperation setPerShareMetadataBlock:^(NSURL * _Nonnull shareURL, CKShareMetadata * _Nullable shareMetadata, NSError * _Nullable error) {
            CKRecord *root = shareMetadata.rootRecord;

            if (!root) {
                NSLog(@"There was an error retrieving the root record- %@", error);
            } else {
                NSLog(@"Root is %@", root);
                NSLog(@"/n");
            }

            CKUserIdentityLookupInfo *info = userInfo.lookupInfo;
            CKFetchShareParticipantsOperation *fetchOperation = [[CKFetchShareParticipantsOperation alloc] initWithUserIdentityLookupInfos:[NSArray arrayWithObject:info]];
            [fetchOperation setShareParticipantFetchedBlock:^(CKShareParticipant * _Nonnull participant) {
                participant.permission = CKShareParticipantPermissionReadWrite;
                [share addParticipant:participant];
                CKModifyRecordsOperation *modifyOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:[NSArray arrayWithObjects:root, share, nil] recordIDsToDelete:nil];
                modifyOperation.savePolicy = CKRecordSaveIfServerRecordUnchanged;
                [modifyOperation setPerRecordCompletionBlock:^(CKRecord * _Nonnull record, NSError * _Nullable error) {
                    if (error) {
                        DLog(@"Error modifying record %@. UserInfo: %@", record, error.userInfo);
                    } else {
                        DLog(@"No Error Reported in Modify Operation");
                    }
                }];
                [container.privateCloudDatabase addOperation:modifyOperation];
            }];
            [fetchOperation setFetchShareParticipantsCompletionBlock:^(NSError * _Nullable operationError) {
                if (operationError) {
                    NSLog(@"There was en error in the fetch operation- %@", operationError.userInfo);
                    // Error may be a network issue, should implement a retry and possibly a limit to how many times to run it
                }
            }];
            [container addOperation:fetchOperation];
        }];
        [container addOperation:fetchMetaDataOperation];
    }];
}

现在看来,如果我将电子邮件地址传递给此功能,他们将被成功邀请分享,前提是用户在我的联系人中并允许发现。

此时,我通过iMessage手动将共享链接发送给用户。已从控制台复制URL。我的目的是现在提供我自己的表单来处理这个问题。

在接收链路上,我使用集成方法:

代码语言:javascript
复制
CDECloudKitFileSystem acceptInvitationToShareWithMetadata:metadata completion:^(NSError *error)

这段代码似乎不起作用,最初接受邀请失败。在没有任何改变的情况下,接受共享开始工作。我不确定初始失败的原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50205484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档