Hallo Hier verwende ich appdeligate für eine bestimmte Methode wie Login in den Apps und Abmeldung von den Anwendungen. Hier habe ich einen Code , die derzeit arbeitet groß zu tun, aber das Problem ist, wenn Anfrage Anmeldung an den Server senden dann die Spinner Ansicht nicht funktioniertspinnerView funktioniert nicht in appdelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
spinnerInitialization();
email = NSUserDefaults.standardUserDefaults().stringForKey("email")\\Getting Email from local storage
password = NSUserDefaults.standardUserDefaults().stringForKey("password")\\\\Getting Password from local storage
if ((email == nil) && (password == nil)){
}
else if ((email == "") && (password == "")) {
}
else {
loginRequest(email!, password: password!)
}
return true
}
Hier Als nächstes wird die Login-Funktion
func loginRequest(var email: String,password: String){
let request = NSMutableURLRequest(URL: NSURL(string: "www.example.com/login")!)
request.HTTPMethod = "POST"
let postString = "email=\(email)&password=\(password)&token=\("ttttttt")"
dispatch_async(dispatch_get_main_queue()) {
self.startSpinner()
}
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
if(error != nil){
let nsError = error! as NSError
let dialog = UIAlertController(title: "Internal Server Error?", message: nsError.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(ACTION) in
print("Ok Button Action ")
}
dialog.addAction(okAction);
dispatch_async(dispatch_get_main_queue(), {
//Code that presents or dismisses a view controller here
self.window?.rootViewController?.presentViewController(dialog, animated: true, completion: nil)
})
}
else{
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
let status = json["status"]as? Int
let message = json["message"]as? String
if(status == 0){
let dialog = UIAlertController(title: "Login Unsuccessful?", message: message, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(ACTION) in
print("Ok Button Action ")
}
dialog.addAction(okAction);
dispatch_async(dispatch_get_main_queue(), {
self.window?.rootViewController?.presentViewController(dialog, animated: true, completion: nil)
})
}
else
{
let newModel = UserDetails()
let result = json["data"]
let userId = result!!["id"] as? String
let companyId = result!!["companyId"] as? String
let name = result!!["name"] as? String
let companyName = result!!["companyName"] as? String
email = (result!!["email"] as? String)!
let userType = result!!["userType"] as? String
NSUserDefaults.standardUserDefaults().setObject(email, forKey: "email")
NSUserDefaults.standardUserDefaults().setObject(userId, forKey: "userId")
NSUserDefaults.standardUserDefaults().setObject(companyId, forKey: "companyId")
NSUserDefaults.standardUserDefaults().setObject(name, forKey: "name")
NSUserDefaults.standardUserDefaults().setObject(companyName, forKey: "companyName")
NSUserDefaults.standardUserDefaults().setObject(userType, forKey: "userType")
NSUserDefaults.standardUserDefaults().setObject(password, forKey: "password")
NSUserDefaults.standardUserDefaults().setObject("afdg2015", forKey: "token")
newModel.userId = userId!
newModel.companyId = companyId!
newModel.userName = name!
newModel.companyName = companyName!
if userType == "0"{
newModel.asEmployeer = true
NSUserDefaults.standardUserDefaults().setObject("true", forKey: "asEmployeer")
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
}
else {
newModel.asEmployeer = false
NSUserDefaults.standardUserDefaults().setObject("false", forKey: "asEmployeer")
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
}
self.userDetails.append(newModel)
dispatch_async(dispatch_get_main_queue()) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var scheduleController = storyboard.instantiateViewControllerWithIdentifier("scheduleHome100")
if (newModel.asEmployeer == false){
//EmployeeScheduleViewController
scheduleController = storyboard.instantiateViewControllerWithIdentifier("EmployeeScheduleViewController")
}
let drawerViewController = storyboard.instantiateViewControllerWithIdentifier("DrawerViewController")
let leftSideNav = UINavigationController(rootViewController: drawerViewController)
let centerNav = UINavigationController(rootViewController: scheduleController)
self.centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav)
self.centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
self.centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
self.window!.rootViewController = self.centerContainer
self.window!.makeKeyAndVisible()
}
}
} catch {
print("error serializing JSON: \(error)")
}
dispatch_async(dispatch_get_main_queue(), {
self.stopSpinner()
})
}
}
task.resume();
}
Darüber hinaus ist hier Spinnern funktionieren
func startSpinner(){
print("In startSpinner Function")
view.addSubview(spinnerView)
}
func stopSpinner(){
print("In stopSpinner Function")
let subViews = view.subviews
for subView in subViews{
if subView.tag == 1000{
subView.removeFromSuperview()
}
}
}
func spinnerInitialization(){
print("In spinnerInitialization Function")
spinnerView = UIView(frame: CGRect(x:0, y:0,width: 250,height: 50))
spinnerView.backgroundColor = UIColor.blackColor()
spinnerView.layer.cornerRadius = 10
let wait = UIActivityIndicatorView(frame: CGRect(x:0,y: 0,width: 50,height: 50))
wait.color = UIColor.whiteColor()
wait.hidesWhenStopped = false
wait.startAnimating()
let text = UILabel(frame: CGRect(x:60,y:0,width:200,height: 50))
text.textColor = UIColor.whiteColor()
text.text = "Please wait..."
spinnerView.addSubview(wait)
spinnerView.addSubview(text)
spinnerView.center = view.center
spinnerView.tag = 1000
}
UPDATE wenn ich die Anfrage von Viewcontroller wie schicke als
@IBAction func loginAction(sender: AnyObject) {
let email = textEmail.text
let password = textPassword.text
spinnerInitialization()
startSpinner()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.loginRequest(email!, password: password!)
}
func spinnerInitialization(){
spinnerView = UIView(frame: CGRect(x:0, y:0,width: 250,height: 50))
spinnerView.backgroundColor = UIColor.blackColor()
spinnerView.layer.cornerRadius = 10
let wait = UIActivityIndicatorView(frame: CGRect(x:0,y: 0,width: 50,height: 50))
wait.color = UIColor.whiteColor()
wait.hidesWhenStopped = false
wait.startAnimating()
let text = UILabel(frame: CGRect(x:60,y:0,width:200,height: 50))
text.textColor = UIColor.whiteColor()
text.text = "Please wait..."
spinnerView.addSubview(wait)
spinnerView.addSubview(text)
spinnerView.center = self.view.center
spinnerView.tag = 1000
}
func startSpinner(){
view.addSubview(spinnerView)
}
func stopSpinner(){
let subViews = view.subviews
for subView in subViews{
if subView.tag == 1000{
subView.removeFromSuperview()
}
}
}
Dann mir der Spinner zeigt
Bitte helfen, dieses Problem Dank
Bitte (dispatch_get_main_queue ()) wo hast du spinner – iMHitesh
ohky ich versuche –
@HiteshSurani Nein, es funktioniert nicht alle drei Funktionen Aufruf von Spinner, aber nicht zeigen –