[SWIFT] Share it with your Instagram story!

Share it with your Instagram story!

What you can learn this time

--Get date and time --Convert View to UIImage --Share the image to the Instagram story. --Check if there is a specific app on your iPhone.

The image of completion is like this!

App side Instagram story

Image and Label are displayed in View, and the View is converted to image and passed to the Instagram story.

Get date and time

This time, the date is displayed so that it is easy to confirm that the value has changed when it is displayed. Adding variables such as user name to Label will make the design easier to Instagram.

ViewController.swift


@IBOutlet weak var dateLable: UILabel!   //Connect with Storyboard Label
//------------------------------------------------------
//super.viewDidLoad()Described in
let format = DateFormatter()
format.timeStyle = .medium
format.dateStyle = .long
format.locale = Locale(identifier: "ja_JP")
let date = Date()
 dateLable.text = format.string(from: date)

Convert View to UIImage

Let's connect the View created in Storyboard to the ViewController using @IBOutle! After connecting, extend UIView and add the following code to call the image = part at the timing you want to convert.

ViewController.swift


@IBOutlet weak var screensShotView: UIView!   //Connect with Storyboard View
//------------------------------------------------------
//Let's add the following code as an extension of UIVIew.
//view.asImage()You can always convert a View to an Image with.
extension UIView {
    //Code to convert UIView to UIImage
    func asImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
}

//view.asImage()You can convert it to UIImage with. It is better to call it when the button is pressed.
let image = screensShotView.asImage()

Share images to Instagram stories

For a detailed list of Instagram schemes, please see Official Documents.

ViewController.swift


private func setupOpenInstagram() {
        let image = screensShotView.asImage()
        let items: [[String: Any]] = [[
            "com.instagram.sharedSticker.stickerImage": image,
            "com.instagram.sharedSticker.backgroundTopColor": "#000000",
            "com.instagram.sharedSticker.backgroundBottomColor": "#FFFFFF"
        ]]
        UIPasteboard.general.setItems(items, options: [:])
        guard let shareInstagramStoryURL = "instagram-stories://share".convertURL else { return }
        UIApplication.shared.open(shareInstagramStoryURL, options: [:], completionHandler: nil)
    }

The material is an image of an illustration shop. https://www.irasutoya.com/2019/04/blog-post_34.html

Check if you have a scheme key for a particular app. https://qiita.com/nagaoyuriko/items/67c5e262f6e88cd88885

Recommended Posts

Share it with your Instagram story!
Automate your work with Rake
A story stuck with NotSerializableException
INRestaurantReservation: Share your booking information with Siri to automatically suggest events