Tuesday, October 11, 2016

Swift / Instagram API - how to auth with Instagram App

Leave a Comment

I need an AccessToken to have the User have granted access to his own Instagram Account within my App. I've registered everything within the Instagram Development Center.

Currently I'm using SimpleAuth to receive an AccessToken by Instagram.

So far everything is working well. I'm receiving the Token and am able to use it.

But when I wanted to login for the first time, I've noticed, that I had no idea what my password was. I always log in with Facebook or am already logged in. I don't want to have my users face the same issues and have them reset their Instagram password...

I have googled for a while now, without success. Is there a way to authenticate the User and receive an AccessToken using the Instagram App?

I already have added instagram to the LSApplicationQueriesSchemes at the info.plist and sending images to the Instagram App to post them there is working. So calling the app itself is not the issue.

This is my current code that opens a WebView within a UIViewController - but the User has to login with username and password.

enter image description here

func connectToInstagramFunc() {      let auth: NSMutableDictionary = ["client_id": INSTAGRAM_CLIENT_ID,                                      SimpleAuthRedirectURIKey: INSTAGRAM_REDIRECT_URI]      SimpleAuth.configuration()["instagram"] = auth      SimpleAuth.authorize("instagram") { (anyObject, error) in          if self.instagrammUsers.count > 0 {             self.deleteInstagram()         }          if anyObject != nil {              let token = String((anyObject.valueForKey("credentials")?.valueForKey("token"))!)             let uid =  String((anyObject.valueForKey("uid"))!)             let bio = String((anyObject.valueForKey("extra")?.valueForKey("raw_info")?.valueForKey("data")?.valueForKey("bio"))!)             let followed_by = String((anyObject.valueForKey("extra")?.valueForKey("raw_info")?.valueForKey("data")?.valueForKey("counts")?.valueForKey("followed_by"))!)             let follows = String((anyObject.valueForKey("extra")?.valueForKey("raw_info")?.valueForKey("data")?.valueForKey("counts")?.valueForKey("follows"))!)             let media = String((anyObject.valueForKey("extra")?.valueForKey("raw_info")?.valueForKey("data")?.valueForKey("counts")?.valueForKey("media"))!)             let username = String((anyObject.valueForKey("user_info")?.valueForKey("username"))!)             let image = String((anyObject.valueForKey("user_info")?.valueForKey("image"))!)              self.saveAccount(token, uid: uid, bio: bio, followed_by: followed_by, follows: follows, media: media, username: username, image: image)          } else {              let alert = UIAlertController(title: "Error!", message: "Please try again later!", preferredStyle: UIAlertControllerStyle.Alert)             alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))             self.presentViewController(alert, animated: true, completion: nil)         }            if error != nil {             SpeedLog.print("Error during SimpleAuth.authorize: \(error)")         }     } } 

What are my possibilities? Help is very appreciated.

1 Answers

Answers 1

No, you cannot use Instagram app to authenticate Instagram account at the moment.

However, web view seems to be a reasonable way to login especially in iOS 9 and above because it does not require user to switch between apps and having to tap on dialogs to switch app. The only pain is for the user to type username and password, but hopefully they only need to do that once (unless they change account).

Facebook has similar issue and choose to go with web view login as default way to authenticate.

For the people who are not signed into Facebook on Safari, they will only need to log into Facebook one time. After that, every future Facebook Login experience is fast and convenient with no extra steps. This means that as adoption of this flow increases over time, so does the quality of the experience people get. In contrast, the traditional fast-app-switch flow does not improve over time: The additional dialogs continue to appear in both directions for every new app that people log into.

https://developers.facebook.com/blog/post/2015/10/29/Facebook-Login-iOS9/

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment