簡體   English   中英

iOS模擬器未將“文件”應用顯示為NSFileproviderExtension的選項

[英]iOS Simulator doesn't show Files app as option for NSFileproviderExtension

我的應用程序無法在iOS 11文件應用程序中顯示為某個位置時遇到問題。 我添加了NSFileproviderExtension 但是,當我運行它時,我只能選擇使用野生動物園和其他一些應用程序。 當我選擇野生動物園,然后關閉野生動物園,打開文件時,它的確顯示了我的擴展名,但是當我單擊任何內容時,它就會崩潰。 有最佳方​​法的想法嗎? 有什么好的教程嗎?

@interface FileProviderExtension ()

@property (nonatomic, readonly, strong) NSFileManager *fileManager;

@end

@implementation FileProviderExtension

- (instancetype)init {
    if (self = [super init]) {
        _fileManager = [[NSFileManager alloc] init];
    }
    return self;
}

- (nullable NSFileProviderItem)itemForIdentifier:(NSFileProviderItemIdentifier)identifier error:(NSError * _Nullable *)error {
    // resolve the given identifier to a record in the model

    // TODO: implement the actual lookup
    NSFileProviderItem item = nil;

    return item;
}

- (nullable NSURL *)URLForItemWithPersistentIdentifier:(NSFileProviderItemIdentifier)identifier {
    // resolve the given identifier to a file on disk
    NSFileProviderItem item = [self itemForIdentifier:identifier error:NULL];
    if (!item) {
        return nil;
    }

    // in this implementation, all paths are structured as <base storage directory>/<item identifier>/<item file name>
    NSFileProviderManager *manager = [NSFileProviderManager defaultManager];
    NSURL *perItemDirectory = [manager.documentStorageURL URLByAppendingPathComponent:identifier isDirectory:YES];

    return [perItemDirectory URLByAppendingPathComponent:item.filename isDirectory:NO];
}

- (nullable NSFileProviderItemIdentifier)persistentIdentifierForItemAtURL:(NSURL *)url {
    // resolve the given URL to a persistent identifier using a database
    NSArray <NSString *> *pathComponents = [url pathComponents];

    // exploit the fact that the path structure has been defined as
    // <base storage directory>/<item identifier>/<item file name> above
    NSParameterAssert(pathComponents.count > 2);

    return pathComponents[pathComponents.count - 2];
}

- (void)startProvidingItemAtURL:(NSURL *)url completionHandler:(void (^)(NSError *))completionHandler {
    // Should ensure that the actual file is in the position returned by URLForItemWithIdentifier:, then call the completion handler

    /* TODO:
     This is one of the main entry points of the file provider. We need to check whether the file already exists on disk,
     whether we know of a more recent version of the file, and implement a policy for these cases. Pseudocode:

     if (!fileOnDisk) {
         downloadRemoteFile();
         callCompletion(downloadErrorOrNil);
     } else if (fileIsCurrent) {
         callCompletion(nil);
     } else {
         if (localFileHasChanges) {
             // in this case, a version of the file is on disk, but we know of a more recent version
             // we need to implement a strategy to resolve this conflict
             moveLocalFileAside();
             scheduleUploadOfLocalFile();
             downloadRemoteFile();
             callCompletion(downloadErrorOrNil);
         } else {
             downloadRemoteFile();
             callCompletion(downloadErrorOrNil);
         }
     }
     */

    completionHandler([NSError errorWithDomain:NSCocoaErrorDomain code:NSFeatureUnsupportedError userInfo:@{}]);
}


- (void)itemChangedAtURL:(NSURL *)url {
    // Called at some point after the file has changed; the provider may then trigger an upload

    /* TODO:
     - mark file at <url> as needing an update in the model
     - if there are existing NSURLSessionTasks uploading this file, cancel them
     - create a fresh background NSURLSessionTask and schedule it to upload the current modifications
     - register the NSURLSessionTask with NSFileProviderManager to provide progress updates
     */
}

- (void)stopProvidingItemAtURL:(NSURL *)url {
    // Called after the last claim to the file has been released. At this point, it is safe for the file provider to remove the content file.

    // TODO: look up whether the file has local changes
    BOOL fileHasLocalChanges = NO;

    if (!fileHasLocalChanges) {
        // remove the existing file to free up space
        [[NSFileManager defaultManager] removeItemAtURL:url error:NULL];

        // write out a placeholder to facilitate future property lookups
        [self providePlaceholderAtURL:url completionHandler:^(NSError * __nullable error) {
            // TODO: handle any error, do any necessary cleanup
        }];
    }
}

#pragma mark - Actions

/* TODO: implement the actions for items here
 each of the actions follows the same pattern:
 - make a note of the change in the local model
 - schedule a server request as a background task to inform the server of the change
 - call the completion block with the modified item in its post-modification state
 */

#pragma mark - Enumeration

- (nullable id<NSFileProviderEnumerator>)enumeratorForContainerItemIdentifier:(NSFileProviderItemIdentifier)containerItemIdentifier error:(NSError **)error {
    id<NSFileProviderEnumerator> enumerator = nil;
    if ([containerItemIdentifier isEqualToString:NSFileProviderRootContainerItemIdentifier]) {
        // TODO: instantiate an enumerator for the container root
    } else if ([containerItemIdentifier isEqualToString:NSFileProviderWorkingSetContainerItemIdentifier]) {
        // TODO: instantiate an enumerator for the working set
    } else {
        // TODO: determine if the item is a directory or a file
        // - for a directory, instantiate an enumerator of its subitems
        // - for a file, instantiate an enumerator that observes changes to the file
    }

    return enumerator;
}

@end

由於某些原因,Xcode不會顯示“文件”應用程序來調試您的NSFileproviderExtension

無論如何要測試它,只需選擇任何其他應用程序,在該應用程序中搜索“共享”按鈕,然后在共享屏幕中選擇“文件”。 您也可以關閉應用程序,然后轉到擴展名將顯示應用程序並進行調試的文件(斷點,控制台消息等)。

至於崩潰,添加一個異常斷點以停止導致崩潰的代碼行(就像調試主程序時那樣)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM