iOS相关tips
Tips
iOS8.3之后,通过iTunes、iFunBox等工具已无法直接访问应用的沙盒,如果需要让自己的应用能够通过其访问,必须在应用的
info.plist中添加UIFileSharingEnabled关键字,并赋值为YES,这样,就能访问其Documents目录了(注意,其它目录还是无法访问)。使用
UIWebView时,发现在首次加载网页时,加载时间异常的长,其原因是服务器需要花费一定的时间来解析请求来自于哪个平台,比如PC、iPhone、iPad等,以针对不同的平台响应不同的页面布局,解决该问题的方法为客户端直接请求特定版本的页面,且设置请求的UA(User Agent),如下代码:1
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : @" Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1"}];
在使用
UIBarButtonItem,并对其使用UIImage赋值时,iOS7之后会对UIImage设置tintColor,从而改变图片的原值色,如果需要保留图片原色,可以对UIImage进行如下代码设置:1
2UIImage *image = [UIImage imageNamed:@"myImage.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
iOS7之后,对于
UIViewController的内容布局,默认会从屏幕顶部开始,这就会导致内容被状态栏所覆盖。
如果UIViewController包含在UINavigationController下且navigationBar是可见的,那么可以使用如下代码来使内容布局不被状态栏覆盖:1
self.edgesForExtendedLayout = UIRectEdgeNone;
如果
navigationBar不可见,那么就只能调整UIVew的位置了,向下移20 points。UIWebView加载页面时底部出现黑条问题解决方法:设置UIWebView的backgroundColor为clearColor,opaque为NO即可。当我们使用Carthage作为依赖管理器时,安装库时,经常会执行
carthage update命令,此时,不同的库,可能会生成多个平台的frameworks,如iOS,macOS,watchOS,tvOS,所以,如果我们的项目只需要使用到iOS的framework,那么可以在执行update命令时加上参数,如carthage update --platform iOS当在
UIWebView中播放audio时,默认在应用进入后台或锁定等情况时,audio会暂停。解决方法为:- 在
info.plist中加入键值对如下:1
2
3
4<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array> - 设置
Audio Session的类别,默认类别为AVAudioSessionCategorySoloAmbient,将类别设置为AVAudioSessionCategoryPlayback,代码如下:1
2
3
4
5
6
7
8
9
10
11
12#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }
- 在
非越狱下获取设备安装的所有应用:
1
2
3
4
5
6
7
8
9
10Class c = NSClassFromString(@"LSApplicationWorkspace");
id s = [(id)c performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *arr = [s performSelector:NSSelectorFromString(@"allInstalledApplications")];
for (id item in arr) {
NSLog(@"%@",[item performSelector:NSSelectorFromString(@"applicationIdentifier")]);
NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleVersion")]);
NSLog(@"%@",[item performSelector:NSSelectorFromString(@"shortVersionString")]);
}当使用
Core Foundation时,当你是对象的拥有者时,需要调用CFRelease,这里需要注意的是,不能调用CFRelease(NULL),会导致程序崩溃,所以,调用CFRelease前需先判断一下对象是否存在(当然,有些特定的release函数容许NULL参数,如CGGradientRelease)。UITableViewCell默认分割线会在左侧留出一定的间距,如果想调整分割线的左、右间距,可以通过调整UITableView的separatorInset属性。当使用
WKWebView的WKScriptMessageHandler时,WKUserContentController会retainmessage handler,所以,要注意避免循环引用。CAAnimation的delegate是强引用的,强引用的,强引用的。可以通过操作完成后移除动画或设置一个代理对象作为delegate。当使用
UIView、CALayer的transform时,会影响到frame属性,此时我们应该废弃掉frame,而是通过bounds和position、center来进行设置。