Получить дату истечения срока действия профиля подготовки во время выполнения?

У меня есть приложение, которое я регулярно раздаю тестировщикам с помощью специального метода распространения. Некоторые из этих тестировщиков «в теме» и достаточно знают о профилях инициализации и ежеквартальных сроках действия и могут (если я забуду) подтолкнуть меня к созданию новой версии для тестирования.

Однако некоторые пользователи, кажется, всегда доходят до того, что он перестает работать, а затем жалуются и стонут по этому поводу — несмотря на то, что они, вероятно, отклоняют напоминание об уровне iOS.

Мой вопрос: могу ли я программно получить дату истечения срока действия во время выполнения и сделать свои собственные оповещения «в приложении» или системные уведомления, чтобы напомнить им о необходимости установить более новую версию?


person iOSProgrammingIsFun    schedule 23.09.2013    source источник


Ответы (5)


Быстрая версия:

// Returns `nil` if it fails
private func getProvisioningProfileExpirationDateAsString() -> String? {
    guard
        let profilePath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision"),
        let profileData = try? Data(contentsOf: URL(fileURLWithPath: profilePath)),
        // Note: We use `NSString` instead of `String`, because it makes it easier working with regex, ranges, substring etc.
        let profileNSString = NSString(data: profileData, encoding: String.Encoding.ascii.rawValue)
        else {
        print("WARNING: Could not find or read `embedded.mobileprovision`. If running on Simulator, there are no provisioning profiles.")
        return nil
    }


    // NOTE: We have the `[\\W]*?` check to make sure that variations in number of tabs or new lines in the future does not influence the result.
    guard let regex = try? NSRegularExpression(pattern: "<key>ExpirationDate</key>[\\W]*?<date>(.*?)</date>", options: []) else {
        print("Warning: Could not create regex.")
        return nil
    }

    let regExMatches = regex.matches(in: profileNSString as String, options: [], range: NSRange(location: 0, length: profileNSString.length))

    // NOTE: range `0` corresponds to the full regex match, so to get the first capture group, we use range `1`
    guard let rangeOfCapturedGroupForDate = regExMatches.first?.range(at: 1) else {
        print("Warning: Could not find regex match or capture group.")
        return nil
    }

    let dateAsString = profileNSString.substring(with: rangeOfCapturedGroupForDate)
    return dateAsString
}
person Nikolay Suvandzhiev    schedule 20.09.2018

Вы ищете что-то вроде

"<key>ExpirationDate</key><date>2014-12-06T00:26:10Z</date>" in [[NSBundle mainBundle] pathForResource:@"embedded" ofType:@"mobileprovision"]

Но попасть туда не просто! Этот код можно улучшить, его части основаны на других сообщениях stackoverflow. Примечание. Другим вариантом может быть загрузка всего, что находится между item plist и /plist, в ... plist (словарь). Но так как мы уже там, мы просто находим родного брата вручную.

