2017-07-26 127 views
0

我正在使用iOS(ObjC)的UIWebView使用WebView應用程序。該應用程序具有一個登錄功能,可以使用className進行分析。登錄後UIWebView不顯示用戶名

根據日誌該應用程序正在獲取用戶名,但它沒有加載它,並與卡住你好,登錄消息。我正在使用NSUserDefaults來登錄或不登錄用戶。菜單不會重新加載以顯示「歡迎用戶名」,因爲它應該是。

的代碼如下

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    WIDTH = [UIScreen mainScreen].applicationFrame.size.width; 

    logoutURL = @"http://XXXXdomain.com/index.php?a=logout&type=app"; 

    preferences = [[NSUserDefaults alloc] init]; 
    NSString *savedSessionId = [preferences objectForKey:@"sessionId"]; 
    islogedin = [preferences objectForKey:@"islogedin"]; 
    userName = @""; 
    if([preferences objectForKey:@"userName"]){ 
     userName = [preferences objectForKey:@"userName"]; 
    } 
    if([islogedin isEqualToString:@"true"]){ 
     NSLog(@"arrNavigation=======%@---------%ld", arrNavigation, (long)openSection); 
     [self reloadMenu]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setName:) name:@"name" object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMenu) name:@"reloadMenu" object:nil]; 
    } else { 
     NSLog(@"arrNavigation=======%@---------%ld", arrNavigation, (long)openSection); 
     [self reloadMenu]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLogout:) name:UIApplicationDidBecomeActiveNotification object:nil]; 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMenu) name:@"reloadMenu" object:nil]; 
    } 
} 

-(void)setName:(NSNotification *)notification{ 
    NSLog(@"logged in & received--------"); 
    NSString *name = [notification object]; 
    NSString *msg = [NSString stringWithFormat:@"Welcome %@", name]; 
    [signinBtn setTitle:msg forState:UIControlStateNormal]; 
    logoutBtn.hidden = NO; 
    islogedin = @"true"; 
} 

-(void)setLogout:(NSNotification *)notification{ 
    NSLog(@"loggedout--------"); 
    //NSString *name = [notification object]; 
    NSString *msg = [NSString stringWithFormat:@"Hello, Sign In"]; 
    [signinBtn setTitle:msg forState:UIControlStateNormal]; 
    logoutBtn.hidden = YES; 
    islogedin = @"false"; 
} 

回答

0

顯然,代碼是另一活動,應定義登錄會話的循環。

排序代碼

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    WIDTH = [UIScreen mainScreen].applicationFrame.size.width; 

    logoutURL = @"http://XXXXdomain.com/index.php?a=logout&type=app"; 

    preferences = [[NSUserDefaults alloc] init]; 
    //NSString *savedSessionId = [preferences objectForKey:@"sessionId"]; 
    islogedin = [preferences objectForKey:@"islogedin"]; 
    userName = @""; 
    if([preferences objectForKey:@"userName"]){ 
     userName = [preferences objectForKey:@"userName"]; 
    } 
    if([islogedin isEqualToString:@"true"]){ 
     NSString *msg = [NSString stringWithFormat:@"Welcome %@", userName]; 
     [signinBtn setTitle:msg forState:UIControlStateNormal]; 
     logoutBtn.hidden = NO; 
    } 
    else{ 
     [signinBtn setTitle:@"Hello, Sign In" forState:UIControlStateNormal]; 
     logoutBtn.hidden = YES; 
    } 

    // Do any additional setup after loading the view. 

    NSLog(@"arrNavigation=======%@---------%ld", arrNavigation, (long)openSection); 
    [self reloadMenu]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setName:) name:@"name" object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMenu) name:@"reloadMenu" object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLogout:) name:@"logout" object:nil]; 

} 

-(void)setName:(NSNotification *)notification{ 
    NSLog(@"logged in & received--------"); 
    NSString *name = [notification object]; 
    NSString *msg = [NSString stringWithFormat:@"Welcome %@", name]; 
    [signinBtn setTitle:msg forState:UIControlStateNormal]; 
    logoutBtn.hidden = NO; 
    islogedin = @"true"; 
} 

-(void)setLogout:(NSNotification *)notification{ 
    NSLog(@"loggedout--------"); 
    //NSString *name = [notification object]; 
    NSString *msg = [NSString stringWithFormat:@"Hello, Sign In"]; 
    [signinBtn setTitle:msg forState:UIControlStateNormal]; 
    logoutBtn.hidden = YES; 
    islogedin = @"false"; 
} 

希望這會解決這個問題。