iOS通过OC代码获取App中的embedded.mobileprovision信息

·  阅读 2195

在实际开发中一个应用会被修改并重新签名,这个时候可以读取当前应用的签名信息了解过期时间以及唯一标识等信息。
直接上代码:

- (NSString *)readMobileProvision {
	NSString *mobileProvisionPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"embedded.mobileprovision"];
	FILE *fp = fopen([mobileProvisionPath UTF8String], "r");
	char ch;
	if (fp ==NULL) {
		NSLog(@"file cannot be opened 文件无法打开");
		return nil;
	}
	NSMutableString *mstr = [NSMutableString string];
	while ((ch =fgetc(fp))!=EOF) {
		// 读取文件并拼接字符串
		[mstr appendFormat:@"%c",ch];
	}
	fclose(fp);
	
	NSString *teamIdentifier = nil;
	// 获取key值,读取索引位置
	NSRange teamIdentifierRange = [mstr rangeOfString:@"<key>com.apple.developer.team-identifier</key>"];
	
	if (teamIdentifierRange.location != NSNotFound) {
		NSInteger location = teamIdentifierRange.location + teamIdentifierRange.length;

		NSInteger length = [mstr length] - location;
		if (length > 0 && location >= 0) {
			// xxxxx******
			// <key>com.apple.developer.team-identifier</key>
			// <string>BXSLXXXxF</string>
			// xxx****
			// 根据key值截取后续的字符串找到对应的索引位置通过</string>把字符串拆分成数组,取第一个
			NSString *newStr = [mstr substringWithRange:NSMakeRange(location, length)];
			NSArray *val = [newStr componentsSeparatedByString:@"</string>"];
			NSString *v = [val firstObject];
			NSRange startRange = [v rangeOfString:@"<string>"];
			NSInteger newLocation = startRange.location + startRange.length;
			NSInteger newLength = [v length] - newLocation;
			if (newLength > 0 && location >= 0) {
				teamIdentifier = [v substringWithRange:NSMakeRange(newLocation, newLength)];
				NSString *teamIdentifier11 = [v substringFromIndex:newLocation];
				NSLog(@"teamIdentifier11=%@",teamIdentifier11);
			}
		}
	}
	NSLog(@"team-identifier=%@",teamIdentifier);
	return teamIdentifier;
}
复制代码

以上方法是获取签名的描述文件的team-identifier这只是个举例同时还有:

<key>application-identifier</key>
<string>BUxxxxx6XF.*</string>
// 获取bundleID
复制代码
<key>ProvisionedDevices</key>
	<array>
		<string>0000802xxxxx5DC3CB8003A</string>
		<string>085b164ssss847f1f2e9</string>
		<string>99xxxxxxxxxxxxxxxxxxxxx626e72e3b694a9032</string>
		<string>6d27d17891129b5b33cd3a93656c1xxxxxxx18d8</string>
		<string>1e0248daf8bd89dc1edf92639cae9e0xxxxx804e</string>
	</array>
	// 获取设备ID的数组
复制代码
<key>TeamName</key>
<string>wxxxx xxxxx</string>
// 拥有者名字
复制代码
<key>CreationDate</key>
<date>2019-09-28T11:23:04Z</date>
// 创建时间
<key>ExpirationDate</key>
<date>2020-09-27T11:23:04Z</date>
// 过期时间
复制代码

更多信息可以打印方法中的mstr查看全部信息,针对的去获取。 参考链接

分类:
iOS
标签:
分类:
iOS
标签:
收藏成功!
已添加到「」, 点击更改