Check if my IOS application is updated

前端 未结 8 1088
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 18:07

I need to check when my app launches if it was being updated, because i need to make a view that only appears when the app is firstly installed to appear again after being

8条回答
  •  攒了一身酷
    2020-12-05 18:30

    Here is a simple code to know if the current version is different (this code work on simulator too.)

    -(BOOL) needsUpdate
    {
        NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString* appID = infoDictionary[@"CFBundleIdentifier"];
        NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
        NSData* data = [NSData dataWithContentsOfURL:url];
        NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
       if ([lookup[@"resultCount"] integerValue] == 1)
       {
           NSString* appStoreVersion = lookup[@"results"][0][@"version"];
           NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"];
           if (![appStoreVersion isEqualToString:currentVersion])
           {
               NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion);
               return YES;
           }
        }
        return NO;
    }
    

    Note: Make sure that when you enter the new version in iTunes, this matches the version in the app you are releasing. If not then the above code will always return YES regardless if the user updates.

提交回复
热议问题