Giter Site home page Giter Site logo

foursquareapiclient's Introduction

FoursquareAPIClient

Build Status Carthage compatible

FoursquareAPIClient is very simple Swift networking library for Foursquare API v2.

Demo

Installation

CocoaPods

First, add the following line to your Podfile:

pod 'FoursquareAPIClient'

Second, install FoursquareAPIClient into your project:

pod install

Carthage

  • Add it to your Cartfile:
github "koogawa/FoursquareAPIClient"
  • Run carthage update --platform iOS
  • Add 'FoursquareAPIClient.framework' to 'Linked Frameworks and Library' on your project.
  • Add /usr/local/bin/carthage copy-frameworks to 'New Run Script Phase'.
  • Add $(SRCROOT)/Carthage/Build/iOS/FoursquareAPIClient.framework to 'Input Files'.

Manually

Copy all the files from the FoursquareAPIClient folder to your project.

  • FoursquareAPIClient.swift
  • FoursquareAuthClient.swift (Optional)
  • FoursquareAuthViewController.swift (Optional)

Usage

Import

import FoursquareAPIClient

Setup session

let client = FoursquareAPIClient(accessToken: "YOUR_ACCESS_TOKEN")

or

let client = FoursquareAPIClient(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET")

Versioning

// Set v=YYYYMMDD param
let client = FoursquareAPIClient(accessToken: "YOUR_ACCESS_TOKEN", version: "20140723")

or

let client = FoursquareAPIClient(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET”,
                                  version: "20140723)

Search Venues

let parameter: [String: String] = [
    "ll": "35.702069,139.7753269",
    "limit": "10",
];

client.request(path: "venues/search", parameter: parameter) { result in
    switch result {
    case let .success(data):
        // parse the JSON data with NSJSONSerialization or Lib like SwiftyJson
        // e.g. {"meta":{"code":200},"notifications":[{"...
        let json = try! JSONSerialization.jsonObject(with: data, options: [])

    case let .failure(error):
        // Error handling
        switch error {
        case let .connectionError(connectionError):
            print(connectionError)
        case let .responseParseError(responseParseError):
            print(responseParseError)   // e.g. JSON text did not start with array or object and option to allow fragments not set.
        case let .apiError(apiError):
            print(apiError.errorType)   // e.g. endpoint_error
            print(apiError.errorDetail) // e.g. The requested path does not exist.
        }
    }
}

Check in to Venue

let parameter: [String: String] = [
    "venueId": "55b731a9498eecdfb"3854a9,
    "ll": "37.33262674912818,-122.030451055438",
    "alt": "10”,
];

client.request(path: "checkins/add", method: .post, parameter: parameter) { result in
    switch result {
    case let .success(data):
        // parse the JSON data with NSJSONSerialization or Lib like SwiftyJson
        // e.g. {"meta":{"code":200},"notifications":[{"...
        let json = try! JSONSerialization.jsonObject(with: data, options: [])
    case let .failure(error):
        // Error handling
        switch error {
        case let .connectionError(connectionError):
            print(connectionError)
        case let .responseParseError(responseParseError):
            print(responseParseError)   // e.g. JSON text did not start with array or object and option to allow fragments not set.
        case let .apiError(apiError):
            print(apiError.errorType)   // e.g. endpoint_error
            print(apiError.errorDetail) // e.g. The requested path does not exist.
        }
    }
}

Add a Photo

let parameter: [String: String] = [
    "checkinId": "IHR8THISVNU",
    "broadcast": "twitter,facebook",
    "postText": "Awesome!",
];

let yourImage = UIImage(named: "photo")
let imageData = UIImageJPEGRepresentation(yourImage!, 1)

client.upload(path: "photos/add", parameter: parameter, imageData: imageData!) {
    result in
    switch result {
    case let .success(data):
        // Upload success
    case let .failure(error):
        // Upload error
    }
}

Authorization

Setup

let client = FoursquareAuthClient(clientId: "YOUR_CLIENT_ID",
                                  callback: "YOUR_CALLBACK_URL",
                                  delegate: self)

Delegate

func foursquareAuthClientDidSucceed(accessToken: String) {
    print(accessToken)
}

func foursquareAuthClientDidFail(error: NSError) {
    print(error.description)
}

Requirements

Swift 5.0 / iOS 12.0+

Creator

Kosuke Ogawa

License

The MIT License. See License.txt for details.

foursquareapiclient's People

Contributors

koogawa avatar mattrubin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

foursquareapiclient's Issues

categoryID parameter, multiple id problem

Hi,

First of all thanks a lot for this repo.

I was checking and trying to implement a project with it but there seems to be a problem when you try to provide multiple ids separating with coma like: "categoryID" : "4xxxxxxx, 4xxxxxx"

It returns back with invalid categoryID response, even though all the categories works perfectly fine alone.

Thanks,
Emre

Multi categoryId

Hi,
I am trying to retrieve multiple categoriesId.
I started with Das-Quadrat for the Foursquare API.
When I use only one categoryID, it obviously works well:

      let searchTask = session.venues.search(parameters) {
      ...

      }
     searchTask.start()

with:

        var parameters = location.parameters()
        parameters += [Parameter.categoryId: "4bf58dd8d48988d103951735"]
        parameters += [Parameter.radius: "2000"]
        parameters += [Parameter.limit: "50"]

When I add another:

        var parameters1 = location.parameters()
        parameters1 += [Parameter.categoryId: "52f2ab2ebcbc57f1066b8b23"]
        parameters1 += [Parameter.radius: "2000"]
        parameters1 += [Parameter.limit: "50"]

and try to use the multi Tasks, the things go wrong, I have no clear idea how to implement the completionHandler to retrieve the venues:

         let multiTask = session.multi.get(tasks: [parameters, parameters1], completionHandler:          ResponseClosure) {
          ...
          }
           multiTask.start()

My function where I interact with foursquare api:

        func getCoffeeShopsWithLocation(location:CLLocation)
{
    if let session = self.session
    {
        var parameters = location.parameters()
        parameters += [Parameter.categoryId: "4bf58dd8d48988d103951735"]//, 4bf58dd8d48988d10c951735, 4bf58dd8d48988d111951735, 52f2ab2ebcbc57f1066b8b23"]//, 52f2ab2ebcbc57f1066b8b32
        parameters += [Parameter.radius: "1200"]
        parameters += [Parameter.limit: "50"]

        // Start a "search", i.e. an async call to Foursquare that should return venue data
        let searchTask = session.venues.search(parameters)
            {
                (result) -> Void in

                if let response = result.response
                {
                    if let venues = response["venues"] as? [[String: AnyObject]]
                    {
                        autoreleasepool
                            {
                                let realm = try! Realm()
                                realm.beginWrite()

                                for venue:[String: AnyObject] in venues
                                {
                                    let venueObject:Venue = Venue()

                                    if let id = venue["id"] as? String
                                    {
                                        venueObject.id = id
                                    }

                                    if let name = venue["name"] as? String
                                    {
                                        venueObject.name = name
                                    }


                                    if  let location = venue["location"] as? [String: AnyObject]
                                    {
                                        if let longitude = location["lng"] as? Float
                                        {
                                            venueObject.longitude = longitude
                                        }

                                        if let latitude = location["lat"] as? Float
                                        {
                                            venueObject.latitude = latitude
                                        }

                                        if let formattedAddress = location["formattedAddress"] as? [String]
                                        {
                                            venueObject.address = formattedAddress.joinWithSeparator(" ")
                                        }
                                    }

                                    realm.add(venueObject, update: true)
                                }

                                do {
                                    try realm.commitWrite()
                                    print("Committing write...")
                                }
                                catch (let e)
                                {
                                    print("Y U NO REALM ? \(e)")
                                }
                        }

                        NSNotificationCenter.defaultCenter().postNotificationName(API.notifications.venuesUpdated, object: nil, userInfo: nil)
                    }
                }
        }

        searchTask.start()
    }
}

When I print result.response, I get all the data requested.

I tried this:

    func getCoffeeShopsWithLocation(location:CLLocation)
{
    if let session = self.session
    {
        var parameters = location.parameters()
        parameters += [Parameter.categoryId: "4bf58dd8d48988d103951735"]//, 4bf58dd8d48988d10c951735, 4bf58dd8d48988d111951735, 52f2ab2ebcbc57f1066b8b23"]//, 52f2ab2ebcbc57f1066b8b32
        parameters += [Parameter.radius: "1200"]
        parameters += [Parameter.limit: "50"]

        var parameters1 = location.parameters()
        parameters1 += [Parameter.categoryId: "4bf58dd8d48988d10c951735"]//, 4bf58dd8d48988d10c951735, 4bf58dd8d48988d111951735, 52f2ab2ebcbc57f1066b8b23"]//, 52f2ab2ebcbc57f1066b8b32
        parameters1 += [Parameter.radius: "1200"]
        parameters1 += [Parameter.limit: "50"]

        let task1 = session.venues.search(parameters)

        let task2 = session.venues.search(parameters1)

       // let task1 = self.quadratSession.users.get()
       // let task2 = self.quadratSession.users.friends(userId: "self", parameters: nil)

        /*let multiTask = self.quadratSession.multi.get([task1, task2]){
            (responses) -> Void in
            println(responses)
        }
        multiTask.start()*/

        let searchTask = session.multi.get([task1, task2]){

            (result) -> Void in

            print ("ok:", result.results)
            print ("ok:", result.response)
            if let response = result.response
            //response
            {
                if let venues = response["venues"] as? [[String: AnyObject]]
                {
                    autoreleasepool
                        {
                            let realm = try! Realm()
                            realm.beginWrite()

                            for venue:[String: AnyObject] in venues
                            {
                                let venueObject:Venue = Venue()

                                if let id = venue["id"] as? String
                                {
                                    venueObject.id = id
                                }

                                if let name = venue["name"] as? String
                                {
                                    venueObject.name = name
                                }


                                if  let location = venue["location"] as? [String: AnyObject]
                                {
                                    if let longitude = location["lng"] as? Float
                                    {
                                        venueObject.longitude = longitude
                                    }

                                    if let latitude = location["lat"] as? Float
                                    {
                                        venueObject.latitude = latitude
                                    }

                                    if let formattedAddress = location["formattedAddress"] as? [String]
                                    {
                                        venueObject.address = formattedAddress.joinWithSeparator(" ")
                                    }
                                }

                                realm.add(venueObject, update: true)
                            }

                            do {
                                try realm.commitWrite()
                                print("Committing write...")
                            }
                            catch (let e)
                            {
                                print("Y U NO REALM ? \(e)")
                            }
                    }

                    NSNotificationCenter.defaultCenter().postNotificationName(API.notifications.venuesUpdated, object: nil, userInfo: nil)
                }
            }
        }

        searchTask.start()

    }
  }

But when I print result.response, I get a nil.
And when I print result.results, I get all the data, but I don'y manage to work with the result.results.

I found out your project few weeks ago, but I was thinking it was easier to use Das-Quadrat, they have potentially implemented a multitask function.
Now I am entirely blocked..

Do you know How I could fix it easily and quickly?
Thank you a lot!!!!!!!!!!!!!!!!! 💯

Completion handler passed to -[FoursquareAPIClient.FoursquareAuthViewController webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once

  1. Show FoursquareAuthViewController
  2. Tap login button
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[FoursquareAPIClient.FoursquareAuthViewController webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000108380f6b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x0000000107ceb121 objc_exception_throw + 48
	2   CoreFoundation                      0x00000001083ff735 +[NSException raise:format:] + 197
	3   WebKit                              0x000000010a83ee25 _ZNK6WebKit28CompletionHandlerCallChecker30completionHandlerHasBeenCalledEv + 203
	4   WebKit                              0x000000010a8817f1 _ZZN6WebKit15NavigationState16NavigationClient31decidePolicyForNavigationActionERNS_12WebPageProxyERN3API16NavigationActionEON3WTF3RefINS_27WebFramePolicyListenerProxyEEEPNS4_6ObjectEEN3$_1clE24WKNavigationActionPolicyP18_WKWebsitePolicies + 45
	5   FoursquareAPIClient                 0x0000000106cece80 _T0So24WKNavigationActionPolicyOIyBy_ABIxy_TR + 32
	6   FoursquareAPIClient                 0x0000000106cedcca _T0So24WKNavigationActionPolicyOIyBy_ABIxy_TRTA + 58
	7   FoursquareAPIClient                 0x0000000106cecd6d _T019FoursquareAPIClient0A18AuthViewControllerC03webD0ySo05WKWebD0C_So18WKNavigationActionC15decidePolicyForySo0hiK0Oc15decisionHandlertF + 2285
	8   FoursquareAPIClient                 0x0000000106cece4c _T019FoursquareAPIClient0A18AuthViewControllerC03webD0ySo05WKWebD0C_So18WKNavigationActionC15decidePolicyForySo0hiK0Oc15decisionHandlertFTo + 156
	9   WebKit                              0x000000010a87f452 _ZN6WebKit15NavigationState16NavigationClient31decidePolicyForNavigationActionERNS_12WebPageProxyERN3API16NavigationActionEON3WTF3RefINS_27WebFramePolicyListenerProxyEEEPNS4_6ObjectE + 1150
	10  WebKit                              0x000000010a9ddd02 _ZN6WebKit12WebPageProxy31decidePolicyForNavigationActionEyRKN7WebCore18SecurityOriginDataEyRKNS_20NavigationActionDataEyS4_RKNS1_15ResourceRequestESA_yRKNS_8UserDataEON3WTF3RefIN8Messages12WebPageProxy31DecidePolicyForNavigationAction12DelayedReplyEEE + 1230
	11  WebKit                              0x000000010aa04cd3 _ZN3IPC22callMemberFunctionImplIN6WebKit12WebPageProxyEMS2_FvyRKN7WebCore18SecurityOriginDataEyRKNS1_20NavigationActionDataEyS6_RKNS3_15ResourceRequestESC_yRKNS1_8UserDataEON3WTF3RefIN8Messages12WebPageProxy31DecidePolicyForNavigationAction12DelayedReplyEEEESL_NSt3__15tupleIJyS4_yS7_yS4_SA_SA_ySD_EEEJLm0ELm1ELm2ELm3ELm4ELm5ELm6ELm7ELm8ELm9EEEEvPT_T0_ONSH_IT1_EEOT2_NSQ_16integer_sequenceImJXspT3_EEEE + 89
	12  WebKit                              0x000000010a9ff75e _ZN3IPC20handleMessageDelayedIN8Messages12WebPageProxy31DecidePolicyForNavigationActionEN6WebKit12WebPageProxyEMS5_FvyRKN7WebCore18SecurityOriginDataEyRKNS4_20NavigationActionDataEyS9_RKNS6_15ResourceRequestESF_yRKNS4_8UserDataEON3WTF3RefINS3_12DelayedReplyEEEEEEvRNS_10ConnectionERNS_7DecoderERNSt3__110unique_ptrINS_7EncoderENSU_14default_deleteISW_EEEEPT0_T1_ + 248
	13  WebKit                              0x000000010a87cdd1 _ZN3IPC18MessageReceiverMap19dispatchSyncMessageERNS_10ConnectionERNS_7DecoderERNSt3__110unique_ptrINS_7EncoderENS5_14default_deleteIS7_EEEE + 141
	14  WebKit                              0x000000010aa4d366 _ZN6WebKit15WebProcessProxy21didReceiveSyncMessageERN3IPC10ConnectionERNS1_7DecoderERNSt3__110unique_ptrINS1_7EncoderENS6_14default_deleteIS8_EEEE + 28
	15  WebKit                              0x000000010a841747 _ZN3IPC10Connection19dispatchSyncMessageERNS_7DecoderE + 203
	16  WebKit                              0x000000010a83f266 _ZN3IPC10Connection15dispatchMessageENSt3__110unique_ptrINS_7DecoderENS1_14default_deleteIS3_EEEE + 104
	17  WebKit                              0x000000010a841964 _ZN3IPC10Connection18dispatchOneMessageEv + 176
	18  JavaScriptCore                      0x0000000114dde0f9 _ZN3WTF7RunLoop11performWorkEv + 169
	19  JavaScriptCore                      0x0000000114dde3b2 _ZN3WTF7RunLoop11performWorkEPv + 34
	20  CoreFoundation                      0x0000000108324a01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	21  CoreFoundation                      0x000000010830921f __CFRunLoopDoSources0 + 271
	22  CoreFoundation                      0x00000001083087cf __CFRunLoopRun + 1039
	23  CoreFoundation                      0x0000000108308149 CFRunLoopRunSpecific + 409
	24  GraphicsServices                    0x000000010ffb19d7 GSEventRunModal + 62
	25  UIKit                               0x000000010880691d UIApplicationMain + 159
	26  FoursquareAPIClient                 0x0000000106d0bb57 main + 55
	27  libdyld.dylib                       0x000000010d3c9b65 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Crash on HTML error from Foursquare API

Hello there,

We just got a crash when we received a plain HTML error instead of a JSON:

capture d ecran 2017-12-11 a 17 11 34

Foursquare API response was:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>503 No healthy backends</title>
  </head>
  <body>
    <h1>Error 503 No healthy backends</h1>
    <p>No healthy backends</p>
    <h3>Guru Mediation:</h3>
    <p>Details: cache-cdg8727-CDG 1513008552 1297218165</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>

I'm on branch swift4.0 (commit 82a8204d4adf9a08e1740dbd31755cc16ed0e71a)

Authentication issue

After tapping the "Connect To Foursquare" button on the login screen, I am receiving an error: "unexpectedly found nil while unwrapping an Optional value" at line 53 FoursquareAuthViewController. This happens in the example app provided. It also happens in my own app, where I am trying to use the Pod, when I call:

let client = FoursquareAuthClient(clientId: clientId, callback: callback, delegate: self)
client.authorizeWithRootViewController(self)

Any ideas? Thanks!

screen shot 2016-08-08 at 11 54 18 pm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.