を作成するために、このコードを使用します。
アプリグループを作成し、そのグループアプリ間でデータを共有するだけで済みます。
以下は、アプリグループを有効にするための手順です。
ステップ1:Target Setting -> Capabilities -> App Group
サンプルグループ名からアプリケーショングループを有効にする:groups.companyname
またはgroups.appgroupname
![enter image description here](https://i.stack.imgur.com/MCvgE.png)
注:このグループにしたいすべてのアプリが同じ持っている必要がありますこのセクションのグループ名は です。
手順2:イメージDBのような共有データを保存できる汎用共有フォルダを取得します。
- (NSString *)getSharedGroupFolderPath:(NSString*)aAppGroupName {
NSURL *aContainerPathURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:aAppGroupName];
return [aContainerPathURL relativePath];
}
スウィフト:
static func appGroupContainerURL() -> URL? {
// 1
let fileManager = FileManager.default
guard let groupURL = fileManager
.containerURL(forSecurityApplicationGroupIdentifier: <APP_GROUP_NAME>) else {
return nil
}
let storagePathUrl = groupURL.appendingPathComponent("File Provider Storage")
let storagePath = storagePathUrl.path
// 2
if !fileManager.fileExists(atPath: storagePath) {
do {
try fileManager.createDirectory(atPath: storagePath,
withIntermediateDirectories: false,
attributes: nil)
} catch let error {
print("error creating filepath: \(error)")
return nil
}
}
// 3
return storagePathUrl
}
あなたは
のObjective-Cの下に言及としてその共有フォルダのパスを取得する必要があり、そのための
上記の方法で、グループコンテナのフォルダパスを取得して、複数のAppsで共有する必要のあるデータをここに保存できます。
ステップ3:
// Returns the file URL for the file to be saved at
static func fileUrlForDocumentNamed(_ name: String) -> URL? {
guard let baseURL = appGroupContainerURL() else { return nil }
let protectedName: String
if name.isEmpty {
protectedName = "Untitled"
} else {
protectedName = name
}
return baseURL.appendingPathComponent(protectedName)
.appendingPathExtension("your file extension")
}
・ホープ、この意志:
guard let destinationURL = TestFileViewController.fileUrlForDocumentNamed("FileName") else {
return
}
let image = UIImage.init(named: "tests")
if !FileManager.default.fileExists(atPath: destinationURL.path) {
do {
try UIImageJPEGRepresentation(image!, 1.0)!.write(to: destinationURL)
print("file saved")
} catch {
print("error saving file")
}
} else {
print("file already exists")
}
ファイルパスを取得するためのサポート方法下記の使用以下のようなコンテナー・パス上の
ストアファイルのあなたのイメージ要件を達成するのに役立ちます。
1番目のアプリから、サーバーに画像をアップロードし、2番目のアプリでサーバーから画像を取得します。 – Wolverine
はオンラインで行う必要があるため、サービスを利用することはできません。 –
共有フォルダまたはiCloudを使用します。 – Sulthan