我在swift中运行示例应用程序。我需要从我的应用程序共享图像。借助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)
}
@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
,"此项目无法发送"。
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")