Created
January 28, 2018 05:32
-
-
Save hboon/5dc789454242e65434681b937ebc47e9 to your computer and use it in GitHub Desktop.
POST with NSURLSession in RubyMotion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def session | |
return @session if @session | |
configuration = NSURLSessionConfiguration.defaultSessionConfiguration.tap do |conf| | |
conf.HTTPAdditionalHeaders = {'Accept'=>'application/json', | |
'Accept-Language'=>'en', | |
'User-Agent'=> user_agent} | |
end | |
@session = NSURLSession.sessionWithConfiguration(configuration) | |
@session | |
end | |
def post_example(aURLString, aDictionary={}, &block) | |
aDictionary = aDictionary.mutableCopy | |
aDictionary['apikey'] = api_key | |
request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString("#{url_prefix_v2}#{aURLString}")).tap do |v| | |
v.HTTPMethod = 'POST' | |
end | |
data = NSJSONSerialization.dataWithJSONObject(aDictionary, options: 0, error: nil) | |
task = session.uploadTaskWithRequest(request, fromData: data, completionHandler: proc {|data, response, error| | |
handle_api_result(data, error, &block) | |
}) | |
task.resume | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment