菜鸟教程小白 发表于 2022-12-11 18:49:18

iOS 模拟器不显示文件应用程序作为 NSFileproviderExtension 的选项


                                            <p><p>我在让我的应用显示为 iOS 11 文件应用中的位置时遇到了一些问题。我添加了一个 <code>NSFileproviderExtension</code>。但是,当我运行它时,我只能选择 safari 和其他一些应用程序。当我选择 safari,然后关闭 safari,打开文件时,它确实显示了我的扩展名,但是当我单击任何内容时它崩溃了。关于最好的方法的任何想法?有什么好的教程吗?</p>

<pre><code>@interface FileProviderExtension ()

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

@end

@implementation FileProviderExtension

- (instancetype)init {
    if (self = ) {
      _fileManager = [ 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 = ;
    if (!item) {
      return nil;
    }

    // in this implementation, all paths are structured as &lt;base storage directory&gt;/&lt;item identifier&gt;/&lt;item file name&gt;
    NSFileProviderManager *manager = ;
    NSURL *perItemDirectory = ;

    return ;
}

- (nullable NSFileProviderItemIdentifier)persistentIdentifierForItemAtURL:(NSURL *)url {
    // resolve the given URL to a persistent identifier using a database
    NSArray &lt;NSString *&gt; *pathComponents = ;

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

    return pathComponents;
}

- (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();
}


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

    /* TODO:
   - mark file at &lt;url&gt; 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
      [ 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&lt;NSFileProviderEnumerator&gt;)enumeratorForContainerItemIdentifier:(NSFileProviderItemIdentifier)containerItemIdentifier error:(NSError **)error {
    id&lt;NSFileProviderEnumerator&gt; enumerator = nil;
    if () {
      // TODO: instantiate an enumerator for the container root
    } else if () {
      // 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
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>由于某种原因,Xcode 不会显示 Files 应用程序来调试您的 <code>NSFileproviderExtension</code>。</p>

<p>无论如何要测试它,只需选择任何其他应用程序,在该应用程序中搜索“共享”按钮,然后在共享屏幕中选择"file"。
您还可以关闭应用程序并转到文件,您的扩展程序将在其中显示应用程序并进行调试(断点、控制台消息等)。</p>

<p>对于崩溃,添加一个异常断点以在导致崩溃的代码行处停止(就像调试主程序时所做的那样)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 模拟器不显示文件应用程序作为 NSFileproviderExtension 的选项,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46454839/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46454839/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 模拟器不显示文件应用程序作为 NSFileproviderExtension 的选项