[英]App's created Folders/Files don't show up in “Files” on iPhone
想知道是否有人可以帮助我。 我有一个应用程序,我正在尝试将一些文件移动到 iCloud 中,以便它们显示在“文件”中并云到其他设备。 我一直在网上浏览大量资源来研究问题所在,但似乎没有任何帮助。
在我的应用项目中,我在功能中打开了 iCloud 文档。
在我的 plist 文件中,我有这个:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.mypublishername.myappname</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>myappname</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
在我的权利文件中,我有:
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.com.mypublishername.myappname</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
<string>iCloud.com.mypublishername.myappname</string>
</array>
</dict>
在 ObjC 中,我正在获取 iCloud 文件夹,如下所示:
NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];
if (rootDirectory)
{
if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) [[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
gCloudFolder=rootDirectory;
}
然后,当我保存文件时,我会在本地执行此操作,并将其移动到云文件夹中,如下所示:
//
// theFilename is a file in the app's documents folder...
//
int aFile=creat(theFilename,S_IREAD|S_IWRITE);close(aFile);
aFile=open(theFilename,O_BINARY|O_RDWR);
if (aFile)
{
write(aFile,theDataPtr,theLen);
close(aFile);
if (gCloudFolder)
{
NSURL *aLocalStr=[NSURL fileURLWithPath:[NSString stringWithUTF8String:theFilename]];
NSURL *aCloudStr=[gCloudFolder URLByAppendingPathComponent:@"testing_file.txt"];
NSError *error;
if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:aLocalStr destinationURL:aCloudStr error:&error]) NSLog(@"iCloud Error occurred: %@", error);
}
所以......会发生什么。 这个文件确实被创建了。 如果我运行两次,它会告诉我它不能移动到 testing_file.txt 因为它已经存在。 此外,如果我尝试在文件上设置 Ubiquitous:NO,它会告诉我在文件未同步时无法将其设置为 no。
知道为什么我的应用程序文件夹和此文件没有显示在 iCloud 下的 FILES 文件夹中吗?
我增加了捆绑版本,这是我在其他地方看到的。 什么也没做。
我究竟做错了什么?
这完全让我震惊; 我不知道这是可能的。 我将完整描述我的测试应用程序。 它会看起来很像你的!
这是捆绑包标识符:
这里是权利:
这是权利文本:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.com.neuburg.matt.SaveIntoFilesApp</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
<string>iCloud.com.neuburg.matt.SaveIntoFilesApp</string>
</array>
</dict>
</plist>
这是 Info.plist 中的条目:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.neuburg.matt.SaveIntoFilesApp</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>MyApp</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
这是应用程序委托:
var ubiq : URL!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
DispatchQueue.global(qos:.default).async {
let fm = FileManager.default
let ubiq = fm.url(forUbiquityContainerIdentifier:nil)
print("ubiq: \(ubiq as Any)")
DispatchQueue.main.async {
self.ubiq = ubiq
}
}
return true
}
这是我点击的按钮:
@IBAction func doButton (_ sender:Any) {
if let del = UIApplication.shared.delegate as? AppDelegate {
if let ubiq = del.ubiq {
do {
let fm = FileManager.default
let docs = ubiq.appendingPathComponent("Documents")
try? fm.createDirectory(at: docs, withIntermediateDirectories: false, attributes: nil)
let url = docs.appendingPathComponent("test.txt")
print("here we go")
try? fm.removeItem(at: url)
try "howdy \(Date())".write(to: url, atomically: true, encoding: .utf8)
print("saved")
} catch {
print(error)
}
}
}
}
我确实必须增加捆绑包版本(从 1 到 2),并且我确实必须杀死并重新启动 Files 应用程序。 然后我看到了我的文件(并且可以打开并检查它):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.