2017-01-13 12 views
5

Mam następujący kod do dzielenia obrazu na Instagram w Swift 3 iOS 10.1:akcji iOS na Instagram: Plik nie może zostać zapisany, ponieważ określony typ URL nie jest obsługiwana

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) 
    } 
} 

An u linia try imageData.write(to: writePath), to rzuca błąd w następujący sposób:

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} 

Czy ktoś może mi pomóc z tym?

Odpowiedz

17

Tworzysz niepoprawnie swoje URL. Zmiana:

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

do:

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

Ilekroć utworzyć URL ze ścieżki pliku należy użyć fileURLWithPath inicjatora. Użycie inicjalizatora string jest poprawne tylko podczas tworzenia URL, które rozpoczyna się od schematu, takiego jak adresy URL z adresem URL: http, mailto,.

Powiązane problemy