Please find the steps for integrating the Instagram in your app.
Create an account for Instagram :
Try logging through an Instagram account https://www.instagram.com/developer/
And then select the Manage Clients option so that you can edit the information:
CLIENT STATUS: Sandbox mode and REVIEW STATUS : Pending
CLIENT STATUS: Live mode and REVIEW STATUS : Reviewed
Click on the MANAGE button to edit the information.
Valid redirect URIs: The URL helps in redirecting it to Instagram from app but mention it in your swift code as well same as written over here.
Invite user in Sandbox mode :
For App Live :
Upload the video through drive public link and then share the video with Instagram developer’s for approval.
And after approval from Instagram Developer, the status will change from Sandbox to Live.
The app may not be live so early because it takes 2-3 weeks to get an approval from Instagram.
Swift Code for integration of Instagram as given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import UIKit import WebKit class InstraViewController: UIViewController { @IBOutlet weak var webView :UIWebView! override func viewDidLoad() { super.viewDidLoad() let authURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True", arguments: [API.INSTAGRAM_AUTHURL,API.INSTAGRAM_CLIENT_ID,API.INSTAGRAM_REDIRECT_URI, API.INSTAGRAM_SCOPE]) let urlRequest = URLRequest.init(url: URL.init(string: authURL)!) webView.delegate = self webView.loadRequest(urlRequest) } @IBAction func close(){ self.dismiss(animated: true, completion: nil) } func checkRequestForCallbackURL(request: URLRequest) -> Bool { let requestURLString = (request.url?.absoluteString)! as String if requestURLString.hasPrefix(API.INSTAGRAM_REDIRECT_URI) { let range: Range<String.Index> = requestURLString.range(of: "#access_token=")! handleAuth(authToken: requestURLString.substring(from: range.upperBound)) return false; } return true } func handleAuth(authToken: String) { API.INSTAGRAM_ACCESS_TOKEN = authToken print("Instagram authentication token ==", authToken) getUserInfo(){(data) in DispatchQueue.main.async { self.dismiss(animated: true, completion: nil) } } } func getUserInfo(completion: @escaping ((_ data: Bool) -> Void)){ let url = String(format: "%@%@", arguments: [API.INSTAGRAM_USER_INFO,API.INSTAGRAM_ACCESS_TOKEN]) var request = URLRequest(url: URL(string: url)!) request.addValue("application/json", forHTTPHeaderField: "Content-Type") let session = URLSession.shared let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in guard error == nil else { completion(false) //failure return } // make sure we got data guard let responseData = data else { completion(false) //Error: did not receive data return } do { guard let dataResponse = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: AnyObject] else { completion(false) //Error: did not receive data return } completion(true) // success (dataResponse) dataResponse: contains the Instagram data } catch let err { completion(false) //failure } }) task.resume() } } extension InstraViewController: UIWebViewDelegate{ func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool { return checkRequestForCallbackURL(request: request) } } extension InstraViewController: WKNavigationDelegate{ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { if checkRequestForCallbackURL(request: navigationAction.request){ decisionHandler(.allow) }else{ decisionHandler(.cancel) } } } struct API{ static let INSTAGRAM_AUTHURL = "https://api.instagram.com/oauth/authorize/" static let INSTAGRAM_USER_INFO = "https://api.instagram.com/v1/users/self/?access_token=" static let INSTAGRAM_CLIENT_ID = "XXXXX" static let INSTAGRAM_CLIENTSERCRET = "XXXXX" static let INSTAGRAM_REDIRECT_URI = "https://XXXXX" static var INSTAGRAM_ACCESS_TOKEN = "" static let INSTAGRAM_SCOPE = "follower_list+public_content" /* add whatever scope you need https://www.instagram.com/developer/authorization/ */ } |
Hope it will help in Integration of Instagram social login in app.