1

현재 iOS 응용 프로그램에 문제가 있습니다.
점진적인 로그인 패턴을 사용하려고합니다. 즉, 로그인을하지 않아도 일부 응용 프로그램에 액세스 할 수 있습니다. 사용자가 로그인을 필요로하는 UIView의 (컨트롤러)에 액세스하려고 할 때 사용자가 로그인이 필요한 모든 탐색 항목을 볼 수있는 모든 시간에탭 막대 컨트롤러 (스토리 보드)가있는 UIViewController에 대한 액세스 금지

  • , 그들은 것입니다 다음과 같이

    필수 기능은 UIAlertView로 로그인하라는 메시지가 나타납니다. (UIAlertView는 시작된 Segue 대상이 제한되어 있음을 앱이 인식하면 나타납니다.)

처음에는 지정한 초기화 프로그램 (initWithCoder)에서 사용자가 로그인했는지 확인하기 위해 NSUserDefaults를 검사 할 UIViewController 하위 클래스를 사용했습니다. 그런 다음 그 서브 클래 싱을 해제했습니다. 다음과 같이 제한했다 :

  • 가있는 UIViewController, 즉있는 UITableViewController의 다른 서브 클래스를 사용할 수 없습니다

  • 뷰를 서브 클래 싱 된 UIViewController에 사용자가 당연하다고 생각하면 나는 오류가 발생할 것 같은데, 이는 출연 한 후 UIAlertView가 와서
  • 로그인

질문 요약 :.
내가 조건부 특정의 UIView (컨트롤러)들과의 UIViewController의 서브 클래스를 액세스하지 못하도록하는 방법을 알고 싶습니다 , 그 UIAlertView가 발생합니다.

업데이트 1
카테고리 및/또는 프로토콜이 실행 가능한 솔루션 일 수 있습니까?

업데이트 2
CodaFi는 사용자의 상태를 관리 할 수있는 좋은 솔루션으로 싱글을 지적했다.

구현 된 이제 사용자의 액세스를 제어하는 ​​방법을 알아야합니다.
storyboards를 사용할 때 UIStoryboardSegue를 서브 클래 싱하는 가장 다양한 구현 방법과 perform 메서드에서 사용자가 제한된 UIViewController에 액세스하려고 시도하는지 확인합니다 (제한된 컨트롤러에 필수 상태를 지정하는 프로토콜 속성이있을 수 있습니다 : logged 입/출력). 그러나 여기에있는 함정은 스토리 보드 그래픽 편집기에서 UIStoryboardSegue의 클래스/하위 클래스를 선택할 수 없다는 것입니다. 나는 프로그램 적으로 할 수 있다는 것을 알고 있지만 IBActions를 추가해야하고 지루한 부분을 수행하는 메소드와 같이 지루한 것처럼 보일뿐 아니라 navigationController 및 tabbarControllers와 같은 방식으로 작동하지 않을 것이라고 생각합니다.

누구나 사용자의 탐색을 제한하는 실행 가능한 해결책이 있습니까? 내가 작성한 답변 탐색 모음 컨트롤러 사이의 계정 segues 고려하지 않기 때문에

업데이트 3
나는이 질문에 대한 답을 추가 한, 그러나 나는 아직도 답으로 간주. 그러나 그것은 어떤 사람들을 도울 수 있습니다.

+0

싱글 톤이 작동 할 수 있습니까? 응용 프로그램에서 기본값을 관리하는 일종의 게이트 키퍼 단일 클래스입니다. – CodaFi

+0

@CodaFi는 UITabViewController 및 UINavigationController 대표를 사용하여 플러스라고 말했습니다. – Till

+0

WikiResearch를 한 후, 싱글 톤이란 말을 듣지 못했습니다. 어떤 종류의 상태 유지 객체를 의미합니까? 또한 스토리 보드를 사용하고 있습니다. UIStoryboardSegue를 확장하면 솔루션이 될 수 있습니까? – MrJD

답변

2

그래서 저는 맞춤 설정 부분을 사용하여이를 수행하는 방법에 답했습니다.

