【问题标题】:iOS: Sharing image in social network SwiftiOS:在社交网络 Swift 中分享图片
【发布时间】:2016-11-06 18:55:49
【问题描述】:

我正在快速运行示例应用程序。我需要从我的应用程序共享图像。 在Social framework 的帮助下,我整合了Facebook 和Twitter。现在我不知道如何与Instagram 和WhatsApp 集成

分享按钮操作

  @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 没有打开。

不知道怎么解决?请指导我。

【问题讨论】:

  • LSApplicationsQueriesSchemes ??它在 Info.plist 中的什么位置?我查不出来。
  • 如果你在 if 语句中放了一个断点,它会命中它吗?
  • 其他部分一直在工作??
  • @shallowThought 如果你知道答案,请告诉我?我到处都是serchin..但不理解概念..请

标签: swift instagram whatsapp uidocumentinteraction


【解决方案1】:

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

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

不要包含“://”。

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

您可以在this WWDC 2015 video找到更多信息。


另外,这条线很可疑:

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

我怀疑你确实从Instagrams documentation 复制了这个。如果是这样,请将USERNAME替换为您的实际用户名。


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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2013-05-25
    • 2018-01-16
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多