Convert swift to objective C

Could someone convert this to objective c
Getting date of Provisional certificate

Code Block language
func getProvisioningProfileExpirationDate() -> Date?
{
self.getCertificateExpirationDate()
let profilePath: String? = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision")
if( profilePath != nil )
{
let plistData = NSData(contentsOfFile: profilePath!)
let plistDataString = String(format: "%@", plistData!)
var plistString: String = extractPlist(fromMobileProvisionDataString:plistDataString)
let pattern = "<key>ExpirationDate</key>.*<date>(.*)</date>"
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let textCheckingResult : NSTextCheckingResult = regex.firstMatch(in: plistString, options: NSRegularExpression.MatchingOptions(rawValue: UInt(0)), range: NSMakeRange(0, plistString.characters.count))!
let matchRange : NSRange = textCheckingResult.range(at: 1)
let expirationDateString : String = (plistString as NSString).substring(with: matchRange)
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
print( "Profile expires: \(dateFormatter.date(from: expirationDateString)!)" )
return dateFormatter.date(from: expirationDateString)!
}
return nil
}
func extractPlist( fromMobileProvisionDataString:String ) -> String
{
var range = Range(NSMakeRange(0, 1), in: fromMobileProvisionDataString)
var plistDataString = fromMobileProvisionDataString.replacingCharacters(in:range!, with: "")
range = Range(NSMakeRange(plistDataString.count-1, 1), in: plistDataString)
plistDataString.replaceSubrange(range!, with: "")
plistDataString = plistDataString.replacingOccurrences(of: " ", with: "")
let profileText = hexStringtoAscii( plistDataString )
let profileWords = profileText.components(separatedBy: CharacterSet.newlines)
var plistString = "";
var inPlist = false;
for word in profileWords
{
if( word.contains("<plist") ) { inPlist = true }
if( inPlist ) { plistString.append(" "); plistString.append( word ) }
if (word.contains("</plist")) { inPlist = false }
}
return plistString;
}
func hexStringtoAscii(_ hexString : String) -> String {
let pattern = "(0x)?([0-9a-f]{2})"
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let nsString = hexString as NSString
let matches = regex.matches(in: hexString, options: [], range: NSMakeRange(0, nsString.length))
let characters = matches.map {
Character(UnicodeScalar(UInt32(nsString.substring(with: $0.range(at: 2)), radix: 16)!)!)
}
return String(characters)
}
code-block


Answered by DTS Engineer in 626446022

I want to provide notification to user to update or force update app before provision certificate get expired

How are you distributing this app? Most distribution channels automatically take care of this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
You don’t want this code in your project. A provisioning profile has a well-defined format, namely, it’s a property list that’s been signed using CMS. This code is ignores that formatting and grovels through the bytes of the profile looking for things it thinks are relevant. And it’s not even doing that correctly. For example, its string-to-date parsing doesn’t follow the rules outlined in QA1480 NSDateFormatter and Internet Dates.

I would not add this code to my project, or attempt a direct port of it from Swift to Objective-C.

A better approach here would be write or acquire a CMS library that can undo the CMS wrapper. You can then feed the extracted signed content into NSPropertyListSerialization and find the value you want from there.

ps I assume you’re working on iOS. If you’re working on macOS then you can use CMSDecoder for the CMS decoding, which makes this task very easy.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thank you for your reply, Actually i was looking for the issue of not getting notification before app provisional certificate get expired i have also posted a question regarding this but i didn't get any response in this forum can any one help me with the issue i can explain here

I want to provide notification to user to update or force update app before provision certificate get expired any one can help me with this issue will be a great help thank you
Accepted Answer

I want to provide notification to user to update or force update app before provision certificate get expired

How are you distributing this app? Most distribution channels automatically take care of this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thank you for your reply,

We have different team to distribute app and can you help me like how can they help us in providing notification?

Thanks
I’m presuming this is an iOS app. What distribution model are you using? Via the App Store? Or In-House (Enterprise) distribution? Or something else?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
We have an iOS app and we are distributing via secure hub in house (Enterprise) distribution model
OK, in that case you could avoid this problem by adding a property to your app’s Info.plist that holds the profile’s expiry date. That is, when you go to sign your app for enterprise distribution:
  1. Get the profile that you want to sign with.

  2. Use Mac tools to extract the expiry date.

  3. Set that expiry date in an Info.plist property on the app.

  4. Embed the profile in the app.

  5. Sign the app with your enterprise distribution signing identity.

This works because the last step is to sign the app, so the code signature seals in the Info.plist change you made in step 3.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Convert swift to objective C
 
 
Q