简体   繁体   中英

Instagram NSURL implementation with swift

I have a photo editing app in swift, which needs the option to directly share to instagram.

Instagram gives ref to iphone hooks here but the implementation has proven difficult

Ideally the pages class has a button that should send the picture saved as pic.igo, (.igo assures instagram only) and brings the user directly to their post page on instagram with "pic.igo" already selected

Heres what hooks gives you, and I cant seem to find any modern use cases. The full page is in the link below

NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
    [[UIApplication sharedApplication] openURL:instagramURL];
}

So how do I put that into this function

@IBAction func Button(_ sender: Any) {


}

Iphone hooks doc

https://www.instagram.com/developer/mobile-sharing/iphone-hooks/

Notes - checking to see if the app is already installed would help, but I really just need to find a way to make this function work and I can prob fumble through the rest

  • "media?id=MEDIA_ID" from the documentation I assume that replaces the location part in their given snippet, but then how would I get the picture in view to be assigned to MEDIA_ID

  • cut and paste leaves me with like 80 errors and no way of visual recovery

If you're just looking for the Swift translation of the ObjC code they provide, this is the result:

@IBAction func Button(_ sender: Any) {
    if let instagramURL = URL(string: "instagram://location?id=1"),
        UIApplication.shared.canOpenURL(instagramURL) {
        UIApplication.shared.open(instagramURL)
    }
}

If you're having trouble converting code from ObjC, you can try some online converters, here are a couple options:

But the above is not for sharing to Instagram, if you want to share, that's what you need to do:

guard let instagramURL = URL(string: "instagram://app"),
    UIApplication.shared.canOpenURL(instagramURL) else {
        print("Instagram app is not installed")
        return
}

let yourIgoFile = Bundle.main.url(forResource: "test", withExtension: "igo")!
let documentController = UIDocumentInteractionController(url: yourIgoFile)
documentController.uti = "com.instagram.exclusivegram"
documentController.presentOpenInMenu(from: view.bounds, in: view, animated: true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM