I love iPhone, Android, Cocos2d-x
        [iPhone/iPad] メールの添付ファイルをアプリケーションで開けるようにする
iPhone や iPad のメールで添付ファイルをアプリケーションに取り込む方法です。
扱えるファイルを指定する
アプリケーションで扱えるファイルタイプを info.plist で指定します。例えば PDF を開けるようにしたい場合は以下のようにします。
-300x98.png)
ただ、plist エディタだと設定が面倒なので、info.plist ファイルを右クリックして「形式を指定して開く」から「ソースコードファイル」を指定して XML で直接指定した方が簡単です。
<key>CFBundleDocumentTypes</key>
<array>
  <dict>
    <key>CFBundleTypeName</key>
    <string>PDF</string>
    <key>LSHandlerRank</key>
    <string>Alternate</string>
    <key>LSItemContentTypes</key>
    <array>
      <string>com.adobe.pdf</string>
    </array>
  </dict>
</array>
ファイルを受け取る
メールの添付ファイルからアプリケーションを指定すると application:didFinishLaunchingWithOptions: の launchOptions にファイルの URL (file://…というスキーマ) で渡されます。
例えばこのファイルをアプリケーションの Documents フォルダに保存する場合は以下のようにします。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [window addSubview:viewController.view];
  [window makeKeyAndVisible];
  if (launchOptions!=nil) {
    NSURL* url = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString* pdf_file = [[url path] lastPathComponent];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* to_path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], pdf_file];
    [data writeToFile:to_path atomically:YES];
  }
}
	関連する投稿
This entry was posted by matsuura on 10月 22, 2010 at 10:40 am, and is filed under iPad, iPhone/iPod touch. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.
コメントをどうぞ コメントをキャンセル
Additional comments powered by BackType