Okay I've got the bridging Header all set up and I can now use my objective c code but I'm having a hard time understanding how to use objective c methods in swift.
This is the method in my .m file that I imported
-(void)sendWithLoadMessage:(NSString *)loadMessage
successMessage:(NSString *)successMessage
failureMessage:(NSString *)failureMessage
recoveryOptions:(NSString *)recoveryOptions
success:(void (^)(CustomResponse *response))success
failure:(void (^)(CustomResponse *response))failure
and here is how I use it in my objective c app. I need to know how to do this in swift:
CustomRequest *request = [_user login];
[request sendWithLoadMessage:#"Signing In"
successMessage:#"Signed In"
failureMessage:#"Failed to log in"
recoveryOptions:#"try again"
success:^(CustomResponse *response) {
....
}
To use an Objective C Method in swift, you need to treat the method like a Swift function. You call functions on an instance through .
For you, it would be like this:
let request = _user.login
request.sendWithLoadMessage("Signing In", successMessage: "Signed In", failureMessage: "Failed to log in", recoveryOptions: "try again", response: { CustomResponse in
println(response)
})
Taking advantage of trailing closures here:
let request = _user.login
request.sendWithLoadMessage("Signing In", successMessage: "Signed In", failureMessage: "Failed to log in", recoveryOptions: "try again") { response: CustomResponse in
println(response)
}
Using the $ syntax, you can make this even shorter by doing it this way. If you already know the parameters passed back in the block, you can access the list of parameters without typing it out by calling $indexOfParameter. The $ syntax is 0 indexed so your first parameter would be $0, your second parameter would be $1, etc.
let request = _user.login
request.sendWithLoadMessage("Signing In", successMessage: "Signed In", failureMessage: "Failed to log in", recoveryOptions: "try again") {
println($0)
}
Related
I've been working with firebase in clojurescript and I've attempted the advice here:
How to catch any Javascript exception in Clojurescript?
The code I have so far is:
(defn register [email password]
(try
(let [reg (.createUserWithEmailAndPassword (.auth firebase) email password)
_ (.log js/console reg)]
reg)
(catch :default e
(.log js/console "Register Error:" e))))
As I suspected the reason it's not been caught is that the evaluation could have been occurring later and this was to test the idea.
I have also tried:
(defn register [email password]
(try
(.createUserWithEmailAndPassword (.auth firebase) email password)
(catch :default e
(.log js/console "Register Error:" e))))
I call this using:
(apply register ((juxt :email :password) #app-state))
If I pass it an email I've already registered with I get as expected an auth/email-already-in-use error, but I can't catch it.
www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyDzsfz98Y6Pjl1n-uAzfI6GHWKqShFdRI4:1
POST https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyDzsfz98Y6Pjl1n-uAzfI6GHWKqShFdRI4 400 ()
firebase.inc.js:81
Uncaught
Qcode: "auth/email-already-in-use"
message: "The email address is already in use by another account."
__proto__: Error
(anonymous) # firebase.inc.js:81
How do I catch it? Or is the only way of handling this by using the raw javascript?
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
Firebase auth is asynchronous. Javascript's (and, generally, Clojurescript's) try...catch does not work in callbacks. Call the catch function provided by the Firebase handler with your callback on error, like this - this is the literal translation of the JS code in your post above:
(defn register [email password]
(.. firebase auth (createUserWithEmailAndPassword email password)
(catch (fn [e] (.log js/console "Register Error:" e))))
So the API I'm working with will sometimes send an error message in the response body when a request fails. This is located in response.data. Sometimes it's JSON, sometimes it's a string. I'm using the validate method so result.value is nil when an error occurs.
Is there a way of having Alamofire serialize the data from NSData to a string or for JSON to [ String : AnyObject ] like it would if the response was successful?
I would like to keep using the validate method.
EDIT:
Here's a link to a feature request I started on the Alamofire GitHub project.
https://github.com/Alamofire/Alamofire/issues/1459
There is not currently. I'm actually working on this very feature in Alamofire 4 right now. In Alamofire 3, you'll have to parse the response.data yourself if you get that validation error. In Alamofire 4, you'll at least have access to the response.data at the time of validation as well as be able to customize the Error that is generated by validation.
Most likely what the final solution will be is the ability to check in validation if you know there's going to be an error (checking response status code and headers). Then based on the type of error, you could parse the response.data to extract the error message from the server and throw a VERY SPECIFIC error from validation. This is most likely what the new system will allow. This way you could identify OAuth2 access token errors right in validation and throw your own custom error rather than having to use a convoluted system of response serializers to do it.
Swift 4
If you get an error, you can try parsing the response data as a string or as json.
import Alamofire
import SwiftyJSON
Alamofire.request("http://domain/endpoint", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil)
.validate()
.responseJSON(completionHandler: { response in
if let error = response.error {
if let data = response.data {
if let errorString = String(bytes: data, encoding: .utf8) {
print("Error string from server: \(errorString)")
} else {
print("Error json from server: \(JSON(data))")
}
} else {
print("Error message from Alamofire: \(error.localizedDescription)")
}
}
guard let data = response.result.value else {
print("Unable to parse response data")
return
}
print("JSON from server: \(JSON(data))")
})
When trying to define a Parse Cloud Code server side function to handle login I get 400 Bad Request when I try to call it. When I look at the Parse logs it records the error "Failed with: ReferenceError: user is not defined". But the user is definitely defined!
Below is the definition of the cloud code for LogIn:
Parse.Cloud.define("LogIn", function(request, response)
{
user.logIn(request.params.username, request.params.password,
{
success: function(user)
{
response.success(
{
"success": "Log in successful."
});
},
error: function(user, error)
{
// We must respond with a success in order to access the
// result of the request inside Unity.
response.success(
{
"error": "Log in failed.",
"code": error.code,
"message": error.message
});
}
});
});
From Unity I make this call to the LogIn coud code function:
ParseCloud.CallFunctionAsync<Dictionary<string, object>> ("LogIn", userInfo).ContinueWith (t =>
{
etc.....
}
I get the following error logged in the server side Parse logs when I call the above from Unity using user sashas123 and also student123:
E2014-09-26T17:06:18.001Z] v8: Ran cloud function LogIn with: Input:
{"username":"sashas123","password":"test"} Failed with:
ReferenceError: user is not defined
at main.js:43:5
E2014-09-26T17:38:50.474Z] v10: Ran cloud function LogIn with:
Input: {"username":"student123","password":"test"} Failed with:
ReferenceError: user is not defined
at main.js:43:5
The following snapshot from the Data Browser shows that the above users are definitely defined:
![Parse User class][1]
Is there any issue with calling user.LogIn on the server side through Cloud Code or is this a Unity issue?
It looks like user.logIn should be request.user.logIn :)
I find it's best to handle the case where the function may be called without a logged in user too:
if (request.user.logIn != null)
{
...
}
else
{
response.error("This function must be called with a logged in user!");
}
Hope this help!
I created a new developer account and I am having a problem authenticating with the REST API.
POST https://rest.developer.yodlee.com/services/srest/restserver/v1.0/authenticate/coblogin
{ cobrandLogin: 'sbCob*****',
cobrandPassword: '**********' }
the system responds with:
{ Error: [ { errorDetail: 'Internal Core Error has occurred' } ] }
am I doing something wrong?
I am testing the API with Postman and apparently I need to send the params with x-www-form-urlencoded to make it work. Using the default form-data lead to the above mentioned error.
In my case, this was solved by changing the content-type as per http://developer.yodlee.com/Aggregation_API/Aggregation_Services_Guide/Aggregation_REST_API_Reference
require 'rest-client'
module Yodlee
def self.login_to_yodlee
site = self.site_resource
login_hash = {
cobrandLogin: 'yourlogin',
cobrandPassword: 'yourpassword'
}
begin
response = site["/authenticate/coblogin"].post login_hash, :'content-type' => 'application/x-www-form-urlencoded'
puts response
rescue RestClient::ResourceNotFound => ex
raise Exception.new(ex.response)
rescue Exception => ex
raise Exception.new(ex)
end
end
def self.site_resource
RestClient::Resource.new('https://rest.developer.yodlee.com/services/srest/restserver/v1.0')
end
end
Yodlee.login_to_yodlee
Generally, this error comes when you do not provide the input parameter's name correctly; while in this mentioned code above I could see that both of them are correct. I'd suggest you to please double check the input parameter name(case sensitive) as well properly. And just to mention you should be sending it as two different parameters i.e., 'cobrandLogin' and cobrandPassword.
I would like to know how to use rails as backend for my iOS app.
All I need is a User with email and password to authenticate using devise.
I already have a User created with devise and rails 4.
I did find this post http://jessewolgamott.com/blog/2012/01/19/the-one-with-a-json-api-login-using-devise/ explaining what I need, but some things are still missing.
When I try to do a POST via my iOS app, I get the message "Can't verify CSRF token authenticity". How do I solve that without skipping the filter verify_authenticity_token ?
How would the request code for the iOS look like? Right now I'm doing a POST to http://localhost:3000/api/users/sign_in.json and setting the HTTPBody = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&jsonError], but the rails server is receiving only a string as key with the entire json dictionary, not an actual json dictionary.
params = {"{\"user\":{\"email\":\"qwe\",\"password\":\"123\"}}"=>nil, "action"=>"create", "controller"=>"api/sessions", "format"=>"json"}
How would I do an https request instead of http, so I can hide the password and email fields in case someone else tries to watch my internet traffic?
Thank you very much.
To use Rails Applications Mobile and Android and IOS, necessarily you have to use JSONP: example:
JS sample:
$.ajax({
url: '/api_mobile',
jsonp: "callback",
dataType: "jsonp",
cache: true,
data: {method: 'login', other_data ...},
success: function(res) {
// response object
console.log(res)
},
error: function(request, status, error) {
alert("Error server: " + request.status);
}
});
RAILS 4:
protect_from_forgery with: :exception, only: :api_mobile
# route /api_mobile
def api_mobile
json = {error: 'Not found Method'}
case params[:method]
when: 'login'
if User.login(params[:username], params[:password])
json = {notice: 'Login success'}
else
json = {error: 'Error Username or Password'}
end
end
render json: json, :callback => params[:callback]
end
All functions must be personalized and parameterized