Monday, February 29, 2016

OAuthSwift Tutorial

I couldn't find a tutorial on this so this is my documented journey into trying to figure out how to implement OAuthSwift into an iOS project. I honestly don't know if I will fail or succeed. So far just from reading the GitHub page for OAuthSwift I can't even figure it out so it is time to just start trying to break it apart piece by piece and implementing it.

OK, let's look at the GitHub page for OAuthSwift and go with the "simplest way":

(From OAuthSwift's GitHub page)
OAuthSwift is packaged as a Swift framework. Currently this is the simplest way to add it to your app:
  • Drag OAuthSwift.xcodeproj to your project in the Project Navigator.
  • Select your project and then your app target. Open the Build Phases panel.
  • Expand the Target Dependencies group, and add OAuthSwift framework.
  • import OAuthSwift whenever you want to use OAuthSwift.

-------------------------------
Let's start with the first step:

Drag OAuthSwift.xcodeproj to your project in the Project Navigator.

Ok, first I clicked the "Download ZIP" button from the GitHub webpage. I went into my Downloads folder and unzipped the file "OAuthSwift-master.zip". I look in the newly unzipped folder and see "OAuthSwift.xcodeproj" in there.

So far so good. Now I'm going to create a new Single View Application project that will use this XCode Project file and call it "InstagramLogin" since this is what I'm interested in trying to do. I bring up my Downloads folder so I can drag the "OAuthSwift.xcodeproj" file into my XCode Project Navigator. I'm not sure where to drag the file to so I just try dropping it into the very top of my new project, "InstagramLogin".

XCode shows me a message:

I remember reading that if you combine more than one "xcodeproj" together than it has to be done in a "workspace". I click Save and name my new workspace the same as my initial project, "InstagramLogin".

After doing this I see the two projects in my Project Navigator in XCode. Looks like all the files were copied from the Downloads folder automatically. I try building and build succeeds.

OK, on to the next step!

Select your project and then your app target. Open the Build Phases panel.

Clicking around I found out what he means. When you click on your project, "InstagramLogin" in my case, it shows the project properties. On the left hand side of these project settings I see "PROJECT" and "TARGETS". So I clicked on my project name under "TARGETS". I'm honestly still kind of a newbie so I really don't know what this means. At the top of this window I found "Build Phases" and clicked on that:

Expand the Target Dependencies group, and add OAuthSwift framework.

I expanded "Target Dependencies" group. There is a "+" and "-" showing. So I click on the "+" button. This window shows:

Should "OAuthSwift" project be showing in there? From here I can't seem to figure out how to complete this step. Googling how to add a project dependency in XCode brought up a StackOverflow answer. Looks like the instructions are different.

So instead of going through "Target Dependencies" I'm going to go to the General tab and scroll to the bottom to "Linked Frameworks and Libraries" and click the plus ("+") button there. This is what I see:

I think I will select the first one. Although it does not say "iOS" on it the others do not say "iOS" so it is kind of a process of elimination.

Now for the final test...

import OAuthSwift whenever you want to use OAuthSwift.

Just to make sure what I have done so far works, I am just going to open up ViewController.swift in my "InstagramLogin" project and add import OAuthSwift and see if the project builds. Yes! It does build! Now on to using this framework!

Implementing OAuthSwift

OK, now that I can link to OAuthSwift it looks like I can move to the next step and start developing the actual Instagram login. So let's keep following the instructions on the GitHub page for OAuthSwift and see how to do this.

The first thing the page says to do is to set the URL scheme and shows a screenshot:

OK, I have no idea how to do this or even where that screenshot is from. I'm going to have to do some Googling once more and search for maybe "URL Types XCode".

I found a link to twitter development and they had a full screenshot of where the image above comes from. I had to select my project "InstagramLogin" in the Project Navigator, selected my project again under TARGETS, then select Info up top. I scrolled to the bottom and there it was. I clicked the "+" icon to add a new item.

I added my application name but I seriously don't even know what this does. I'm just trusting that it is needed.

OK, continuing on, let's look at the Examples section on the OAuthSwift GitHub page.

I am just going to copy and paste the code example under "Handle URL in AppDelegate" into my AppDelegate:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
  if (url.host == "oauth-callback") {
    OAuthSwift.handleOpenURL(url)
  }
  return true
}

I try to build but it fails. Looks like I need to add:

import OAuthSwift

Now it builds. Do I need to use a different string where it says "oauth-callback"? I do not know. Maybe I will find out later.

Now on the GitHub page they have some code examples. I'm going to go with the OAuth2.0 example:

let oauthswift = OAuth2Swift(
    consumerKey:    "********",
    consumerSecret: "********",
    authorizeUrl:   "https://api.instagram.com/oauth/authorize",
    responseType:   "token"
)
oauthswift.authorizeWithCallbackURL(
    NSURL(string: "oauth-swift://oauth-callback/instagram")!,
    scope: "likes+comments", state:"INSTAGRAM",
    success: { credential, response, parameters in
      print(credential.oauth_token)
    },
    failure: { error in
      print(error.localizedDescription)
    }
)

Looks like an Instagram example, which is good for me. Hmm...let's see. What do I do with this? It looks like I first have to create a variable (oathswift) that will hold my Instagram account info. I have already setup my Instagram account so I have all of this information.

I am a little confused actually as far as what is going on so I will have to do a little more research on what typically happens with Open Authentication.

To be continued...
















9 comments:

  1. I am in the same last step of yours. I understood that the next step is to understand the authentication workflow. besides, one of the authors of OAuth discourages you from using OAuth 2. So, reading this is your next step: https://hueniverse.com/oauth/guide/

    ReplyDelete
    Replies
    1. Wow, thanks. This is very interesting. I ended up giving up on this because it was eating up all my time. Instead of took the easy route out and hired someone from fiverr.com to just build me that one piece that I needed. Ha ha.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Do you think maybe you post the code that your fiverr person built for you that works?

      Delete
  2. Just wondering if you've gotten any further with this?

    ReplyDelete
  3. Hi Mark thanks for the post. Can you refer the guy made the api for you to me? I probably need the same piece. Thanks a lot!

    ReplyDelete
  4. Stinks you couldn't get it to work but it was reassuring to find someone posting honestly that they are not an expert in every single iOS thing. I thought I was the only member of that club. Thanks for making me feel better Mark!

    ReplyDelete

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...