将 Instagram 添加到 UIActivityViewController
Add Instagram to UIActivityViewController
我正在尝试使用标准 UIActivityViewController
分享图片,可以在 Facebook、Twitter 上分享,并且使用此代码保存图片:
let firstActivityItem = "foo text"
let secondActivityItem : UIImage = image!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo
]
self.presentViewController(activityViewController, animated: true, completion: nil)
我还需要一件事,Instagram:
If UIApplication.sharedApplication().canOpenURL(instagramURL!) {
// Success
var img = image!
var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.igo")
UIImageJPEGRepresentation(img, 1).writeToFile(savePath, atomically: true)
var imgURL = NSURL(string: NSString(format: "file://%@", savePath) as! String)
docController = UIDocumentInteractionController(URL: imgURL!) // 1
docController.UTI = "com.instagram.exclusivegram" // 2
docController.delegate = self
docController.annotation = ["InstagramCaption":"testsss"] // 3
docController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true) // 4
} else {
// Error
}
这两个代码单独使用都很好,我怎样才能将 Instagram 添加到 UIActivityViewController
?有可能吗?
似乎不可能,因为 Instagram 需要 .igo
扩展。
我认为将其他社交分享添加到您为 Instagram 编写的代码中会更容易。 “.igo”扩展名是 Instagram 独有的,因此其他应用程序将不支持它。只需将此扩展名从“.igo”更改为“.ig”,其他应用程序就会读取它:
var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.ig")
但 Instagram 也有一个专属的 UTI,以避免其他应用程序出现在同一个文档交互视图中。因此,您还需要将其从 "exclusivegram" 更改为 "photo":
docController.UTI = "com.instagram.photo"
我有一个具有类似功能的应用程序,这是我的原始代码:
@IBAction func shareOnIntagram(sender: UIButton) {
let finalImage: UIImage = UIImage.imageWithView(photoView)
let instagramURL = NSURL(string: "instagram://app")
if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
let imageData = UIImageJPEGRepresentation(finalImage, 1)
let captionString = "caption"
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
if imageData?.writeToFile(writePath, atomically: true) == false {
return
} else {
let fileURL = NSURL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(URL: fileURL)
self.documentController.delegate = self
self.documentController.UTI = "com.instagram.photo"
self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)
}
} else {
print(" Instagram is not installed ")
}
}
要使此代码正常工作,请不要忘记在 UIViewController class 中添加 UIDocumentInteractionControllerDelegate
。
我正在尝试使用标准 UIActivityViewController
分享图片,可以在 Facebook、Twitter 上分享,并且使用此代码保存图片:
let firstActivityItem = "foo text"
let secondActivityItem : UIImage = image!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo
]
self.presentViewController(activityViewController, animated: true, completion: nil)
我还需要一件事,Instagram:
If UIApplication.sharedApplication().canOpenURL(instagramURL!) {
// Success
var img = image!
var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.igo")
UIImageJPEGRepresentation(img, 1).writeToFile(savePath, atomically: true)
var imgURL = NSURL(string: NSString(format: "file://%@", savePath) as! String)
docController = UIDocumentInteractionController(URL: imgURL!) // 1
docController.UTI = "com.instagram.exclusivegram" // 2
docController.delegate = self
docController.annotation = ["InstagramCaption":"testsss"] // 3
docController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true) // 4
} else {
// Error
}
这两个代码单独使用都很好,我怎样才能将 Instagram 添加到 UIActivityViewController
?有可能吗?
似乎不可能,因为 Instagram 需要 .igo
扩展。
我认为将其他社交分享添加到您为 Instagram 编写的代码中会更容易。 “.igo”扩展名是 Instagram 独有的,因此其他应用程序将不支持它。只需将此扩展名从“.igo”更改为“.ig”,其他应用程序就会读取它:
var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.ig")
但 Instagram 也有一个专属的 UTI,以避免其他应用程序出现在同一个文档交互视图中。因此,您还需要将其从 "exclusivegram" 更改为 "photo":
docController.UTI = "com.instagram.photo"
我有一个具有类似功能的应用程序,这是我的原始代码:
@IBAction func shareOnIntagram(sender: UIButton) {
let finalImage: UIImage = UIImage.imageWithView(photoView)
let instagramURL = NSURL(string: "instagram://app")
if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
let imageData = UIImageJPEGRepresentation(finalImage, 1)
let captionString = "caption"
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
if imageData?.writeToFile(writePath, atomically: true) == false {
return
} else {
let fileURL = NSURL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(URL: fileURL)
self.documentController.delegate = self
self.documentController.UTI = "com.instagram.photo"
self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)
}
} else {
print(" Instagram is not installed ")
}
}
要使此代码正常工作,请不要忘记在 UIViewController class 中添加 UIDocumentInteractionControllerDelegate
。