设置名称和方向 设置项目名称
设置方向
设置必要的权限 在Info.plist的dict节点下添加以下配置
主要配置了http请求允许和所需权限的申请提示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <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 > NSLocationAlwaysAndWhenInUseUsageDescription</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 >
pod常用依赖 项目根目录添加Podfile文件 添加以下内容 注意项目名改为自己的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 source 'https://github.com/CocoaPods/Specs.git' platform :ios, '10.0' use_frameworks! inhibit_all_warnings! target 'ShanSi' do pod 'Alamofire' , '~> 4.7' pod 'SwiftyJSON' , '~> 4.0' pod 'SnapKit' , '~> 4.0.0' pod 'SwiftDate' , '~> 5.0' pod 'UIColor_Hex_Swift' , '~> 4.0.2' end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['ENABLE_BITCODE' ] = 'NO' end end end
pod插件更新
1 2 3 4 5 gem sources -l //注意上面删除的上面列出的 gem sources --remove https://gems.ruby-china.org/ gem sources --add https://gems.ruby-china.com/ sudo gem install -n /usr/local /bin cocoapods --pre
安装依赖
或者更新仓库后安装
1 pod install --repo-update
禁用Bitcode Build Settings中搜索Bitcode
把 Enable Bitcode 设置为 NO
添加Git忽略文件 项目根目录 添加.gitignore文件 添加以下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 *.xccheckout .DS_Store xcuserdata UserInterfaceState.xcuserstate contents.xcworkspacedata build/ *.moved-aside DerivedData *.hmap *.ipa Pods .idea/ *.xcbkptlist
设置主页面 默认新建项目有两个文件LaunchScreen.storyboard和Main.storyboard
LaunchScreen.storyboard是闪屏 加载后才会执行AppDelegate的回调
Main.storyboard是默认闪屏后的主页 我不建议开发用storyboard所以我们用代码控制页面跳转
创建一个MainController
然后在AppDelegate的回调didFinishLaunchingWithOptions中添加代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 func application (_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any ]?) -> Bool { Thread .sleep(forTimeInterval: 2.0 ) self .window?.backgroundColor = UIColor .white; let appear = UINavigationBar .appearance(); appear.isTranslucent = false ; appear.tintColor = UIColor .white; appear.barTintColor = ZJColor .mainColor; appear.backgroundColor = ZJColor .mainColor; appear.setBackgroundImage(UIImage (), for : UIBarMetrics .default ) appear.shadowImage = UIImage (); appear.titleTextAttributes = [ NSAttributedStringKey .foregroundColor: UIColor .white, NSAttributedStringKey .font: UIFont (name: "Heiti SC" , size: 18.0 )! ]; let tabbarAppear = UITabBar .appearance(); tabbarAppear.tintColor = ZJColor .mainColor; let mainController = MainController () let mainNavi = UINavigationController (rootViewController: mainController) self .window?.rootViewController = mainNavi return true }
在项目的根目录添加一个头文件 假如就叫做 Bridge-Header.h
打开项目配置 -> Build Settings
搜索 swift
找到Objective-C Bridging Header
设置值为$(SWIFT_MODULE_NAME)/Bridge-Header.h
设置状态栏颜色 项目的Info.plist添加配置View controller-based status bar appearance设置为NO
设置前景色为白色
1 UIApplication .shared.setStatusBarStyle(UIStatusBarStyle .lightContent, animated: false );
设置前景色为黑色
1 UIApplication .shared.setStatusBarStyle(UIStatusBarStyle .default , animated: false );
设置隐藏
动画有三种方式(Slide(滑动)/Fade(淡入淡出)/None(无动画))
1 UIApplication .shared.setStatusBarHidden(true , withAnimation: UIStatusBarAnimation .slide)