// // KMTools_OC.m // PDF Master // // Created by tangchao on 2023/3/27. // #import "KMTools_OC.h" #import @implementation KMTools_OC + (BOOL)isDefaultRoleHandlerForContentType:(NSString*)fileExtension { NSString *app = (NSString*)CFBridgingRelease(LSCopyDefaultRoleHandlerForContentType((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]), kLSRolesAll)); if ([app isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) { return YES; } return NO; } + (BOOL)setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension { OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]), kLSRolesAll, (CFStringRef) CFBridgingRetain([[NSBundle mainBundle] bundleIdentifier])); if (returnStatus != 0) { return NO; } return YES; } + (BOOL) setPreviewAsDefaultApplicationForFileExtension:(NSString *) fileExtension { OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]), kLSRolesAll, (CFStringRef)@"com.apple.Preview"); if (returnStatus != 0) { return NO; } return YES; } #pragma mark --SetDefaultRoleHandlerForContentType + (NSString *)UTIforFileExtension:(NSString *) extension { NSString * UTIString = (NSString *)CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)CFBridgingRetain(extension), NULL)); return UTIString; } + (KMAppTarget)getAppTarget { // VERSION_DMG=1 // VERSION_FREE=1 // VERSION_PRO=1 #if VERSION_FREE return KMAppTargetFree; #endif #if VERSION_PRO return KMAppTargetPro; #endif #if VERSION_DMG return KMAppTargetDMG; #endif return KMAppTargetFree; } @end @implementation KMTools_OC (Testing) + (NSData *)getPDFData { NSMutableData *pdfData = [[NSMutableData alloc] init]; CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData); CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("PDF Master")); CGRect mediaBox = CGRectMake(0, 0, 595, 842); CGContextRef pdfContext = CGPDFContextCreate(consumer, &mediaBox, myDictionary); CGContextSetRGBFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0); CGContextSetTextDrawingMode(pdfContext, kCGTextFill); CFRelease(myDictionary); CGContextBeginPage(pdfContext, &mediaBox); CGContextEndPage(pdfContext); CGPDFContextClose(pdfContext); CGContextRelease (pdfContext); CGDataConsumerRelease(consumer); return pdfData; } @end @implementation KMPageSizeTool + (NSString *)conversionWithUnit:(NSString *)unit value:(CGFloat)value { CGFloat f = value; if ([unit isEqualToString:@"cm"]) { f = value/10.0; } else if([unit isEqualToString:@"in"]){ f = value/25.4; } else { f = value; } if (fmodf(f, 1)==0) {//如果有一位小数点 return [NSString stringWithFormat:@"%.0f",f]; } else if (fmodf(value*10, 1)==0) {//如果有两位小数点 return [NSString stringWithFormat:@"%.1f",f]; } else { return [NSString stringWithFormat:@"%.2f",f]; } } @end