iOS:如何检索代码签名标识

时间:2016-11-27 09:08:47

标签: ios code-signing

出于诊断目的,有时希望能够读取正在运行的应用程序的代码签名标识符。

此信息存在于应用包中的embedded.mobileprovision文件中,如下所示:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>AppIDName</key>
    <string>Happy</string>
    <key>ApplicationIdentifierPrefix</key>
    <array>
    <string>7YCRDE64W6</string>
    </array>

可以使用哪些代码段来阅读此信息?

1 个答案:

答案 0 :(得分:0)

这是Objective-C代码,可以为我们完成这项工作:

-(NSString*_Nullable)codeSignId
{
    NSString *provisPath = [[NSBundle mainBundle] pathForResource:@"embedded.mobileprovision" ofType:nil];
    NSData *provisData = [NSData dataWithContentsOfFile:provisPath];

    NSData *tokApplicationIdentifierPrefix = [@"ApplicationIdentifierPrefix" dataUsingEncoding:NSUTF8StringEncoding];
    NSData *tokStringStart = [@"<string>" dataUsingEncoding:NSUTF8StringEncoding];
    NSData *tokStringEnd = [@"</string>" dataUsingEncoding:NSUTF8StringEncoding];
    #define RangeOK(rng) rng.location != NSNotFound
    NSUInteger len = provisData.length;

    NSRange range1 = [provisData rangeOfData:tokApplicationIdentifierPrefix options:0 range:NSMakeRange(0, len)];
    if (RangeOK(range1)) { //found ApplicationIdentifierPrefix
        NSRange range2 = [provisData rangeOfData:tokStringStart options:0 range:NSMakeRange(range1.location, len-range1.location)];
        if (RangeOK(range2)) { //found <string>
            NSRange range3 = [provisData rangeOfData:tokStringEnd options:0 range:NSMakeRange(range2.location, len-range2.location)];
            if (RangeOK(range3)) { //found </string>
                NSString *appIdPrefix = [[NSString alloc] initWithData:[provisData subdataWithRange:NSMakeRange(range2.location+8, range3.location - range2.location -8)] encoding:NSUTF8StringEncoding];
                return appIdPrefix;
            }
        }
    }
    return nil;
}

修改

我刚刚了解到,有时文件embedded.mobileprovision包含ApplicationIdentifierPrefixTeamIdentifier的不同值。我很乐意了解这两者之间的差异。