這段時(shí)間一直在看Mac開發(fā)相關(guān)的書籍,相對(duì)于iOS來說,市面上寫的比較好的數(shù)據(jù)少之又少变汪,《macOSObjc》算是找到的寫的比較好的了,但是大部分都是介紹控件的使用蚁趁。與之相比的《Learn Objective-C on the Mac》寫的很詳細(xì)裙盾,不過只有英文版的,看了之后受益匪淺他嫡。
下面是看書時(shí)總結(jié)的筆記番官,接下來會(huì)持續(xù)更新。
1钢属、Mac下獲取電腦Application icon徘熔、name、bundle url
NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSSystemDomainMask, YES).firstObject;
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:path] includingPropertiesForKeys:@[NSURLLocalizedNameKey, NSURLIsApplicationKey, NSURLEffectiveIconKey] options:NSDirectoryEnumerationSkipsPackageDescendants | NSDirectoryEnumerationSkipsHiddenFiles errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) {
if(error) {
NSLog(@"error: %@", error.description);
}
return YES;
}];
for(NSURL *applicationURL in enumerator) {
NSImage *applicationImage = nil;
NSError *error;
[applicationURL getResourceValue:&applicationImage forKey:NSURLEffectiveIconKey error:&error];
NSString *applicationName;
[applicationURL getResourceValue:&applicationName forKey:NSURLLocalizedNameKey error:&error];
if(error) {
NSLog(@"error ---> %@", error.description);
return;
}
if(applicationName && applicationImage) {
ApplicationModel *model = [[ApplicationModel alloc] init];
model.image = applicationImage;
model.name = applicationName;
model.url = applicationURL;
[self.dataArray addObject:model];
}
}
NSLog(@"array - > %@", self.dataArray);
2淆党、Mac下打開應(yīng)用程序的代碼
[[NSWorkspace sharedWorkspace] openURL:url];
3近顷、獲取Mac下所有在運(yùn)行的app的代碼
@property (nonatomic, strong) NSMutableArray *dataArr;
NSArray <NSRunningApplication *> *apps = [[NSWorkspace sharedWorkspace] runningApplications];
for(NSRunningApplication *app in apps) {
[self.dataArr addObject:app.localizedName];
}
[self.tableView reloadData];
4、監(jiān)聽Mac上所有已經(jīng)運(yùn)行的app
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidRunning:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
5宁否、kill進(jìn)程代碼
- (void)tableViewDoubleClick:(NSTableView *)tableView {
NSInteger selectedIndex = [tableView selectedRow];
ApplicationModel *model = self.dataArr[selectedIndex];
NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleWarning;
alert.messageText = @"溫馨提示";
alert.informativeText = @"確定要?dú)⑺涝撨M(jìn)城嗎";
[alert addButtonWithTitle:@"確定"];
[alert addButtonWithTitle:@"取消"];
__weak typeof(self) weakSelf = self;
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
if(returnCode == 1000) {
kill(model.pid, SIGKILL);
[weakSelf.dataArr removeObject:model];
[weakSelf.tableView reloadData];
}
}];
}
6窒升、NSTableView 雙擊事件
[self.tableView setDoubleAction:@selector(tableViewDoubleClick:)];
7、長(zhǎng)按拖動(dòng)NSTableViewCell慕匠,首先注冊(cè)tableview
[self.tableView registerForDraggedTypes:[NSArray arrayWithObject:CustomTableViewCellDrap]];
實(shí)現(xiàn)tableview拖動(dòng)的delegate
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:CustomTableViewCellDrap] owner:self];
[pboard setData:data forType:CustomTableViewCellDrap];
self.originIndex = rowIndexes.firstIndex;
return YES;
}
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation {
return NSDragOperationMove;
}
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation {
id obj = self.dataArr[self.originIndex];
[self.dataArr insertObject:obj atIndex:row];
if(self.originIndex < row) {
[self.dataArr removeObjectAtIndex:self.originIndex];
}
else {
[self.dataArr removeObjectAtIndex:self.originIndex + 1];
}
return YES;
}
8饱须、拖動(dòng)NSCollectionViewCell,首先注冊(cè)拖動(dòng)事件
[self.collection registerForDraggedTypes:@[NSPasteboardTypeString]];
[self.collection setDraggingSourceOperationMask:NSDragOperationEvery forLocal:yearMask];
[self.collection setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
其次實(shí)現(xiàn)拖動(dòng)代理函數(shù)
@property (nonatomic, assign) NSIndexPath *currentIndex;
- (void)collectionView:(NSCollectionView *)collectionView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths {
self.currentIndex = indexPaths.allObjects.firstObject;
}
- (id<NSPasteboardWriting>)collectionView:(NSCollectionView *)collectionView pasteboardWriterForItemAtIndex:(NSUInteger)index {
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
ApplicationModel *model = self.dataArr[index];
NSString *indexString = [NSString stringWithFormat:@"%@", model.path];
[item setString:indexString forType:NSPasteboardTypeString];
return item;
}
- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id<NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation {
return YES;
}
- (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id<NSDraggingInfo>)draggingInfo proposedIndexPath:(NSIndexPath *__autoreleasing _Nonnull *)proposedDropIndexPath dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation {
if(*proposedDropOperation == NSCollectionViewDropBefore) {
*proposedDropOperation = NSCollectionViewDropOn;
return NSDragOperationNone;
}
[collectionView moveItemAtIndexPath:self.currentIndex toIndexPath:*proposedDropIndexPath];
return NSDragOperationMove;
}