2017-07-26 97 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"; 
} 

希望这会解决这个问题。