版权声明:本文为博主原创文章,转载请附上本文链接地址。from : https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/86000692
关于 Move to Applications folder
很多应用下载开启后,经常可以看到提示
推想一下,sparkle 的原理,是将新应用下载保存到 library 之后,移动替换掉原来App 的位置。 那么这里的移动应该也是有辅助程序?
GitHub 搜索 Move to Applications folder ,可以找到几个开源代码:
https://github.com/Eric-Bro/EBApplication
https://github.com/RISCfuture/TMMoveToApplicationsFolder
根据 TMMoveToApplicationsFolde 提供的文件写了个简单的demo,适合伸手党:
https://download.csdn.net/download/lovechris00/10901065
- 不需要写辅助程序,复制App 文件到 Applications 文件夹;
- 移动后启动applications 里面 app,并终结自己。
- 关闭沙盒后运行没问题;开启沙盒则会提示:
“MoveAppFolderDemo” couldn’t be copied because you don’t have permission to access “Applications”.
file access 没有对 Applications 权限设置。
- 如果 Applications 文件夹已存在内容,则会提示
“MoveAppFolderDemo” couldn’t be copied to “Applications” because an item with the same name already exists.
你也可以在移动之前删除文件。
核心代码
判断当前应用是否在 Applications 文件夹
- (BOOL) applicationIsInApplicationsFolder {
NSArray *appPath = [[self currentPath] pathComponents];
NSString *docName = [appPath objectAtIndex:([appPath count] - 2)];
return ([docName isEqualToString:@"Applications"]);
}
移动应用程序
- (void) moveApplication {
NSError *error = NULL;
BOOL result = [[NSFileManager defaultManager] copyItemAtPath:[self currentPath] toPath:[self newPath] error:&error];
if (result) {
NSArray *files = @[[[self currentPath] lastPathComponent]];
NSInteger tag = 0;
NSString *path = [[self currentPath] stringByDeletingLastPathComponent];
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:path destination:@"" files:files tag:&tag];
[self relaunch];
}
else [self displayMoveError:error];
}
重启
- (void) relaunch {
[[NSWorkspace sharedWorkspace] openFile:[self newPath]];
[[NSApplication sharedApplication] terminate:self];
}
- (NSString *) newPath {
if (!newPath) {
NSString *bundleName = [[self currentPath] lastPathComponent];
newPath = [@"/Applications" stringByAppendingPathComponent:bundleName];
}
return newPath;
}