1.Info.Plist文件的操作
1.支持Https協(xié)議
//1.打開(kāi)info.plist->Open As ->Source Code
//2.加入允許的鍵值對(duì)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
2.導(dǎo)航欄上方的狀態(tài)欄顏色等修改操作
//1.在info.plist文件中設(shè)置
(雙擊info.plist-->Open As --> Source Code)加入以下鍵值對(duì)
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
//3.在Appdelegate中設(shè)置
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//4.隱藏導(dǎo)航欄上方的狀態(tài)欄
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
5.語(yǔ)言本地化/國(guó)際化設(shè)置
1.語(yǔ)言國(guó)際化詳細(xì)說(shuō)明請(qǐng)參考
2.完整的語(yǔ)言代號(hào)列表
//1.打開(kāi)info.plist -->Localization native development region ->en(默認(rèn)英文)
//2.如果只支持中文"en"代號(hào)替換成''zh"代號(hào)
6.使用地圖時(shí)鲸阻,開(kāi)啟相關(guān)條件
//1.打開(kāi)info.plist->Open As ->Source Code
//2.加入允許的鍵值對(duì)
//一直允許定位
<key>NSLocationAlwaysUsageDescription</key>
<string>
請(qǐng)點(diǎn)擊“允許”以允許“中原找房”訪問(wèn)您的位置信息当娱。
</string>
//使用時(shí)允許定位
<key>NSLocationWhenInUseUsageDescription</key>
<string>
請(qǐng)點(diǎn)擊“允許”以允許“中原找房”訪問(wèn)您的位置信息脓鹃。
</string>
//3.對(duì)應(yīng)的方法
//在IOS8中定位功能新增了兩個(gè)方法:
- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
7.Plist文件的讀取與創(chuàng)建
- 目的: 方便存儲(chǔ)一些列表固定數(shù)據(jù)(如:城市列表及相關(guān)參數(shù))
- 介紹: plist文件的實(shí)質(zhì)為Json數(shù)據(jù)的一個(gè)容器(Root結(jié)構(gòu)可以是NSDictorny/NSArray)
- 手動(dòng)創(chuàng)建plist文件
/*!
* @brief 創(chuàng)建的plist文件&&讀扰鬯(手動(dòng))
*/
- (void)readSystemPlistFiles{
//1.創(chuàng)建步驟,Command+N -> 選擇Resource --> PropertyPlist --> xx(文件名).plist
//2.設(shè)置plist文件結(jié)構(gòu)(.plist文件的實(shí)質(zhì)就是一個(gè)json數(shù)據(jù)的容器)
//3.讀取文件
//3.1獲取已有完整路徑
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"plist"];
//3.2根據(jù)文件路徑讀取json數(shù)據(jù),注意:如果Root是NSArray結(jié)構(gòu),就用NSArray讀取接收;如果是NSDictory,就用NSDictory讀取接收
NSArray *resourcePlist = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"%@",resourcePlist);
}
//輸出:
2016-08-12 14:31:01.282 Plist文件的使用和創(chuàng)建[5091:156679] (
{
ciityId = 2;
cityCode = 1001;
cityName = "\U4e0a\U6d77";
},
{
ciityId = 3;
cityCode = 1002;
cityName = "\U5317\U4eac";
},
{
ciityId = 4;
cityCode = 1003;
cityName = "\U5357\U4eac";
},
{
ciityId = 6;
cityCode = 1005;
cityName = "\U6df1\U5733";
}
)
- 代碼創(chuàng)建plist文件弓熏,存儲(chǔ)在NSDocumentDirectory中
/*!
* @brief 代碼創(chuàng)建plist文件
*/
- (void)createPlistFilesByCode {
//1.讀取文件路徑
NSString *filePath = getDoucmentPathWithName(@"PropertyList");
//2.根據(jù)文件路徑,讀取接收數(shù)據(jù)
NSMutableArray<NSDictionary*> *resourcePlist = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
NSMutableDictionary *dic = [[NSMutableDictionary alloc ] init];
[dic setObject:@"總管達(dá)人" forKey:@"name"];
[dic setObject:@"123456" forKey:@"password"];
[resourcePlist addObject:dic];
//3.寫(xiě)入文件
[resourcePlist writeToFile:filePath atomically:YES];
NSLog(@"%@",resourcePlist);
}
/*!
* @brief 獲取本地沙盒路徑
*
* @param name 文件名字
*
* @return 文件路徑
*/
static NSString *getDoucmentPathWithName(NSString *fileName) {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//獲取完整路徑
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
return plistPath;
}
//輸出:
2016-08-12 15:12:21.848 Plist文件的使用和創(chuàng)建[5292:187489] (
{
name = "\U5b59\U609f\U7a7a111";
password = sunwukong;
},
{
name = "\U5b59\U609f\U7a7a111";
password = sunwukong;
},
{
name = "\U603b\U7ba1\U8fbe\U4eba";
password = 123456;
}
)
2.AppDelegate操作
1.后臺(tái)運(yùn)行
使用 block 的另一個(gè)用處是可以讓程序在后臺(tái)較長(zhǎng)久的運(yùn)行糠睡。在以前挽鞠,當(dāng) app 被按 home 鍵退出后,app 僅有最多 5 秒鐘的時(shí)候做一些保存或清理資源的工作。但是應(yīng)用可以調(diào)用 UIApplication 的beginBackgroundTaskWithExpirationHandler方法滞谢,讓 app 最多有 10 分鐘的時(shí)間在后臺(tái)長(zhǎng)久運(yùn)行串稀。這個(gè)時(shí)間可以用來(lái)做清理本地緩存,發(fā)送統(tǒng)計(jì)數(shù)據(jù)等工作狮杨。
// AppDelegate.h 文件
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;
// AppDelegate.m 文件
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self beingBackgroundUpdateTask];
// 在這里加上你需要長(zhǎng)久運(yùn)行的代碼
[self endBackgroundUpdateTask];
}
- (void)beingBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void)endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}