2017-05-23 3 views
0

나는 한 번만 팝업해야하는 용어 계약이 있지만 앱이 시작될 때마다 팝업됩니다. 한 번만 팝업하고 한 번 누르면 팝업 할 수 없습니다. 앱을 삭제하고 다시 다운로드하지 않으면 다시 사용 가능합니다. 내가 How can I show a view on the first launch only?한 번만 표시 할 UIAlertView

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     if !UserDefaults.standard.bool(forKey: "Walkthrough") { 
      UserDefaults.standard.set(false, forKey: "Walkthrough") 
     } 
    } 

} 

class FirstViewController: UIViewController, UIAlertViewDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if UserDefaults.standard.bool(forKey: "Walkthrough") { 
      print("already shown") 
      // Terms have been accepted, proceed as normal 
     } else { 
      agree() 
     } 
    } 

} 

기능을 동의의 나 따라 시도하고 동의하면 내가

UserDefaults.standard.set(true, forKey: "Walkthrough") 

답변

0

입니다 또는 "WalkThrough"키가 존재하는지 여부를 확인하십시오. 그렇지 않으면 알 수없는 경우 경고를 표시합니다.

viewDidAppear 방법으로 초기의 ViewController이 추가 :

let alert = UIAlertController(title: "Message", message: "You need to agree to terms and conditions", preferredStyle: .alert) 
    let action = UIAlertAction(title: "Not Agreed", style: .default) { (action) in 

     UserDefaults.standard.set(false, forKey: "WalkThrough") 

     alert.dismiss(animated: true, completion: nil) 
    } 
    let action2 = UIAlertAction(title: "Agreed", style: .default) { (action) in 

     UserDefaults.standard.set(true, forKey: "WalkThrough") 

     alert.dismiss(animated: true, completion: nil) 
    } 

    alert.addAction(action) 
    alert.addAction(action2) 


    if (UserDefaults.standard.object(forKey: "WalkThrough") == nil) { 
     //show alert and save answer 
     self.present(alert, animated: true, completion: nil) 

    } 
+0

추가 정보를 추가 할 귀하의 질문에 편집 링크를 이용해주십시오. 답변 게시 버튼은 질문에 대한 완전한 대답을 위해서만 사용되어야합니다. - [검토 중] (리뷰/저품절 게시물/16202079) – dfd

+1

무엇을 편집할까요? 완전한 대답은 내가 그것을 설정하기 위해 라인을 추가하는 것을 잊었다는 것이었다. 그것은 답이다. – user8000557

0

가 그냥 대답 UserDefaults에 부울 저장) (동의 후 추가하는 것을 잊었다 경고 컨트롤러