forked from DanTheMan827/ios-app-signer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProvisioningProfile.swift
98 lines (89 loc) · 4.28 KB
/
ProvisioningProfile.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// provisioningProfile.swift
// AppSigner
//
// Created by Daniel Radtke on 11/4/15.
// Copyright © 2015 Daniel Radtke. All rights reserved.
//
import Foundation
import AppKit
struct ProvisioningProfile {
var filename: String,
name: String,
created:NSDate,
expires: NSDate,
appID: String,
teamID: String,
rawXML: String,
entitlements: AnyObject?
private let delegate = NSApplication.sharedApplication().delegate as! AppDelegate
static func getProfiles() -> [ProvisioningProfile] {
var output: [ProvisioningProfile] = []
let fileManager = NSFileManager()
if let libraryDirectory = fileManager.URLsForDirectory(.LibraryDirectory, inDomains: .UserDomainMask).first,
libraryPath = libraryDirectory.path {
let provisioningProfilesPath = libraryPath.stringByAppendingPathComponent("MobileDevice/Provisioning Profiles") as NSString
if let provisioningProfiles = try? fileManager.contentsOfDirectoryAtPath(provisioningProfilesPath as String) {
for provFile in provisioningProfiles {
if provFile.pathExtension == "mobileprovision" {
let profileFilename = provisioningProfilesPath.stringByAppendingPathComponent(provFile)
if let profile = ProvisioningProfile(filename: profileFilename) {
output.append(profile)
}
}
}
}
}
return output;
}
init?(filename: String){
let securityArgs = ["cms","-D","-i", filename]
let taskOutput = NSTask().execute("/usr/bin/security", workingDirectory: nil, arguments: securityArgs)
if taskOutput.status == 0 {
self.rawXML = taskOutput.output
if let results = try? NSPropertyListSerialization.propertyListWithData(taskOutput.output.dataUsingEncoding(NSUTF8StringEncoding)!, options: .Immutable, format: nil) {
if let expirationDate = results.valueForKey("ExpirationDate") as? NSDate,
creationDate = results.valueForKey("CreationDate") as? NSDate,
name = results.valueForKey("Name") as? String,
entitlements = results.valueForKey("Entitlements"),
applicationIdentifier = entitlements.valueForKey("application-identifier") as? String,
periodIndex = applicationIdentifier.characters.indexOf(".") {
self.filename = filename
self.expires = expirationDate
self.created = creationDate
self.appID = applicationIdentifier.substringFromIndex(periodIndex.advancedBy(1))
self.teamID = applicationIdentifier.substringToIndex(periodIndex)
self.name = name
self.entitlements = entitlements
} else {
Log.write("Error processing \(filename.lastPathComponent)")
return nil
}
} else {
Log.write("Error parsing \(filename.lastPathComponent)")
return nil
}
} else {
Log.write("Error reading \(filename.lastPathComponent)")
return nil
}
}
func getEntitlementsPlist(tempFolder: String) -> NSString? {
let mobileProvisionPlist = tempFolder.stringByAppendingPathComponent("mobileprovision.plist")
do {
try self.rawXML.writeToFile(mobileProvisionPlist, atomically: false, encoding: NSUTF8StringEncoding)
let plistBuddy = NSTask().execute("/usr/libexec/PlistBuddy", workingDirectory: nil, arguments: ["-c", "Print :Entitlements",mobileProvisionPlist, "-x"])
if plistBuddy.status == 0 {
return plistBuddy.output
} else {
Log.write("PlistBuddy Failed")
Log.write(plistBuddy.output)
return nil
}
} catch let error as NSError {
Log.write("Error writing mobileprovision.plist")
Log.write(error.localizedDescription)
return nil
}
}
}