Reputation: 127
I have this interface in Objective C that I am trying to call from Swift and it does not seem fall into the completion code. Not sure what I am missing!
@interface WPLogin : NSObject
/*!
* @brief Logs into the application returning success or failure with an error object.
* If login is successful the credential is automatically stored
*
* @param serverURLString Address to login to
* @param username Username to login with
* @param password Password for user
* @param completion Completion block to be called after attempting login
*/
- (void)loginToURL:(NSString *)serverURLString
withUsername:(NSString *)username
password:(NSString *)password
completion:(void (^)(WPLoginStatus success, NSError * error))completion;
@end
the calling function from Swift looks like this...
var uid = "test"
var pwd = "test"
var url = "http://www.google.com"
var loginAuth = WPLogin();
loginAuth.loginToURL(url, withUsername: uid, password: pwd, completion: { (status:WPLoginStatus, error:NSError!) -> Void in
println("Inside Login")
})
bridging file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "WPLogin.h"
version that works in Objective C
if (!self.login) {
self.login = [WPLogin new];
}
[self.login loginToURL:url
withUsername:uid
password:pwd
completion:^(WPLoginStatus status, NSError *error) {
NSLog(@"complete");
}];
Upvotes: 0
Views: 417
Reputation: 535138
The problem is that in your Swift version the WPLogin object is a local variable (var loginAuth
). Therefore it dies before it has a chance to do anything. Make it a property, as in your Objective-C version (self.login
):
class MyClass {
var login : WPLogin = WPLogin()
func myMethod () {
var uid = "test"
var pwd = "test"
var url = "http://www.google.com"
self.login.loginToURL(url, withUsername: uid, password: pwd, completion: {
(status:WPLoginStatus, error:NSError!) -> Void in
println("Inside Login")
})
}
}
This object needs to take action over time - going out on the Internet, logging in, calling your completion handler - and it cannot do that if it dies instantly, as it does in your Swift code. It must persist over significant time.
Upvotes: 1