When the Touch ID alert is displayed, there is also a "Cancel" button. I would prefer to NOT allow the user to cancel because they are prohibited from continuing any further. 1. Is there a way to remove the "Cancel" button. 2. If the "Cancel" button is required, how can I force the user to re-authenticate with a fingerprint? If authenticate() is called a second time, the Touch ID API just lets them in. There is no alternative passcode and I'd hate to have to code up yet another view controller for it.
func authenticate() {
let myContext:LAContext = LAContext()
let authError:NSErrorPointer = nil
if (myContext.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: authError)) {
myContext.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Press fingerprint", reply: { (success:Bool, error:NSError?) -> Void in
if success == true {
log.debug("SUCCESSFUL AUTHENTICATION")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.performSegueWithIdentifier("showUI", sender: self)
})
}
else {
log.debug("FAILED AUTHENTICATION")
self.authenticate()
}
})
}
}
You need to dispatch your failure call to self.authenticate on the main queue;
func authenticate() {
let myContext:LAContext = LAContext()
let authError:NSErrorPointer = nil
if (myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: authError)) {
myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Press fingerprint", reply: { (success:Bool, error:NSError?) -> Void in
if success {
log.debug("SUCCESSFUL AUTHENTICATION")
DispatchQueue.main.async {
self.performSegueWithIdentifier("showUI", sender: self)
}
}
else {
log.debug("FAILED AUTHENTICATION")
DispatchQueue.main.async {
self.authenticate()
}
}
})
}
}
Related
I have a really common problem trying to create spinning activity indicator during an user authentication task with Firebase
I tried to use CGD dispatch_async but that doesn't seem to handle my issue.
Here is my code
#IBAction func SignMeIn(sender: AnyObject) {
ActivityIndicator.hidden = false
ActivityIndicator.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
NSLog("Before login func")
self.logIn()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
NSLog("After login func")
self.ActivityIndicator.stopAnimating()
self.ActivityIndicator.hidden = true
})
});
}
func logIn(){
myRootRef.authUser(TXT_User.text, password: TXT_Password.text,
withCompletionBlock: { error, authData in
if error != nil {
NSLog(error.debugDescription)
} else {
NSLog("Connected !")
}
})
}
The thing is I surely do something wrong since in debug mode appears in this order :
"Before login func"
"After login func"
"Connected !"
Whereas I should have
"Before login func"
"Connected !"
"After login func"
What am I doing wrong please ?
Thank you very much for your help :) !
Your problem is that you have 2 async tasks
1. Login completion block
2. Activity indicator stop
If you want to stop activity indicator after the login process you should move the code in the completion block like this
#IBAction func SignMeIn(sender: AnyObject) {
ActivityIndicator.hidden = false
ActivityIndicator.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
NSLog("Before login func")
self.logIn()
});
}
func logIn(){
myRootRef.authUser(TXT_User.text, password: TXT_Password.text,
withCompletionBlock: { error, authData in
if error != nil {
NSLog(error.debugDescription)
} else {
NSLog("Connected !")
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
NSLog("After login func")
self.ActivityIndicator.stopAnimating()
self.ActivityIndicator.hidden = true
})
})
}
The problem is that execution returns from logIn immediately, and not when the completion block is called.
To do what you need, you have to make the call to the main queue within the logIn completion handler - like this
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
NSLog("Before login func")
self.logIn()
}
func logIn(){
myRootRef.authUser(TXT_User.text, password: TXT_Password.text,
withCompletionBlock: { error, authData in
// success or fail, you need to stop the Activity Indicator
dispatch_async(dispatch_get_main_queue(), { () -> Void in
NSLog("After login func")
self.ActivityIndicator.stopAnimating()
self.ActivityIndicator.hidden = true
})
});
if error != nil {
NSLog(error.debugDescription)
} else {
NSLog("Connected !")
}
})
}
I'm implementing the login possibility with touchID using Swift.
Following: when the App is started, there is a login screen and a touchID popup - that's working fine. The problem occurs, when the app is loaded from background: I want the touchID popup appear over a login screen if a specific timespan hasn't been exceeded yet - but this time I want the touchID to go to the last shown view before the app entered background. (i.e. if the user wants to cancel the touchID, there is a login screen underneath where he then can authenticate via password, which leads him to the last shown view OR if the touchID authentication succeeded, the login screen should be dismissed and the last shown view presented.)
I really tried everything on my own, and searched for answers - nothing did help me. Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
//notify when foreground or background have been entered -> in that case there are two methods that will be invoked: willEnterForeground and didEnterBackground
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "willEnterForeground", name:UIApplicationWillEnterForegroundNotification, object: nil)
notificationCenter.addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
password.secureTextEntry = true
if (username != nil) {
username.text = "bucketFit"
}
username.delegate = self
password.delegate = self
if let alreadyShown : AnyObject? = def.objectForKey("alreadyShown") {
if (alreadyShown == nil){
authenticateWithTouchID()
}
}
}
willEnterForeground:
func willEnterForeground() {
//save locally that the guide already logged in once and the application is just entering foreground
//the variable alreadyShown is used for presenting the touchID, see viewDidAppear method
def.setObject(true, forKey: "alreadyShown")
if let backgroundEntered : AnyObject? = def.objectForKey("backgroundEntered") {
let startTime = backgroundEntered as! NSDate
//number of seconds the app was in the background
let inactivityDuration = NSDate().timeIntervalSinceDate(startTime)
//if the app was longer than 3 minutes inactiv, ask the guide to input his password
if (inactivityDuration > 2) {
showLoginView()
} else {
def.removeObjectForKey("alreadyShown")
showLoginView()
}
}
}
authenticateWithTouchID():
func authenticateWithTouchID() {
let context : LAContext = LAContext()
context.localizedFallbackTitle = ""
var error : NSError?
let myLocalizedReasonString : NSString = "Authentication is required"
//check whether the iphone has the touchID possibility at all
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
//if yes then execute the touchID and see whether the finger print matches
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString as String, reply: { (success : Bool, evaluationError : NSError?) -> Void in
//touchID succeded -> go to students list page
if success {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.performSegueWithIdentifier("studentsList", sender: self)
})
} else {
// Authentification failed
print(evaluationError?.description)
//print out the specific error
switch evaluationError!.code {
case LAError.SystemCancel.rawValue:
print("Authentication cancelled by the system")
case LAError.UserCancel.rawValue:
print("Authentication cancelled by the user")
default:
print("Authentication failed")
}
}
})
}
}
shouldPerformSegueWithIdentifier:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if (false) { //TODO -> username.text!.isEmpty || password.text!.isEmpty
notify("Login failed", message: "Please enter your username and password to proceed")
return false
} else if (false) { //TODO when backend ready! -> !login("bucketFit", password: "test")
notify("Incorrect username or password", message: "Please try again")
return false
//if the login page is loaded after background, dont proceed (then we need to present the last presented view on the stack before the app leaved to background)
} else if let alreadyShown : AnyObject? = def.objectForKey("alreadyShown") {
if (alreadyShown != nil){
//TODO check whether login data is correct
dismissLoginView()
return false
}
}
return true
}
Thank you in advance.
What you could do is create a AuthenticationManager. This manager would be a shared instance which keep track of whether authentication needs to be renewed. You may also want this to contain all of the auth methods.
class AuthenticationManager {
static let sharedInstance = AuthenticationManager()
var needsAuthentication = false
}
In AppDelegate:
func willEnterForeground() {
def.setObject(true, forKey: "alreadyShown")
if let backgroundEntered : AnyObject? = def.objectForKey("backgroundEntered") {
let startTime = backgroundEntered as! NSDate
//number of seconds the app was in the background
let inactivityDuration = NSDate().timeIntervalSinceDate(startTime)
//if the app was longer than 3 minutes inactiv, ask the guide to input his password
if (inactivityDuration > 2) {
AuthenticationManager.sharedInstance.needsAuthentication = true
}
}
}
Then, subclass UIViewController with a view controller named SecureViewController. Override viewDidLoad() in this subclass
override fun viewDidLoad() {
super.viewDidLoad()
if (AuthenticationManager.sharedInstance().needsAuthentication) {
// call authentication methods
}
}
Now, make all your View Controllers that require authentication subclasses of SecureViewController.
I have a login screen that ensures that there is valid data in the input fields before attempting to login. Or so I thought.
The problem is when we come back from another screen that "logouts" the user, if I submit with an invalid username password combo after returning to this page, I see the error dialog as expected, but after dismissing it I am then taken to the next view controller as if I logged in.
Any help please?
#IBAction func btnSubmit(sender: UIButton) {
if txtUsername.text == "" || txtPassword.text == "" {
//they're missing a username or password
displayAlert("Missing Field(s)", message: "Please enter both a username and password")
}else {
//we check if they're in signup/login mode
if Switch.on {
//user is in signup mode
if txtPassword.text != txtConfirmPassword.text {
//the password fields do not match
displayAlert("Mismatched Passwords", message: "Please enter matching passwords")
}else {
//the password fields do match, and the user can register with this username/email and password
var user = PFUser()
user.username = txtUsername.text
user.password = txtPassword.text
// other fields can be set just like with PFObject
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as! String
// Show the errorString somewhere and let the user try again.
self.displayAlert("Signup Error", message: errorString)
} else {
// Hooray! Let them use the app now.
self.performSegueWithIdentifier("register", sender: self)
}
} }
}else {
//user is in login mode and we can submit credentials
PFUser.logInWithUsernameInBackground(txtUsername.text, password:txtPassword.text) {
(user: PFUser?, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as! String
// Show the errorString somewhere and let the user try again.
self.displayAlert("Login Error", message: errorString)
} else {
if PFUser.currentUser()!.username != nil {
// Do stuff after successful login.
self.performSegueWithIdentifier("login", sender: self)
}
}
}
}
}
}
Here's my logout call from the other page
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "logout" {
PFUser.logOut()
}
}
I believe that the issue is that you're not uses the succeeded Bool that is returned in the completion handler. When I log users in with Parse I use that primarily to see if log in was successful or not, if it is not successful I will then check what the error message was. This should stop you from allowing the user to continue into the app when they have not been logged in correctly.
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if error != nil {
let errorString = error!.userInfo?["error"] as! String
// Show the errorString somewhere and let the user try again.
self.displayAlert("Signup Error", message: errorString)
} else {
if succeeded {
// Hooray! Let them use the app now.
self.performSegueWithIdentifier("register", sender: self)
} else {
//Something went wrong
}
}
}
I'm registering new users under my application that uses facebook API and Parse. The users are being created without segueing to another view. Unfortunately, if I try to segue for a next view the user is created without the name and email info. I know this runs in background so i need a solution to know when the execution is finished. Any ideas?
override func viewDidLoad() {
FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
}
override func viewDidAppear(animated: Bool) {
if let access = FBSDKAccessToken.currentAccessToken() {
PFFacebookUtils.logInInBackgroundWithAccessToken(access, block: {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
println("already registred user")
self.performSegueWithIdentifier("firstView", sender: self)
} else {
println("Uh oh. There was an error logging in.")
}
})
}
}
#IBAction func fbLoginButtonTouchUpInside (sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions(["public_profile", "email", "user_friends"], block: { (user, error) in
if let user = user {
User.updateUserInfo()
println("new user")
//THIS SEGUE IS CRASHING
self.performSegueWithIdentifier("newUserSegue", sender: self)
} else {
println("Uh oh. The user cancelled the Facebook login.")
}
})
}
Try this. AFAK you must perform on main thread that is why it is crashing:
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("newUserSegue", sender: self)
}
The problem here was that I was running another background task inside
PFFacebookUtils.logInInBackgroundWithReadPermissions
The solution was to add a completion handler to User.updateUserInfo() and perform the segue inside of it.
I want to share an image on Facebook using swift language. I am able to share image using Objective C.
I tried using
1) How to Share image + text with facebook in swift iOS?
but not working, then I tried using other options but not able to share image using swift language. then I tried this
2) https://github.com/rajeshsegu/facebook-ios-swift/tree/master/FBApp
I copied Facebook.swift and write another function for share image my code for Facebook.swift
import Foundation
import Social
let FB = Facebook();
class Facebook {
var fbSession:FBSession?
init(){
self.fbSession = FBSession.activeSession();
}
func hasActiveSession() -> Bool{
let fbsessionState = FBSession.activeSession().state;
if ( fbsessionState == FBSessionState.Open
|| fbsessionState == FBSessionState.OpenTokenExtended ){
self.fbSession = FBSession.activeSession();
return true;
}
return false;
}
func login(callback: () -> Void){
let permission = ["publish_actions","email","user_location","user_birthday","user_hometown","user_photos","user_about_me"];
let activeSession = FBSession.activeSession();
let fbsessionState = activeSession.state;
var showLoginUI = true;
if(fbsessionState == FBSessionState.CreatedTokenLoaded){
showLoginUI = false;
}
if(fbsessionState != FBSessionState.Open
&& fbsessionState != FBSessionState.OpenTokenExtended){
FBSession.openActiveSessionWithPublishPermissions(permission, defaultAudience: FBSessionDefaultAudience.Friends, allowLoginUI: showLoginUI, completionHandler: { (session:FBSession!, state:FBSessionState, error:NSError!) -> Void in
if(error != nil){
println("Session Error: \(error)");
}
self.fbSession = session;
// println("Session : \(self.fbSession?.permissions)");
callback();
})
// FBSession.openActiveSessionWithReadPermissions(
// permission,
// allowLoginUI: showLoginUI,
// completionHandler: { (session:FBSession!, state:FBSessionState, error:NSError!) in
//
// if(error != nil){
// println("Session Error: \(error)");
// }
// self.fbSession = session;
// println("Session : \(self.fbSession?.permissions)");
// callback();
//
// }
// );
return;
}
callback();
}
func logout(){
self.fbSession?.closeAndClearTokenInformation();
self.fbSession?.close();
}
func getInfo(){
FBRequest.requestForMe()?.startWithCompletionHandler({(connection:FBRequestConnection!, result:AnyObject!, error:NSError!) in
if(error != nil){
println("Error Getting ME: \(error)");
}
println("\(result)");
var dictData:NSDictionary!=result as? NSDictionary
});
}
func handleDidBecomeActive(){
FBAppCall.handleDidBecomeActive();
}
func shareImage (imageName:UIImageView){
let fbsessionState = FBSession.activeSession().state;
if(fbsessionState == FBSessionState.Open)
{
//var arr : NSArray=NSArray(array: ["publish_actions"])
self.fbSession?.requestNewPublishPermissions(["publish_actions"], defaultAudience:FBSessionDefaultAudience.Friends, completionHandler: { (session:FBSession!, error:NSError!) -> Void in
if(error == nil){
var requestConneciton:FBRequestConnection=FBRequestConnection()
requestConneciton.errorBehavior=FBRequestConnectionErrorBehavior.None
requestConneciton.addRequest(FBRequest(forUploadPhoto:imageName.image)) { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
println("\(error)");
println("\(result)");
//[self showAlert:#"Photo Post" result:result error:error];
}
requestConneciton.start()
}
else if(error.fberrorCategory == FBErrorCategory.UserCancelled){
var alt:UIAlertView!=UIAlertView(title:"Permission denied", message:"Unable to get permission to post", delegate:nil, cancelButtonTitle:"Ok")
alt.show()
}
})
}
}
func showAlert(msg:NSString!,result:AnyObject,error:NSError!) {
var alertTitle:NSString!
var alertMsg:NSString!;
if (error == nil) {
if((error.fberrorUserMessage != nil && FBSession.activeSession().isOpen) ) {
alertTitle = "";
}
else{
// Otherwise, use a general "connection problem" message.
alertMsg = "Operation failed due to a connection problem, retry later.";
}
}
else {
//var dictResult:NSDictonary = result as NSDictionary
alertMsg="Successfully posted "
var alertObj:UIAlertView!=UIAlertView(title:"Demo App", message:alertMsg, delegate:nil, cancelButtonTitle:"Ok");
alertObj.show();
}
}
func performPublishAction(action:() -> Void){
var arrP:NSArray!=NSArray(array: ["publish_actions"]);
fbSession?.requestNewPublishPermissions(arrP, defaultAudience:FBSessionDefaultAudience.Friends, completionHandler: { (session:FBSession!, error:NSError!) -> Void in
if(error == nil){
action()
}
else if(error.fberrorCategory == FBErrorCategory.UserCancelled){
var alt:UIAlertView!=UIAlertView(title:"Permission denied", message:"Unable to get permission to post", delegate:nil, cancelButtonTitle:"Ok")
alt.show()
}
})
}
}
and In ViewController.swift
import UIKit
class ViewController: UIViewController,FBLoginViewDelegate {
#IBOutlet var imageObj:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnFBLoginClick(sender: UIButton) {
FB.login(self.handleLogin);
}
func handleLogin(){
println("SUCCESS");
FB.getInfo();
}
#IBAction func btnShareclick(sender: UIButton) {
FB.shareImage(imageObj)
}
}
Login button click working perfect and it can fetch all data of login user, but when i share the image using FB.shareImae(imageObj) its give me a permission error, I am working on this point from last 2 days now I am stuck. if i write same code in Objective C its working fine.
eror :
permissions:(
"public_profile",
email,
"user_friends"
)>, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 200;
message = "(#200) Permissions error";
type = OAuthException;
};
};
code = 403;
}}
Can any one help me? to find out this problem...
I don't want to use SLComposeViewController, I want to use Facebook framework.
Thank you in advance!
This is a code i created in an old project. Give it a try ;)
#IBAction func btn_Share(sender: AnyObject) {
let facebookPost = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookPost.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
//Code to deal with it being cancelled
break
case SLComposeViewControllerResult.Done:
//Code here to deal with it being completed
break
}
}
facebookPost.setInitialText("Test Facebook") //The default text in the tweet
facebookPost.addImage(masked_image) //Add an image
facebookPost.addURL(NSURL(string: "http://facebook.com")) //A url which takes you into safari if tapped on
self.presentViewController(facebookPost, animated: false, completion: {
//Optional completion statement
})
}`
I solved my problem (I put my comment here cause I don't have enough place to explain). So I was trying to share a link on Facebook.
First I was wrong with the params, I was writing
var params=["http://www.google.com":"link"]
and the correct way is
var params=["link":"http://www.google.com"]
(Because of the way it's written in Obj-C on dev.facebook.com I got confused).
Secondly, if I follow the logic of the original github project, I call my function this way:
FB.performPublishAction(self.shareLinkOnFB)
where performPublishAction (in the Facebook class) ask for the new publish permissions:
func performPublishAction(action:() -> Void){
fbSession?.requestNewPublishPermissions(fbSettings.publishPermissions, defaultAudience: fbSettings.publishAudience, completionHandler: {(session:FBSession!, error:NSError!)->Void in
if error==nil {
println("perform publish action / no error")
action()
}else{
println("perform publish action / error: \(error.localizedDescription)")
println(error)
}
})
and shareLinkOnFb function calls FB.requestToShareLink():
func shareLinkOnFB(){
FB.requestToShareLink()
}
func requestToShareLink(){
var params=["link":"\(self.link)", "message":"\(self.message)"]
FBRequestConnection.startWithGraphPath("/me/feed", parameters:params, HTTPMethod: "POST", completionHandler: { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) in
if error != nil {
println("Request to share link / An error occurred: \(error.localizedDescription)")
println(error)
//handle error
}else{
println("link successfully posted: \(result)")
}
})
}
I'm not sure you have the same problem but I hope it can help you.
check this guy's answer https://stackoverflow.com/a/58481784/12250012
also now facebook is not allowing programatically.
You have to request in your facebook app the "publish_actions" permissions.
I have had the same issue. I have solve it by writing such code. If you have an active Facebook session this code is perfectly work.
func performPublishAction(callBack:() -> Void){
self.fbSession?.requestNewPublishPermissions(["publish_actions"], defaultAudience: FBSessionDefaultAudience.Friends, completionHandler: {(session:FBSession!, error:NSError!)->Void in
if error == nil {
callBack()
}else{
println(error)
}
})
}
func shareImage (imageName:UIImage){
var requestConneciton:FBRequestConnection=FBRequestConnection()
requestConneciton.errorBehavior=FBRequestConnectionErrorBehavior.None
requestConneciton.addRequest(FBRequest(forUploadPhoto:imageName)) { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
println("\(error)");
println("\(result)");
}
requestConneciton.start()
}
use of these methods will be as:
facebookHelper.performPublishAction { () -> Void in
facebookHelper.shareImage(self.itemImage)
//NkCommonClass.displayToast("Posted successfully on your wall.")
}