1.沙盒文件路径
//沙盒文件路径
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
2.编译包位置
上边路径的位置回退三层找到Bundle文件夹,下翻Application里边就有该版本模拟器的所有编译包
3.获取app版本号
NSString *versionNumber = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
4.设置联网请求状态(状态栏上的菊花转圈)
//设置NO 就停止
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
5.设置应用程序图标的小红数字
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:10];
//注意:显示需要注册用户badge用户通知
[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert categories:nil]];
6.main.m文件main函数注解
From:Apple Developer Documentation
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
①Main.m文件
main例程只做三件事:
1.创建一一个自动释放池,
2.调用UIApplicationMain函数,
3.释放自动释放池。
② UIApplicationMain函数
1)参数释义:
- argc和argv是ISO C标准的main函数的参数,直接传递给UIApplicationMain进行相关处理。
- principalClassName是应用程序类的名字(字符串类型),该类必须继承自UIApplication类。如果传入
nil
,系统就默认@"UIApplication"
。- delegateClassName是应用程序类的代理类.如果主要nib文件(在info.plist文件中指定,key是NSMainNibFile)存在,就会在nib文件对象里寻找Application对象和连接它的delegate。
2).作用:
1.根据principalClassName提供的类名创建
UIApplication
对象
2.利用delegateClassName创建一个delegate对象,并将UIApplication对象中的delegate属性设置为delegate对象
3.开启一个主行循环,准备接收处理事件
4.加载info.plist文件,根据info配置加载应用程序
7、iOS程序启动过程
打开程序--->
执行main函数--->
执行UIApplicationMain函数--->
初始化UIApplication(创建和设置代理对象,开启事件循环)--->
监听系统事件--->
通知UIApplicationDelegate对象--->
创建窗口Window--->
初始化根控制器的view--->
将控制器的view加到window上--->
现在控制器的view就显示到屏幕上了
8、线程间的通信
①Perform
//子 呼叫 主
[self performSelectorOnMainThread:@selector(callMainThread:) withObject:@"object" waitUntilDone:NO];
//新开辟一条线程执行一些操作
[self performSelectorInBackground:@selector(callSubThread:) withObject:nil];
// 任意线程 呼叫 任意线程
[self performSelector:@selector(selector) onThread:thread withObject:nil waitUntilDone:NO];
- 参数waitUntilDone:是否要等待被通信线程做完这个事情再走下面的子线程.YES:阻塞当前线程等被通信线程任务执行完毕以后再走当前线程下面的操作。通俗点:YES:我给你传个消息,我看着这个消息处理完之后,我再干我的事情。 NO:不等,我只发个消息就不管了。
- 参数 Object被呼叫
SEL
传递的参数
② GCD 通信
//获取全局并发队列,添加下载图片任务
1. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
2. NSURL *url = [NSURL URLWithString:@"https://img.haomeiwen.com/i6687791/5ad29d1a5c1c84db.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"];
3. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
4. dispatch_async(dispatch_get_main_queue(), ^{
//回到主线程刷新UI
[self.imageVIew setImage:image];
5. NSLog(@"主线程任务"); ;
});
6. NSLog(@"下载任务子线程");
});
//打印结果 : 先打印 下载任务子线程 再打印 主线程任务
如果将第4行的异步函数改成同步函数 dispatch_sync(dispatch_get_main_queue(), ^{
的话,那么打印顺序就是 :先主线程任务 再 打印 下载任务子线程
原因:sync 同步函数立即触发执行任务再向下执行,async 异步函数是等当前任务做完再开始队列任务
③NSOperation
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
// 2.添加操作
[queue addOperationWithBlock:^{
// 异步进行耗时操作
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@", [NSThread currentThread]); // 打印当前线程
}
// 回到主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// 进行一些 UI 刷新等操作
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@", [NSThread currentThread]); // 打印当前线程
}
}];
}];
④NSPort端口
在子线程持有一个主线程的端口,通过这个端口进行通讯。其常用子类有NSMessagePort
NSMachPort
。同样 主线程也可以持有子线程的端口,进行主线程call子线程
9、文件移动
把from
文件夹下的文件全部剪切到to
文件夹下
NSString *from = @"/Users/**/Desktop/des/Form";
NSString *to = @"/Users/**/Desktop/des/TO";
NSFileManager *fmg = [NSFileManager defaultManager];
NSArray *array = [fmg subpathsAtPath:from];
dispatch_apply(array.count, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSString *subsPath = array[index];
NSString *fromFullPath = [from stringByAppendingPathComponent:subsPath];
NSString *toFullPath = [to stringByAppendingPathComponent:subsPath];
[fmg moveItemAtPath:fromFullPath toPath:toFullPath error:nil];
});
10、iOS 沙盒文件的写入与读取相关
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
static BOOL flag = NO;
if (flag == YES) {
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [filePath stringByAppendingPathComponent:@"download.jpeg"];
if ([[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil]) {
NSLog(@"删除成功文件");
};
flag = NO;
return;
}
flag = YES;
__block NSData *data = nil;
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSBlockOperation *downloadOperation = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"https://img.haomeiwen.com/i6687791/5ad29d1a5c1c84db.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"];
data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"%@",[NSThread currentThread]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
}];
}];
NSBlockOperation *writeBlock = [NSBlockOperation blockOperationWithBlock:^{
if (data) {
//将data写入沙盒caches路径
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [filePath stringByAppendingPathComponent:@"download.jpeg"];
NSLog(@"%@",imagePath);
if ([data writeToFile:imagePath atomically:YES]) {
data = nil;
}
}
}];
//获取刚写入文件属性block
NSBlockOperation *attributeBlock = [NSBlockOperation blockOperationWithBlock:^{
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [filePath stringByAppendingPathComponent:@"download.jpeg"];
NSError *error = nil;
//获取文件属性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:imagePath error:&error];
NSLog(@"%@",dict);
}];
[writeBlock addDependency:downloadOperation];
[attributeBlock addDependency:writeBlock];
[queue addOperation:downloadOperation];
[queue addOperation:writeBlock];
[queue addOperation:attributeBlock];
}
11、info.plist常用
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能访问蓝牙</string>
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能访问日历</string>
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能访问健康分享</string>
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能访问健康更新 </string>
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期间访问位置</string>
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能访问麦克风</string>
<key>NSMotionUsageDescription</key>
<string>App需要您的同意,才能访问运动与健身</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能访问提醒事项</string>
网友评论