이제 실제 문제는 tabbarcontrollerdelegate 프로토콜을 사용하여 정신적으로 끊어 졌다는 것입니다. 내가 탭 표시 줄 컨트롤러에게 말했다 처리하는 클래스를 만들었 탭에 대한 액세스를 방지하는 솔루션으로

#import <Foundation/Foundation.h> 

@interface TabBarDelegate : NSObject<UITabBarControllerDelegate,UIAlertViewDelegate>{ 
    UITabBarController *cachedTabBarController; 
} 

@end 

#import "TabBarDelegate.h" 

@implementation TabBarDelegate 

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ 
    NSLog(@"%s",__FUNCTION__); 
    NSLog(@"Pretending the user isnt logged in"); 

    if(true){ 
     NSString *message = [NSString stringWithFormat:@"You require an account to access %@",viewController.tabBarItem.title]; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Account Required" message:message delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: @"Login",@"Create Account",nil]; 
     [alert show]; 
     //Hold tabbarcontroller property for alert callback 
     cachedTabBarController = tabBarController; 
     return false; 
    } 

    return true; 
} 

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    if(cachedTabBarController){ 
     switch (buttonIndex) { 
      case 1: 
       //Login 
       [cachedTabBarController performSegueWithIdentifier:@"tabBarToLogin" sender:cachedTabBarController]; 
       break; 
      case 2: 
       //Sign up 
       [cachedTabBarController performSegueWithIdentifier:@"tabBarToSignup" sender:cachedTabBarController]; 
       break; 
      default: 
       break; 
     } 
     //Release tab bar controller from memory 
     cachedTabBarController = nil; 
    } 
} 

@end 
다음

내가 아래 applicationDidFinishLaunching 내 AppDelegate에에 최대 유선 ...

//Hook up tab bar delegate 
    mainTabController = (UITabBarController*)self.window.rootViewController; 
    mainTabBarDelegate = [[TabBarDelegate alloc] init]; 
    mainTabController.delegate = mainTabBarDelegate; 

그리고 Voila

0

좋아, 그래서 나는 해결책의 일부를 가지고있다.

사용자 지정 단음을 선택할 수있는 상황에서만 작동합니다 (모달이 아닌 푸시하기위한 코드 만 작성했습니다).

인증이 필요한 모든 컨트롤러에 대한 프로토콜 "UIViewControllerAuthentication"이 프로토콜에는 필요한 인증 상태를 검색하는 방법이 포함되어 있습니다. 인증이 필요

enum authenticationStatus { 
notLoggedIn = 0, 
noPassword = 1, 
loggedIn = 2 
}; 

@protocol UIViewControllerAuthentication <NSObject> 

-(enum authenticationStatus)authStatusRequired; 

@end 

컨트롤러는이 프로토콜을 준수 :

-(enum authenticationStatus)authStatusRequired{ 
return loggedIn; 
} 

그런 다음

@interface SeguePushWithAuth : UIStoryboardSegue 

@end 
-(void)perform{ 
    NSLog(@"custom segue destination : %@",self.destinationViewController); 
    NSLog(@"custom segue source : %@",self.sourceViewController); 
    NSLog(@"custom segue dest conforms to protocol : %i",[self.destinationViewController conformsToProtocol:@protocol(UIViewControllerAuthentication)]); 

    if([self.destinationViewController conformsToProtocol:@protocol(UIViewControllerAuthentication)]){ 
     UIViewController <UIViewControllerAuthentication> *destination = (UIViewController <UIViewControllerAuthentication> *)self.destinationViewController; 
     if (!((int)[AccountModel userAuthStatus] >= (int)[destination authStatusRequired])) { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication" message:@"You need an account to access this area" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login",@"Create Account", nil]; 
      //alert 
      [alert show]; 
      return; 
     } 
    } 

    [[self.sourceViewController navigationController] pushViewController:self.destinationViewController animated:true]; 
} 

@end 

그런 다음 사용하는 사용자 정의가

이다 uistoryboard에 segues 인증 된 푸시 SEGUE이를 사용 목적지 컨트롤러가 segue를 취소 할 수있게하는 좋은 패턴.
그러나 탭볼 컨트롤러를 자녀에게 연결하는 하위 태그를 인증 할 수 있기를 원하는만큼 멀리 떨어져 있습니다.

관련 문제