iOS APP内弹窗推送版本更新信息(实现跳转、强制更新等)

1、一打开APP就检测版本更新信息,则需要AppDelegate.mm里面添加:

    NSString *version = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"id"] = @"123456789";// 你程序的apple ID号
    [mgr POST:@"http://itunes.apple.com/cn/lookup?id=123456789" parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // App_URL http://itunes.apple.com/lookup
        NSArray *array = responseObject[@"results"];
        if (array.count != 0) {// 先判断返回的数据是否为空   没上架的时候是空的
            NSDictionary *dict = array[0];
 
            if ([dict[@"version"] floatValue] > [subVersion floatValue]) {
                //如果有新版本 这里要注意下如果你版本号写得是1.1.1或者1.1.1.1这样的格式,就不能直接转floatValue,自己想办法比较判断。
                UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                alertWindow.rootViewController = [[UIViewController alloc] init];
                alertWindow.windowLevel = UIWindowLevelAlert + 1;
                [alertWindow makeKeyAndVisible];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:@"发现新版本。为保证各项功能正常使用,请您尽快更新。" preferredStyle:UIAlertControllerStyleAlert];
                //显示弹出框
                [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"现在更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id123456789?mt=8"]];
                    //这里写的URL地址是该app在app store里面的下载链接地址,其中ID是该app在app store对应的唯一的ID编号。
                    NSLog(@"点击现在升级按钮,跳转");
                }]];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"点击下次再说按钮");  //如果不add这段Action,则弹窗中只有1个按钮,即强制用户更新
                }]];
                
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    return YES;

2、如果要每次打开某个页面就弹出,可将检查更新代码放在该页面的viewController内。例如有个页面中功能不允许为登录用户操作,则在该viewController中嵌入检查更新代码。

 

以上代码亲测可用,已经使用了好几个版本啦。有些说法是内置版本推送更新会被拒,其实并不会,只要在审核期间保证不会弹出更新窗口就好了。

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
iOS中,推送弹窗通常使用系统提供的`UNUserNotificationCenter`来实现。下面是一个简单的代码示例,用于在前台时展示推送弹窗: ```swift import UserNotifications class ViewController: UIViewController, UNUserNotificationCenterDelegate { override func viewDidLoad() { super.viewDidLoad() // 设置通知中心的代理 UNUserNotificationCenter.current().delegate = self // 请求用户授权 let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in if let error = error { print("Error requesting authorization: \(error.localizedDescription)") } // 用户授权后,可以向通知中心注册推送 if granted { DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } } } // 实现通知中心的代理方法,用于展示推送通知 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { // 在前台时展示推送弹窗 completionHandler([.alert, .badge, .sound]) } } ``` 在代码中,首先需要请求用户授权,然后向通知中心注册推送。在`UNUserNotificationCenterDelegate`协议中,实现`userNotificationCenter(_:willPresent:withCompletionHandler:)`方法,用于在前台时展示推送弹窗。由于这个方法是在后台线程中调用的,因此需要在完成之后调用`completionHandler`来通知系统如何处理推送通知。在这个示例中,我们将推送通知以弹窗的形式展示。 需要注意的是,如果应用在前台时收到推送通知,系统默认不会展示弹窗。因此,我们需要在代码中主动调用`completionHandler`来展示弹窗

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值