Is there a way to post a video or photo directly to the Instagram Stories of the user's Instagram account? For a normal photo share on Instagram, you can use the URL Scheme and open the Editor directly. Is there a way for Stories, too?
Thanks!
Is there a way to post a video or photo directly to the Instagram Stories of the user's Instagram account? For a normal photo share on Instagram, you can use the URL Scheme and open the Editor directly. Is there a way for Stories, too?
Thanks!
Swift 4.2 version of knshn's answer:
func shareBackgroundImage() {
let image = UIImage(imageLiteralResourceName: "backgroundImage")
if let pngImage = image.pngData() {
backgroundImage(pngImage, attributionURL: "http://your-deep-link-url")
}
}
func backgroundImage(_ backgroundImage: Data, attributionURL: String) {
// Verify app can open custom URL scheme, open if able
guard let urlScheme = URL(string: "instagram-stories://share"),
UIApplication.shared.canOpenURL(urlScheme) else {
// Handle older app versions or app not installed case
return
}
let pasteboardItems = [["com.instagram.sharedSticker.backgroundImage": backgroundImage,
"com.instagram.sharedSticker.contentURL": attributionURL]]
let pasteboardOptions: [UIPasteboard.OptionsKey: Any] = [.expirationDate: Date().addingTimeInterval(60 * 5)]
// This call is iOS 10+, can use 'setItems' depending on what versions you support
UIPasteboard.general.setItems(pasteboardItems, options: pasteboardOptions)
UIApplication.shared.open(urlScheme)
}
Instagram officially supports this since March 2018. https://developers.facebook.com/docs/instagram/sharing-to-stories/
For iOS:
You need to add instagram-stories
to the LSApplicationQueriesSchemes
key in your app's Info.plist
.
This sample code shows how to pass the Instagram app a background layer image asset and an attribution deep link.
- (void)shareBackgroundImage {
[self backgroundImage:UIImagePNGRepresentation([UIImage imageNamed:@"backgroundImage"])
attributionURL:@"http://your-deep-link-url"];
}
- (void)backgroundImage:(NSData *)backgroundImage
attributionURL:(NSString *)attributionURL {
// Verify app can open custom URL scheme, open if able
NSURL *urlScheme = [NSURL URLWithString:@"instagram-stories://share"];
if ([[UIApplication sharedApplication] canOpenURL:urlScheme]) {
// Assign background image asset and attribution link URL to pasteboard
NSArray *pasteboardItems = @[@{@"com.instagram.sharedSticker.backgroundImage" : backgroundImage,
@"com.instagram.sharedSticker.contentURL" : attributionURL}];
NSDictionary *pasteboardOptions = @{UIPasteboardOptionExpirationDate : [[NSDate date] dateByAddingTimeInterval:60 * 5]};
// This call is iOS 10+, can use 'setItems' depending on what versions you support
[[UIPasteboard generalPasteboard] setItems:pasteboardItems options:pasteboardOptions];
[[UIApplication sharedApplication] openURL:urlScheme options:@{} completionHandler:nil];
} else {
// Handle older app versions or app not installed case
}
}
You can use this answer to convert a PHAsset's identifier into an asset URL, then format it into an instagram://
URL using this logic. That used to only be capable of making conventional Instagram posts, but as of the past few weeks it actually prompts users if they want to make a story or a post with the asset you provided on launch!
If you download .ipa of instagram and look at their Info.plist, we can found :
<key>CFBundleURLSchemes</key>
<array>
<string>instagram-stories</string>
<string>fb124024574287414</string>
<string>instagram</string>
<string>instagram-capture</string>
<string>fsq+kylm3gjcbtswk4rambrt4uyzq1dqcoc0n2hyjgcvbcbe54rj+post</string>
</array>
but of course it's private (not official) and not documented by Instagram. If anyone now how to use it/find parameters, I'm really curious to know !
For SwiftUI
import SwiftUI
fileprivate let wands: [Wand] = [
Wand(name: "Dumbledore's Wand", stickerAsset: "dumbledore"),
Wand(name: "Potter's Wand", stickerAsset: "potter"),
Wand(name: "Granger's Wand", stickerAsset: "granger")
]
struct ContentView: View {
func shareToInstagramStories(
stickerImage: String,
stickerLink: String,
backgroundTopColor: String = "#7F0909",
backgroundBottomColor: String = "#303030"
) {
// 1. Get a data object of our UIImage...
let stickerImageData = UIImage(named: stickerImage)?.pngData()
// 2. Verify if we are able to open instagram-stories URL schema.
// If we are able to, let's add our Sticker image to UIPasteboard.
let urlScheme = URL(string: "instagram-stories://share?source_application=\(Bundle.main.bundleIdentifier ?? "")")
if let urlScheme = urlScheme {
if UIApplication.shared.canOpenURL(urlScheme) {
var pasteboardItems: [[String : Any]]? = nil
if let stickerImageData = stickerImageData {
pasteboardItems = [
[
"com.instagram.sharedSticker.stickerImage": stickerImageData,
"com.instagram.sharedSticker.backgroundTopColor": backgroundTopColor,
"com.instagram.sharedSticker.backgroundBottomColor": backgroundBottomColor,
"com.instagram.sharedSticker.link": stickerLink
]
]
}
// We'll expire these pasteboard items in 5 minutes...
let pasteboardOptions = [
UIPasteboard.OptionsKey.expirationDate: Date().addingTimeInterval(60 * 5)
]
if let pasteboardItems = pasteboardItems {
UIPasteboard.general.setItems(pasteboardItems, options: pasteboardOptions)
}
// 3. Try opening the URL...
UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
} else {
// App may not be installed. Handle those errors here...
print("Something went wrong. Maybe Instagram is not installed on this device?")
}
}
}
var body: some View {
NavigationView {
List(wands, id: \.name){ wand in
Text(wand.name).onTapGesture {
shareToInstagramStories(stickerImage: wand.stickerAsset, stickerLink: "www.google.com")
}
}
.navigationBarTitle(Text("Ollivanders"), displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Wand {
let name: String
let stickerAsset: String
}
Special thanks to [Ishan Chhabra][https://ishanchhabra.com/thoughts/sharing-to-instagram-stories]