2017-02-08 20 views
1

2つのiOSアプリをAとBとしたいとします。次に、Aアプリでユーザーのギャラリーから5枚の画像をアップロードしますこれらの5つの画像をアプリBで表示します。これをDocument Directoryで試しましたが、各アプリに独自のドキュメントディレクトリがあり、他のアプリはそのドキュメントディレクトリにアクセスできないことがわかりました。今私はこれを行う方法を知らない。どんな助け?1つのデバイスに2つのアプリがあり、最初のアプリからアップロードされた画像が2番目のアプリに表示されます

申し訳ありませんがオフラインで済むようにしておきたいので、両方のアプリでapiを使用することはできません。

+0

1番目のアプリから、サーバーに画像をアップロードし、2番目のアプリでサーバーから画像を取得します。 – Wolverine

+0

はオンラインで行う必要があるため、サービスを利用することはできません。 –

+0

共有フォルダまたはiCloudを使用します。 – Sulthan

答えて

0

を作成するために、このコードを使用します。

アプリグループを作成し、そのグループアプリ間でデータを共有するだけで済みます。

以下は、アプリグループを有効にするための手順です。

ステップ1:Target Setting -> Capabilities -> App Group

サンプルグループ名からアプリケーショングループを有効にする:groups.companynameまたはgroups.appgroupname

enter image description here

注:このグループにしたいすべてのアプリが同じ持っている必要がありますこのセクションのグループ名は です。

手順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") 
     } 

ファイルパスを取得するためのサポート方法下記の使用以下のようなコンテナー・パス上の

ストアファイルのあなたのイメージ要件を達成するのに役立ちます。

+0

あなたの答えをありがとう。 –

+0

そのグループコンテナフォルダにイメージを追加して他のアプリケーションでイメージを取得する方法を教えてください。 –

+0

確かに1時間私は会議に出る必要があるので私の答えを更新します。ありがとう。 – CodeChanger

-2

オブジェクト - C

NSURL *url = [NSURL URLWithString:@"TestB://com.dali.TestB"]; 
[[UIApplication sharedApplication] openURL:url]; 

スウィフト

let url = NSURL(string: "TestB://com.dali.TestB")! 
UIApplication.sharedApplication().openURL(url) 

画像data.when画像を転送するURLスキームを使用するには、この方法が良いではない、非常に多くのです。

+0

あなたのポイントを正確に取得できませんでした。 –

0

グループアプリを使用できます。 Project Navigator - >機能 - >グループアプリをオンにします。グループ名の同義語は同じ名前である必要があります。また、開発者アカウントで有効にする必要があります。共有スペースへのディレクトリを作成し、両方からデータまたはイメージにアクセスします。 enter image description here

//はい、あなたがiOS 8.0と上記の新しいアップデートごとにアプリのグループ内の一部のデータを共有できる共有ディレクトリ

let fileManager = FileManager.default 

    guard let groupUrl = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "your app group name") else { 
     return nil 
    } 

    var storagePathUrl = groupUrl.appendingPathComponent("FileProviderStorage") 
    let storagePath = storagePathUrl.path 

    if !fileManager.fileExists(atPath: storagePath) { 
     do { 
      try fileManager.createDirectory(atPath: storagePath, withIntermediateDirectories: false, attributes: nil) 
     } catch _ { 
     } 
    } else { 
     print("File Path: \(storagePath)") 
    } 
+0

私はグループを追加する必要がありますか?(私のアプリケーショングループ名)は、あなたのグループを追加する必要がありますか?groupUrl = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "あなたのアプリケーショングループ名")else { return nil –

+0

ありがとうございます。それは働いている。私は正常に共有パスを作成しました –

関連する問題