아이폰 Xcode Swift 환경에서 앱 업데이트 필요시, 팝업을 표시하고 앱스토어로 이동하기

 

 

 

 

 

 

 

 

 

 

 

    let myVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

    var latestVersion = "1.1.0"​
    var isVersionPopShow : Bool = false

    func updateAppHandler(alert: UIAlertAction){ //  앱 업데이트

        let myAppId = "1234567890"  //   앱 아이디

        if let url = URL(string: "itms-apps://itunes.apple.com/app/id\(myAppId)"), UIApplication.shared.canOpenURL(url) { // 유효한 URL인지 검사합니다. 에뮬은 안됨.

            if #available(iOS 10.0, *) { //iOS 10.0부터 URL를 오픈하는 방법이 변경 되었습니다.

                UIApplication.shared.open(url, options: [:], completionHandler: nil)

            } else {

                UIApplication.shared.openURL(url)

            }

        }

    }​

​

func compareVersion(){

        let myLatestVersion = latestVersion.replacingOccurrences(of: ".", with: "")

        if let myInstallVersion = myVersion?.replacingOccurrences(of: ".", with: ""){

            print("@@@ 버전 비교, latest=\(myLatestVersion), my=\(myInstallVersion)")

            if myLatestVersion > myInstallVersion {

                if isVersionPopShow {

                    return

                }

                print("@@@  앱 업데이트 필요")

                let message = "최신버전의 앱이 있습니다. 앱을 업데이트하시겠습니까?"

                let alertController = UIAlertController(title: "땡땡땡앱", message: message, preferredStyle: .alert)

                let yes = UIAlertAction(title: "네", style: .default, handler: updateAppHandler)

                let no = UIAlertAction(title: "취소", style: .default, handler: nil)

                alertController.addAction(no)

                alertController.addAction(yes)

                present(alertController, animated: true, completion: nil)

                isVersionPopShow = true;

            }

        }

    }​

 

 

안드로이드 환경에서 앱 업데이트 필요시, 팝업을 표시하고 구글 플레이 스트어로 이동하기

 

   try{
       String myVer = BuildConfig.VERSION_NAME;
       String lastVersion = "1.1.0";
       int nLastVer = Integer.parseInt(lastVersion.replace(".",""));
       int nCurVer = Integer.parseInt(myVer.replace(".",""));
       if((nLastVer > nCurVer) && mustYn.equals("Y")){ // 최신버전이 있고, 설치요청팝업이 보여야하면​
	   AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
	   // 여기서 부터는 알림창의 속성 설정
	   builder.setTitle("땡땡땡앱")        // 제목 설정
		   .setMessage("최신 버전의 땡땡땡앱을 설치하시겠습니까?")        // 메세지 설정
		   .setCancelable(true)        // 뒤로 버튼 클릭시 취소 가능 설정
		   .setNegativeButton("취소", new DialogInterface.OnClickListener(){	// 취소 버튼 클릭시 설정
		       public void onClick(DialogInterface dialog, int whichButton){
			   dialog.cancel();
			   ISUPDATECANCEL = true;
		       }
		   })
		   .setPositiveButton("확인", new DialogInterface.OnClickListener(){       // 확인 버튼 클릭시 설정
		       public void onClick(DialogInterface dialog, int whichButton){
			   // https://m.blog.naver.com/chumy/220850047176
			   ISUPDATECANCEL = true;
			   final String appPackageName = getPackageName();
			   Intent marketLaunch = new Intent(Intent.ACTION_VIEW);
			   marketLaunch.setData(Uri.parse("market://details?id="+appPackageName));
			   try{
			       startActivity(marketLaunch);
			   }catch (android.content.ActivityNotFoundException e){
			       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
			   }
		       }
		   });
	   AlertDialog dialog = builder.create();    // 알림창 객체 생성
	   dialog.show();    // 알림창 띄우기​
       }
   }catch (Exception e){
       Log.e(TAG,e.getMessage());
   }​

+ Recent posts