- (NSString*) getExpiry{

    NSString *profilePath = [[NSBundle mainBundle] pathForResource:@"embedded" ofType:@"mobileprovision"];
    // Check provisioning profile existence
    if (profilePath)
    {
        // Get hex representation
        NSData *profileData = [NSData dataWithContentsOfFile:profilePath];
        NSString *profileString = [NSString stringWithFormat:@"%@", profileData];

        // Remove brackets at beginning and end
        profileString = [profileString stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
        profileString = [profileString stringByReplacingCharactersInRange:NSMakeRange(profileString.length - 1, 1) withString:@""];

        // Remove spaces
        profileString = [profileString stringByReplacingOccurrencesOfString:@" " withString:@""];


        // Convert hex values to readable characters
        NSMutableString *profileText = [NSMutableString new];
        for (int i = 0; i < profileString.length; i += 2)
        {
            NSString *hexChar = [profileString substringWithRange:NSMakeRange(i, 2)];
            int value = 0;
            sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
            [profileText appendFormat:@"%c", (char)value];
        }

        // Remove whitespaces and new lines characters
        NSArray *profileWords = [profileText componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        //There must be a better word to search through this as a structure! Need 'date' sibling to <key>ExpirationDate</key>, or use regex
        BOOL sibling = false;
        for (NSString* word in profileWords){
            if ([word isEqualToString:@"<key>ExpirationDate</key>"]){
                NSLog(@"Got to the key, now need the date!");
                sibling = true;
            }
            if (sibling && ([word rangeOfString:@"<date>"].location != NSNotFound)) {
                NSLog(@"Found it, you win!");
                NSLog(@"Expires: %@",word);
                return word;
            }
        }

    }

    return @"";
}
person Pierre-Francoys Brousseau    schedule 08.01.2014
comment
Он возвращает ноль для profilePath в самой первой строке. Я использую xcode 8.2.1 и использую этот код Objective-C с быстрым. - person Skywalker; 09.03.2018
comment
Вы запускаете код в симуляторе? Он не подписан, поэтому при запуске в симуляторе не будет файла embedded.mobileprovision. Попробуйте запустить код на физическом устройстве (код должен быть подписан, и файл embedded.mobileprovision теоретически должен быть там. - person wottle; 10.03.2018

В iOS 13 это не работает. Описание NSData меняется с

{0x30822755 06092a86 4886f70d 010702a0 ... 889bfcbe ed768bac }

to

{length = ..., bytes = 0x30822755 06092a86 4886f70d 010702a0 ... 889bfcbe ed768bac }

Попробуйте этот код:

NSData *data = ....;
NSUInteger dataLength = [data length];
NSMutableString *string =[NSMutableString stringWithCapacity:dataLength*2];

const unsigned char *dataBytes = [data bytes];
for (NSInteger idx = 0; idx < dataLength; ++idx)
{
    [string appendFormat:@"%02x", dataBytes[idx]];
}
person Ttnka    schedule 27.11.2019

В iOS 13, чтобы упростить предложение Ttnka, вы можете использовать profileData.debugDescription для создания profileString (из изначально опубликованного кода Objective-C):

NSString *profileString = [NSString stringWithFormat:@"%@", profileData.debugDescription];

Ссылка: https://developer.apple.com/forums/thread/119111

person Julius G    schedule 27.01.2021

Объединение ответов @Pierre-Francoys Brousseau и @Ttnka:

- (NSString *) getAppExpiry
{

    NSString *profilePath = [[NSBundle mainBundle] pathForResource:@"embedded" ofType:@"mobileprovision"];
    // Check provisioning profile existence
    if (profilePath)
    {
        // Get hex representation
        NSData *profileData = [NSData dataWithContentsOfFile:profilePath];
        NSString *profileString;
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13.0)
        {
            NSUInteger dataLength = [profileData length];
            NSMutableString *string =[NSMutableString stringWithCapacity:dataLength*2];

            const unsigned char *dataBytes = [profileData bytes];
            for (NSInteger idx = 0; idx < dataLength; ++idx)
            {
                [string appendFormat:@"%02x", dataBytes[idx]];
            }
            
            profileString = string;

        }
        else
        {
            profileString = [NSString stringWithFormat:@"%@", profileData];
            
            // Remove brackets at beginning and end
            profileString = [profileString stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
            profileString = [profileString stringByReplacingCharactersInRange:NSMakeRange(profileString.length - 1, 1) withString:@""];

            // Remove spaces
            profileString = [profileString stringByReplacingOccurrencesOfString:@" " withString:@""];
        }

        // Convert hex values to readable characters
        NSMutableString *profileText = [NSMutableString new];
        for (int i = 0; i < profileString.length; i += 2)
        {
            NSString *hexChar = [profileString substringWithRange:NSMakeRange(i, 2)];
            int value = 0;
            sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
            [profileText appendFormat:@"%c", (char)value];
        }
        
        // Remove whitespaces and newline characters
        NSArray *profileWords = [profileText componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        // There must be a better word to search through this as a structure! Need 'date' sibling to <key>ExpirationDate</key>
        BOOL sibling = false;
        for (NSString* word in profileWords){
            if ([word isEqualToString:@"<key>ExpirationDate</key>"]){
                NSLog(@"Got to the key, now need the date!");
                sibling = true;
            }
            if (sibling && ([word rangeOfString:@"<date>"].location != NSNotFound)) {
                NSLog(@"Found it, you win!");
                NSLog(@"Expires: %@", word);
                return word;
            }
        }
    }
    else
        NSLog(@"profile path is nil");
    
    NSLog(@"Could not find app expiry date");
    
    return @"";
}
person ghr    schedule 06.07.2021