标签: uidocument

重命名iCloud文档

问题

到目前为止我所拥有的是创建新本地文件并删除iCloud文件的代码.

是否可以重命名iCloud文档,以便它保留在iCloud中?

GarageBand可以做到.可以重命名iCloud歌曲.重命名完成后,歌曲仍然在iCloud中.然而,GarageBand是Apple应用程序,因此它可能使用私有api.

我目前的代码:

- (void)moveFrom:(NSURL*)sourceURL
          moveTo:(NSString*)destinationName
      completion:(void (^)())completion
{
    MyDocument *document = [[MyDocument alloc] initWithFileURL:sourceURL];
    [document openWithCompletionHandler:^(BOOL success)
    {
        NSURL *fileURL = [self.localRoot URLByAppendingPathComponent:destinationName];
        DLog(@"Create %@", fileURL);
        [document saveToURL:fileURL
           forSaveOperation:UIDocumentSaveForCreating
          completionHandler:^(BOOL success)
        {
            NSLog(@"Saved %@", fileURL);
            [document closeWithCompletionHandler:^(BOOL success) {
                // Delete the old document from a secondary thread
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^()
                {
                    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
                    [fileCoordinator coordinateWritingItemAtURL:sourceURL
                                                        options:NSFileCoordinatorWritingForDeleting
                                                          error:nil
                                                     byAccessor:^(NSURL* writingURL) {
                        NSFileManager* fileManager = [[NSFileManager alloc] init];
                        [fileManager removeItemAtURL:writingURL error:nil];
                        DLog(@"Deleted %@", sourceURL); …
Run Code Online (Sandbox Code Playgroud)

nsfilemanager ios icloud uidocument nsfilecoordinator

4
推荐指数
1
解决办法
1606
查看次数

UIDocument openWithCompletionHandler返回NO表示成功

我有这个代码:

[metaDoc openWithCompletionHandler:^(BOOL success) {
    if (!success) {
        NSLog(@"UH OH");
    }
}];
Run Code Online (Sandbox Code Playgroud)

为什么这会回归NO?对我来说,即使在我身上,它也会回归NO

- (BOOL)loadFromContents:(id)contents 
                  ofType:(NSString *)typeName 
                   error:(NSError **)outError 
Run Code Online (Sandbox Code Playgroud)

方法,我总是返回YES.

谢谢

cocoa-touch objective-c ios icloud uidocument

3
推荐指数
1
解决办法
985
查看次数

Mac 和 iOS 应用程序之间通用的 iCloud 容器?

我想构建一个基于 iCloud 的应用程序,可以在 Apple 平台、iOS 和 Mac 上运行。该应用程序将使用 iOS 上的 UIDocument 和 Mac 上的 NSDocument 来拥有基于文档的同步模型 - 在 iOS 上创建的文档将在 Mac 上可用,反之亦然。

我面临的具体问题是我想在两个应用程序上使用相同的 iCloud 容器。XCode 根据 App ID 为应用程序创建一个默认容器,两个应用程序的 ID 必须不同,因为这似乎是 Apple 的要求 - 没有两个应用程序可以具有相同的应用程序 ID,即使它们位于不同的平台上。

因此,我保留了默认创建的容器,并在 Mac 应用程序和 iOS 应用程序上添加了另一个容器。该应用程序似乎工作正常,我可以在两个应用程序中的正确位置创建一个新文件包,但我遇到了两个问题:

  1. 该容器未正确显示在 iCloud 设置管理屏幕中。在 XCode 的权利部分中,我将容器命名为com.mycompany.myapp.mycontainer两个应用程序中的容器。在 iOS 的设置屏幕(设置 -> 常规 -> 使用 -> 管理存储 -> 显示全部)中,我的应用程序被列为“mycontainer”,而不是我预期的应用程序的实际名称。是否可以在应用程序的 Info.plist 中提供名称和图标以使其正确列出?

  2. 我的 NSMetadataQuery 无法正常工作。我使用空应用程序模板制作了 iOS 应用程序,因此 Info.plist 未使用 CFBundleDocumentTypes 键正确设置,但我发现并修复了它。为了在 iOS 应用程序中触发 NSMetadataQuery,我使用:

    [self.queryCloud setPredicate:[NSPredicate predicateWithFormat:@"%K like '*.myextension*'",NSMetadataItemFSNameKey]];

    搜索范围设置如下: …

cocoa cocoa-touch ios icloud uidocument

3
推荐指数
1
解决办法
1591
查看次数

UIDocument永远不会调用dealloc

我有一个问题,我似乎无法dealloc UIDocument(在iCloud中使用)

运行NSMetaDataQuery后查找文件如下..

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
_query = query;
[query setSearchScopes:[NSArray arrayWithObject:
                        NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat: 
                     @"%K == %@", NSMetadataItemFSNameKey, kFILENAME];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(queryDidFinishGathering:) 
 name:NSMetadataQueryDidFinishGatheringNotification 
 object:query];

[query startQuery];
Run Code Online (Sandbox Code Playgroud)

我处理我的查询

- (void)queryDidFinishGathering:(NSNotification *)notification {

NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:NSMetadataQueryDidFinishGatheringNotification
                                              object:query];

_query = nil;

[self loadData:query];

}
Run Code Online (Sandbox Code Playgroud)

然后加载或创建一个新文档.

- (void)loadData:(NSMetadataQuery *)query {

if ([query resultCount] == 1) {

    NSMetadataItem *item = [query resultAtIndex:0];
    NSURL *url = …
Run Code Online (Sandbox Code Playgroud)

retain dealloc ios icloud uidocument

3
推荐指数
1
解决办法
601
查看次数

UIDocument openWithCompletionHandler:崩溃

我在我的项目中使用Core Data,并在以下代码部分中遇到罕见的崩溃

 -(void) useDocument{
     AFFormsCoreDataEngine* engine = [AFFormsCoreDataEngine sharedInstance];
     if (![[NSFileManager defaultManager] fileExistsAtPath: [engine.formsDatabase.fileURL path]])
     {
         [engine.formsDatabase saveToURL: engine.formsDatabase.fileURL forSaveOperation: UIDocumentSaveForCreating completionHandler: ^(BOOL success){
            // setup
         }];
    }
    else if (engine.formsDatabase.documentState == UIDocumentStateClosed)
    {
        [engine.formsDatabase openWithCompletionHandler: ^(BOOL success){
             // setup
        }];
    }
    else if (engine.formsDatabase.documentState == UIDocumentStateNormal)
    {
         // setup
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是崩溃日志所说的:

Last Exception Backtrace:
0   CoreFoundation                  0x371fd88f __exceptionPreprocess + 163
1   libobjc.A.dylib                 0x31272259 objc_exception_throw + 33
2   CoreFoundation                  0x371fd789 +[NSException raise:format:] + 1
3   Foundation                      0x32ce83a3 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] …
Run Code Online (Sandbox Code Playgroud)

crash core-data ios uidocument

3
推荐指数
1
解决办法
2694
查看次数

iOS Swift 3以编程方式将文件复制到iCloud驱动器

我有文件下载选项。当用户从我的应用程序下载文档时,我需要将其存储到已经安装在用户移动设备中的用户iCloud Drive中。我已经在Web和Xcode中都配置了iCloud,但是问题是我无法正确将文件复制到iCloud Drive。文件已成功下载,并且也已移至iCloud,但文件不会出现在iCloud Drive App中。这是我尝试的代码:

<key>NSUbiquitousContainers</key>
    <dict>
        <key>iCloud.MyAppBundleIdentifier</key>
        <dict>
            <key>NSUbiquitousContainerIsDocumentScopePublic</key>
            <true/>
            <key>NSUbiquitousContainerName</key>
            <string>iCloudDriveDemo</string>
            <key>NSUbiquitousContainerSupportedFolderLevels</key>
            <string>Any</string>
        </dict>
    </dict>
Run Code Online (Sandbox Code Playgroud)

这是我的iCloud存储代码:

func DownloadDocumnt()
    {
        print("Selected URL: \(self.SelectedDownloadURL)")
        let fileURL = URL(string: "\(self.SelectedDownloadURL)")!

        let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
        let destinationFileUrl = documentsUrl.appendingPathComponent("Libra-\(self.SelectedDownloadFileName)")

        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)

        let request = URLRequest(url:fileURL)

        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil
            {
                if let statusCode = (response as? …
Run Code Online (Sandbox Code Playgroud)

ios icloud uidocument swift3

3
推荐指数
2
解决办法
4936
查看次数

如何解读document.state == 12(iCloud)

每当我从iCloud加载UIDocument时,我会像这样检查它的状态:

NSLog(@"Library loadFromContents: state = %d", self.documentState);
Run Code Online (Sandbox Code Playgroud)

在某些情况下,我收到了导致崩溃的8号或12号文件.我现在想知道8和12究竟是什么代表什么.据我所知,documentState是一个位字段,因此它有许多不同的标志.该文件表明:

enum {
UIDocumentStateNormal          = 0,
UIDocumentStateClosed          = 1 << 0,
UIDocumentStateInConflict      = 1 << 1,
UIDocumentStateSavingError     = 1 << 2,
UIDocumentStateEditingDisabled = 1 << 3   }; 
typedef NSInteger UIDocumentState;
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何在我的情况下解释这一点.我如何找出8和12代表什么?

iphone objective-c ios5 icloud uidocument

2
推荐指数
1
解决办法
854
查看次数

iPhone:在Instagram上上传图片

我已经编写了在instagram上传图像的代码,如下所示:

CGRect rect = CGRectMake(20 ,20 , 200, 200);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *jpgPath = [documentsDirectory stringByAppendingPathComponent:@"after2sm.ig"];

    NSString *fileURL = [NSString stringWithFormat:@"file://%@",jpgPath];
    NSURL *igImageHookFile = [NSURL fileURLWithPath:fileURL];

    self.dic.UTI = @"com.instagram.photo";

    self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
    self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];

    [NSDictionary dictionaryWithObject:@"My Caption" forKey:@"InstagramCaption"];
    [self.dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];
Run Code Online (Sandbox Code Playgroud)

但是它会打开一个带有"instagram"选项的动作表,但是当我点击它没有任何反应时,请你帮助我

iphone instagram uidocument ios6

2
推荐指数
1
解决办法
458
查看次数

如何初始化 UIDocumentPickerViewController 注意到“forOpeningContentTypes”(来自 doco)似乎在 IOS 中已被弃用?

当使用 Xcode 12.5.1 和 IOS 14.5 部署目标遵循 UIDocumentPickerViewController 的现场 IOS 文档时,我收到错误:

Incorrect argument label in call (have 'forOpeningContentTypes:', expected 'forExporting:')
Run Code Online (Sandbox Code Playgroud)

关于现在应该如何使用 IOS UIDocumentPickerViewController 有什么建议吗?

我的代码除了:

// Create a document picker for directories.
let documentPicker =
    UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
Run Code Online (Sandbox Code Playgroud)

错误快照:

在此输入图像描述

有关如何使用文档选择器的文档,请参见此处:

https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories给出:

xcode ios uidocument swift uidocumentpickerviewcontroller

2
推荐指数
1
解决办法
572
查看次数

调用updateChangeCount时,UIDocument自动保存不起作用

我有一个自定义子类,UIDocument用于存储我的应用程序的用户内容.我-[UIDocument updateChangeCount:UIDocumentChangeDone]直接打电话来跟踪对文档的更改.保存和加载工作正常,但文档永远不会自动保存.为什么会这样?

uikit ios uidocument

1
推荐指数
1
解决办法
589
查看次数

处理包含多个图像的 UIDocument 中的图像加载/存储

我有一个简单的模型对象,Location其中包含一些文本项和一个images : [UIImages]?. Location所以Codable我将文本位存储为 JSON,然后将图像写入相同的FileWrapper.

我的问题是如何存储图像文件和 [UIImage] 数组之间的关系。图像必须以相同的顺序返回。有没有办法可以连接到编码,以便数组被指向图像的 URL 替换?

或者,我应该始终将图像作为单独的文件(例如在缓存目录中)并将 [UIImage] 替换为 [URL]

nsfilewrapper ios uidocument swift

1
推荐指数
1
解决办法
983
查看次数

将照片导出到Instagram

我正在尝试使用以下代码将照片从我的应用程序导出到Instagram:

// Capture Screenshot
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,self.view.opaque,0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
dic.UTI = @"com.instagram.photo";
dic.annotation = [NSDictionary dictionaryWithObject:@"my caption" forKey:@"InstagramCaption"];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
  [[UIApplication sharedApplication] openURL:instagramURL];
} else {
  NSLog(@"No Instagram Found");
}
Run Code Online (Sandbox Code Playgroud)

但它没有用.它崩溃时出现以下错误:

2013-01-19 20:40:41.948 Ayat [12648:c07] *由于未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:'* - [NSURL initFileURLWithPath:]:nil string parameter'

我不确定我是否正确:

1-保存jpgpath作为我拍摄的截图.

2-将"dic"文档传递给Instagram.

iphone xcode ios instagram uidocument

0
推荐指数
1
解决办法
4994
查看次数