2017-01-13 16 views
5

がサポートされていないため、ファイルが、私は迅速な3 iOSの10.1にInstagramの上で画像を共有するために、次のコードを持っています保存することができませんでした。iOSのシェア:指定されたURLのタイプは

func shareOnInstagram(_ photo: UIImage, text: String?) { 
    let instagramUrl = URL(string: "instagram://app")! 
    if UIApplication.shared.canOpenURL(instagramUrl) { 
     let imageData = UIImageJPEGRepresentation(photo, 1.0)! 
     let captionString = text ?? "" 

     let writePath = URL(string: NSTemporaryDirectory())!.appendingPathComponent("instagram.igo") 
     do { 
      try imageData.write(to: writePath) 
      let documentsInteractionsController = UIDocumentInteractionController(url: writePath) 
      documentsInteractionsController.delegate = self 
      documentsInteractionsController.uti = "com.instagram.exlusivegram" 
      documentsInteractionsController.annotation = ["InstagramCaption": captionString] 
      documentsInteractionsController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true) 
     }catch { 
      return 
     } 
    }else { 
     let alertController = UIAlertController(title: "Install Instagram", message: "You need Instagram app installed to use this feature. Do you want to install it now?", preferredStyle: .alert) 
     let installAction = UIAlertAction(title: "Install", style: .default, handler: { (action) in 
      //redirect to instagram 
     }) 
     alertController.addAction(installAction) 

     let laterAction = UIAlertAction(title: "Later", style: .cancel, handler: nil) 
     alertController.addAction(laterAction) 

     present(alertController, animated: true, completion: nil) 
    } 
} 

でアンラインtry imageData.write(to: writePath)は、それは次のようなエラーがスローされます。

Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported." UserInfo={NSURL=/private/var/mobile/Containers/Data/Application/5B80A983-5571-44A5-80D7-6A7B065800B5/tmp/instagram.igo} 

することは、誰かがこれで私を助けることができますか?

答えて

17

URLを間違って作成しています。変更:

let writePath = URL(string: NSTemporaryDirectory())!.appendingPathComponent("instagram.igo") 

には:あなたはファイルパスからURLを作成するたびに

let writePath = URL(fileURLWithPath: NSTemporaryDirectory())!.appendingPathComponent("instagram.igo") 

あなたはfileURLWithPath初期化子を使用する必要があります。 stringイニシャライザを使用するのは、http,mailto,telなどのスキームで始まるURLを作成する場合にのみ有効です。

関連する問題