这里记录一下平时使用的一些小技巧(持续更新中...)
1. 复制内容到剪切板
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @“testString”; //testString是你要复制的内容
2. 退回到桌面(等同于按Home键)
[[UIApplication sharedApplication] performSelector:@selector(suspend)];
3. 遍历安装的字体
NSArray *familyNames = [UIFont familyNames];
for (NSString *familyName in familyNames) {
printf(" ---------- familyNames = %s ----------\n",[familyName UTF8String]);
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for (NSString *fontName in fontNames) {
printf("\tfontName = %s\n",[fontName UTF8String]);
}
printf("\n");
}
4. 设置屏幕常亮(禁止黑屏)
//oc版本
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
//swift版本
//UIApplication.shared.isIdleTimerDisabled = true;
5. 禁用暗黑模式
如果想要适配Dark Model请参考我的另一篇文章适配暗黑模式
全局禁用
方法一:在info.plist中
<key>UIUserInterfaceStyle</key>
<string>Light</string>
方法二:
程序启动的时候让window的模式设为一般模式
if (@available(iOS 13.0, *)) {
[UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
单个界面禁用 (这个会影响他的子视图)
self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
6.UIScrollView顶部有留白
iOS11之后出现的现象,解决方案
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
7.获取App当前版本
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
8.忽略cocoapods警告
在PodFile文件中添加 inhibit_all_warnings!
platform :ios,'9.0'
#忽略pod产生的警告
inhibit_all_warnings!
target 'YourProjectName' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
end
9.获取手机连接的WiFi名和WiFi地址
首先我们需要导入 #import <SystemConfiguration/CaptiveNetwork.h> 头文件。
1.获取WiFi名称
//获取SSID(Service Set Identifier):服务集标识,即WiFi网络的名称。
+ (NSString *)getWifiName{
NSArray *wifiName = CFBridgingRelease(CNCopySupportedInterfaces());
id info1 = nil;
for (NSString *wfName in wifiName) {
info1 = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef) wfName);
if (info1 && [info1 count]) {
break;
}
}
NSDictionary *dic = (NSDictionary *)info1;
NSString *ssidName = [[dic objectForKey:@"SSID"] lowercaseString];
return ssidName;
}
2.获取WiFi mac地址
//获取BSSID,即WiFi的mac地址
+ (NSString *)getWifiMacAddress{
NSArray *wfMac = CFBridgingRelease(CNCopySupportedInterfaces());
id info1 = nil;
for (NSString *macName in wfMac) {
info1 = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef) macName);
if (info1 && [info1 count]) {
break;
}
}
NSDictionary *dic = (NSDictionary *)info1;
NSString *wifiMac = [dic objectForKey:@"BSSID"];
return wifiMac;
}
注意:1. ⚠️⚠️⚠️获取WiFi信息的两个方法在iOS12之前可以直接获取WiFi信息。如果是iOS12之后,我们需要找到 Target —> Capabilities —> Access WiFi Information 并打开这个选项。详情请查看 资料 。
2. ⚠️⚠️⚠️iOS13之后我们除了打开Access WiFi Information这个选项,我们还需要打开位置权限,否则也是获取不到WiFi信息的。