如何从网站下载文件到用户的 iCloud
How to download a file from a website to a user's iCloud
我有一个 iOS 应用程序可以从网页下载数据文件。用户通常会浏览该网页,但还没有安装该应用程序。我不想让他们下载应用程序然后在应用程序中重新找到网页,而是下载数据文件,然后让应用程序在安装时选择它。据我所知,我需要将该文件放在 iCloud 中。但是当我尝试使用 Safari 下载文件时,它告诉我无法下载文件。如果我使用 Chrome,它希望我使用 GoogleDrive 来保存文件。网页有没有办法将文件保存到 iCloud?或者是否有其他方式告诉应用程序安装后要下载什么文件?
我是 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;
}
}
刚听到苹果官方的消息。没有办法 copy/transfer/download 一个文件(图像或其他)从互联网站点到用户的 iCloud。
通过实验我发现Safari会从网页下载一些文件类型(例如jpegs),其他文件类型会给出问题中提到的错误,但不会将任何文件放在iCloud中。因此,我的应用程序将无法使用任何文件。
我有一个 iOS 应用程序可以从网页下载数据文件。用户通常会浏览该网页,但还没有安装该应用程序。我不想让他们下载应用程序然后在应用程序中重新找到网页,而是下载数据文件,然后让应用程序在安装时选择它。据我所知,我需要将该文件放在 iCloud 中。但是当我尝试使用 Safari 下载文件时,它告诉我无法下载文件。如果我使用 Chrome,它希望我使用 GoogleDrive 来保存文件。网页有没有办法将文件保存到 iCloud?或者是否有其他方式告诉应用程序安装后要下载什么文件?
我是 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;
}
}
刚听到苹果官方的消息。没有办法 copy/transfer/download 一个文件(图像或其他)从互联网站点到用户的 iCloud。
通过实验我发现Safari会从网页下载一些文件类型(例如jpegs),其他文件类型会给出问题中提到的错误,但不会将任何文件放在iCloud中。因此,我的应用程序将无法使用任何文件。