Skip to content

Instantly share code, notes, and snippets.

@zhangkn
Last active August 6, 2022 14:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangkn/5f8a0454ef2c0d252002fa9a5956706d to your computer and use it in GitHub Desktop.
Save zhangkn/5f8a0454ef2c0d252002fa9a5956706d to your computer and use it in GitHub Desktop.
typedef struct __CFUserNotification *CFUserNotificationRef;
FOUNDATION_EXTERN CFUserNotificationRef CFUserNotificationCreate ( CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary );
FOUNDATION_EXTERN SInt32 CFUserNotificationReceiveResponse ( CFUserNotificationRef userNotification, CFTimeInterval timeout, CFOptionFlags *responseFlags );
FOUNDATION_EXTERN CFDictionaryRef CFUserNotificationGetResponseDictionary ( CFUserNotificationRef userNotification );
//官方使用例子
SInt32 CFUserNotificationDisplayNotice(CFTimeInterval timeout, CFOptionFlags flags, CFURLRef iconURL, CFURLRef soundURL, CFURLRef localizationURL, CFStringRef alertHeader, CFStringRef alertMessage, CFStringRef defaultButtonTitle) {
CHECK_FOR_FORK();
CFUserNotificationRef userNotification;
SInt32 retval = ERR_SUCCESS;
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (iconURL) CFDictionaryAddValue(dict, kCFUserNotificationIconURLKey, iconURL);
if (soundURL) CFDictionaryAddValue(dict, kCFUserNotificationSoundURLKey, soundURL);
if (localizationURL) CFDictionaryAddValue(dict, kCFUserNotificationLocalizationURLKey, localizationURL);
if (alertHeader) CFDictionaryAddValue(dict, kCFUserNotificationAlertHeaderKey, alertHeader);
if (alertMessage) CFDictionaryAddValue(dict, kCFUserNotificationAlertMessageKey, alertMessage);
if (defaultButtonTitle) CFDictionaryAddValue(dict, kCFUserNotificationDefaultButtonTitleKey, defaultButtonTitle);
userNotification = CFUserNotificationCreate(kCFAllocatorSystemDefault, timeout, flags, &retval, dict);
if (userNotification) CFRelease(userNotification);
CFRelease(dict);
return retval;
}
//例子0: https://github.com/naota/diskdev_cmds/blob/73f17c3b6543b6224c0958a649783809a833dace/nofs.tproj/ui.c
noteRef = CFUserNotificationCreate(NULL, 60,
kCFUserNotificationCautionAlertLevel,
&error,
noteDict);
// CFUserNotificationCreate 的使用例子
//例子一
CFUserNotificationRef notif = CFUserNotificationCreate(kCFAllocatorDefault, 0, 0, NULL, (__bridge CFDictionaryRef)parameters);
//例子二
CFUserNotificationRef userNotification = CFUserNotificationCreate(
NULL, //Default Allocater
0, //No timeout
kCFUserNotificationPlainAlertLevel, //Normal alert
NULL, //We don't need the response
CFDictionaryCreate(0, keys, values, sizeof(keys)/sizeof(*keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)
);
CFUserNotificationReceiveResponse(userNotification, 0, NULL);
CFStringRef resultString = CFUserNotificationGetResponseValue(userNotification, kCFUserNotificationTextFieldValuesKey, 0);
CFStringGetCString(resultString, result, maxChars, kCFStringEncodingUTF8);
//例子三
tempDict = CFDictionaryCreate(NULL, (const void **)keys, (const void **)values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
_userNotification = CFUserNotificationCreate(NULL, 1000, kCFUserNotificationPlainAlertLevel, NULL, tempDict);
CFOptionFlags responseFlags = CFUserNotificationPopUpSelection(0);
int x = CFUserNotificationReceiveResponse(_userNotification, 1000, &responseFlags);
CFDictionaryRef response = CFUserNotificationGetResponseDictionary(_userNotification);
NSLog(@"response: %@", response);
// 例子四
CFDictionaryRef dict = CFDictionaryCreate(0, keys, vals,
sizeof(keys)/sizeof(*keys),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
pDlg = CFUserNotificationCreate(kCFAllocatorDefault, 0,
kCFUserNotificationPlainAlertLevel,
&nRes, dict);
//例子5
m_dialog = CFUserNotificationCreate(
kCFAllocatorDefault,
0,
kCFUserNotificationPlainAlertLevel,
&m_result,
dictionary.reference()
);
//例子6 ios-ssh-askpass https://github.com/rephorm/ios-ssh-askpass/blob/8ee6a9a835e231e8b2ea4c5f48dbf2b402b6ab48/main.mm
int main(int argc, char **argv, char **envp) {
int title_allocated = 0;
const void *keys[] = {kCFUserNotificationAlertHeaderKey,
kCFUserNotificationDefaultButtonTitleKey,
kCFUserNotificationAlternateButtonTitleKey,
kCFUserNotificationTextFieldTitlesKey
};
const void *values[] = {CFSTR("Please enter your ssh key passphrase"), // default title get's replaced below
CFSTR("Ok"),
CFSTR("Cancel"),
CFSTR("Passphrase"),
};
if (argc > 1) {
values[0] = CFStringCreateWithCString(NULL, argv[1], kCFStringEncodingUTF8);
title_allocated = 1;
}
CFDictionaryRef params = CFDictionaryCreate(0, keys, values, sizeof(keys) / sizeof(*keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
SInt32 err = 0;
CFUserNotificationRef ref;
ref = CFUserNotificationCreate(kCFAllocatorDefault, 0,
kCFUserNotificationPlainAlertLevel |
//kCFUserNotificationNoDefaultButtonFlag |
CFUserNotificationSecureTextField(0),
&err,
params);
CFOptionFlags response;
CFUserNotificationReceiveResponse(ref, 0, &response);
if ((response & 0x3) == kCFUserNotificationDefaultResponse) {
CFStringRef str = CFUserNotificationGetResponseValue(ref, kCFUserNotificationTextFieldValuesKey, 0);
if (title_allocated) CFRelease(values[0]);
puts([(NSString *)str UTF8String]);
return 0;
} else {
return -1;
}
}
// vim:ft=objc
//例子7https://github.com/lanayotech/vagrant-manager/blob/e5a129ed78d131731d4a5bcf94c12c822f37d433/Vagrant%20Manager/PasswordHelper.m
CFUserNotificationRef passwordDialog;
NSDictionary *panelDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Enter Your Password", kCFUserNotificationAlertHeaderKey,
@"Vagrant Requested Administrator Privileges", kCFUserNotificationAlertMessageKey,
@"", kCFUserNotificationTextFieldTitlesKey,
@"Cancel", kCFUserNotificationAlternateButtonTitleKey,
(__bridge CFURLRef)iconUrl, kCFUserNotificationIconURLKey,
nil];
passwordDialog = CFUserNotificationCreate(kCFAllocatorDefault,
0,
kCFUserNotificationPlainAlertLevel
|
CFUserNotificationSecureTextField(0),
&error,
(__bridge CFDictionaryRef)panelDict);
//例子8 https://github.com/ioshomebrew/libcydianotifications/blob/294a4f4d3a20a5101c8af592d5236daf136918ca/libcydianotifications/Notifications.m
-(void) showAlert {
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
[dict setObject: @"Alert!" forKey: (__bridge NSString*)kCFUserNotificationAlertHeaderKey];
[dict setObject: @"Updates Ready!" forKey: (__bridge NSString*)kCFUserNotificationAlertMessageKey];
[dict setObject: @"View" forKey:(__bridge NSString*)kCFUserNotificationDefaultButtonTitleKey];
[dict setObject: @"Cancel" forKey:(__bridge NSString*)kCFUserNotificationAlternateButtonTitleKey];
SInt32 error = 0;
CFUserNotificationRef alert =
CFUserNotificationCreate(NULL, 0, kCFUserNotificationPlainAlertLevel, &error, (__bridge CFDictionaryRef)dict);
CFOptionFlags response;
// we block, waiting for a response, for up to 10 seconds
if((error) || (CFUserNotificationReceiveResponse(alert, 10, &response))) {
NSLog(@"alert error or no user response after 10 seconds");
} else if((response & 0x3) == kCFUserNotificationAlternateResponse) {
// user clicked on Cancel ... just do nothing
NSLog(@"cancel");
} else if((response & 0x3) == kCFUserNotificationDefaultResponse) {
// user clicked on View ... so, open the UI App
NSLog(@"view");
[self openApp];
}
CFRelease(alert);
}
//例子9
void NS_INLINE dt_warn_permissions(void) {
CFDictionaryRef alert_flags = (__bridge CFDictionaryRef)@{
(id)kCFUserNotificationAlertHeaderKey: NSLocalizedString(@"DeltaService is not powerful enough to complete your request.", @"no tl please"),
(id)kCFUserNotificationAlertMessageKey: NSLocalizedString(@"Elevating to root is not supported yet. Please update manually.", @"no tl please")};
CFUserNotificationRef warning = NULL;
warning = CFUserNotificationCreate(kCFAllocatorDefault, 0, kCFUserNotificationCautionAlertLevel, NULL, alert_flags);
CFRelease(warning);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment