iOS:在社交网络Swift中分享图片



我在swift中运行示例应用程序。我需要从我的应用程序共享图像。借助Social framework,我对FacebookTwitter进行了积分。现在我不知道如何与InstagramWhatsApp整合

分享按钮动作

  @IBAction func shareAcn(sender: UITapGestureRecognizer) {
    var documentController: UIDocumentInteractionController!
    let fileURL = NSBundle.mainBundle().pathForResource("smc", ofType: "png")
    documentController = UIDocumentInteractionController.init(URL: NSURL(fileURLWithPath: fileURL!))
    documentController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true)
 }

WhatsApp

 @IBAction func shareAcn(sender: UITapGestureRecognizer) {
   var documentController: UIDocumentInteractionController!
   let image = UIImage(named: "smc")
   let filename = "myimage.wai"
   let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as NSString
   let destinationPath = documentsPath.stringByAppendingString("/" + filename)
   UIImagePNGRepresentation(image!)!.writeToFile(destinationPath, atomically: false)
   let fileURL = NSURL(fileURLWithPath: destinationPath) as NSURL
   documentController = UIDocumentInteractionController.init(URL: fileURL)
   documentController.UTI = "net.whatsapp.image"
   documentController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true)
 }

如果我选择[导入到WhatsApp或复制到WhatsApp]应用程序崩溃。如果我选择WhatsApp,"此项目无法发送"。

Instagram

var instagramURL: NSURL = NSURL(string: "instagram://user?username=USERNAME")
if UIApplication.sharedApplication().canOpenURL(instagramURL) {
    UIApplication.sharedApplication().openURL(instagramURL)
}

所有代码都不能运行。

我安装了Instagram并以我的用户身份登录到我的测试设备中,如果我运行,Instagram没有打开。

我不知道如何解决这个问题?

确保您已经在info.plist文件中设置了LSApplicationQueriesSchemes条目,声明了它尝试查询的模式。

<key>LSApplicationQueriesSchemes</key>  
<array>  
     <string>whatsapp</string>
     <string>instagram</string>  
</array>  

不包含"://"

如果包含"://" canOpenUrl:将返回NO。当你实际调用canOpenUrl:时,你必须仍然包含冒号,即canOpenUrl:@"instagram:"

你可以在这个WWDC 2015视频中找到更多信息。


同样,这一行也很可疑:

var instagramURL: NSURL = NSURL(string: "instagram://user?username=USERNAME")

我怀疑你确实从instagram的文档中复制了这个。如果是,将USERNAME替换为您的实际用户名。


要隔离错误,您可能想先尝试一个更简单的URL:

var instagramURL: NSURL = NSURL(string: "instagram://app")

最新更新