【问题标题】:How to download a file from a website to a user's iCloud如何将文件从网站下载到用户的 iCloud
【发布时间】:2016-04-20 05:17:26
【问题描述】:

我有一个从网页下载数据文件的 iOS 应用。通常,用户会看到该网页,但尚未拥有该应用程序。我不想让他们下载应用程序,然后在应用程序中重新找到网页,我想下载数据文件,然后让应用程序在安装时选择它。据我所知,我需要将该文件放在 iCloud 中。但是当我尝试用 Safari 下载文件时,它告诉我它无法下载文件。如果我使用 Chrome,它希望我使用 GoogleDrive 来保存文件。网页有没有办法将文件保存到 iCloud?或者有没有其他方法告诉应用程序安装后要下载什么文件?

【问题讨论】:

  • 用这个link查看解决方案
  • 谢谢,但这并没有告诉我网页如何将文件下载到用户 iCloud。我可以在我的应用程序中做到这一点。但我想要浏览器下载的文件。

标签: ios download file-sharing


【解决方案1】:

我是 ios 应用程序开发的新手,但我正在尝试回答这个问题。请使用以下代码:

 - (void)download:(NSURL *)url
{
    dispatch_queue_t q_default;
    q_default = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(q_default, ^{

        NSError *error = nil;
        BOOL success = [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:url error:&error];
        if (!success)
        {
            // failed to download
        }
        else
        {
            NSDictionary *attrs = [url resourceValuesForKeys:@[NSURLUbiquitousItemIsDownloadedKey] error:&error];
            if (attrs != nil)
            {
                if ([[attrs objectForKey:NSURLUbiquitousItemIsDownloadedKey] boolValue])
                {
                    // already downloaded
                }
                else
                {
                    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
                    [query setPredicate:[NSPredicate predicateWithFormat:@"%K > 0", NSMetadataUbiquitousItemPercentDownloadedKey]];
                    [query setSearchScopes:@[url]]; // scope the search only on this item

                    [query setValueListAttributes:@[NSMetadataUbiquitousItemPercentDownloadedKey, NSMetadataUbiquitousItemIsDownloadedKey]];

                    _fileDownloadMonitorQuery = query;

                    [[NSNotificationCenter defaultCenter] addObserver:self
                                                             selector:@selector(liveUpdate:)
                                                                 name:NSMetadataQueryDidUpdateNotification
                                                               object:query];

                    [self.fileDownloadMonitorQuery startQuery];
                }
            }
        }
    });
}

- (void)liveUpdate:(NSNotification *)notification
{
    NSMetadataQuery *query = [notification object];

    if (query != self.fileDownloadMonitorQuery)
        return; // it's not our query

    if ([self.fileDownloadMonitorQuery resultCount] == 0)
        return; // no items found

    NSMetadataItem *item = [self.fileDownloadMonitorQuery resultAtIndex:0];
    double progress = [[item valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey] doubleValue];
    NSLog(@"download progress = %f", progress);

    // report download progress somehow..

    if ([[item valueForAttribute:NSMetadataUbiquitousItemIsDownloadedKey] boolValue])
    {
        // finished downloading, stop the query
        [query stopQuery];
        _fileDownloadMonitorQuery = nil;
    }
}

【讨论】:

  • 谢谢,但我希望网页将文件下载到 iCloud。也许我根本不应该提到这个应用程序。我不需要帮助应用程序在 iCloud 中发现文件。在安装我的应用程序之前,我只需要将文件放到(进入?)iCloud。如果只有某种传递参数可以通过应用商店发送。
【解决方案2】:

刚刚听到 Apple 的官方消息。无法将文件(图像或其他)从互联网站点复制/传输/下载到用户的 iCloud。

通过实验,我发现 Safari 会从网页下载某些文件类型(例如 jpegs),其他文件类型会给出问题中提到的错误,但不会将任何文件放在 iCloud 中。因此,我的应用将无法使用任何文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2018-12-29
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多