2017-03-31
iOS_Docs
變更記錄
序號(hào) | 錄入時(shí)間 | 備注 |
---|---|---|
1 | 2017-03-31 | 新建文章 |
2 | 2018-02-28 | 更新引言 |
3 | 2018-04-22 | 更新目錄笙僚,去除標(biāo)簽 |
4 | 2019-09-30 | 更新圖片 |
5 | 2020-03-21 | 更新格式 |
TODO
- 更新Xib和AutoLayout
- Xcode11新特性
引言
對(duì)于iOS開(kāi)發(fā)中佩研,
Xcode
是我們天天打交道使用的官方開(kāi)發(fā)工具纯命,經(jīng)過(guò)許多個(gè)版本的迭代惶岭,Xcode
已經(jīng)越來(lái)越好用涎拉,功能越來(lái)越強(qiáng)大泳秀,開(kāi)發(fā)者雖然經(jīng)常使用丐枉,但是不一定清楚Xcode
的一切奇贏技巧小泉。本篇將介紹從常用快捷鍵芦疏、debug方法以及常見(jiàn)問(wèn)題等來(lái)全面介紹Xcode
。
目錄
[Build settings reference](#Build settings reference)
[Scheme Menu](#Scheme Menu)
添加文件
搜索
Signing
Distribution發(fā)布App
Debug
[Code Test](#Code Test)
附錄
參考
<a id="Build settings reference"></a>Build settings reference
<a id="Scheme Menu"></a>Scheme Menu
Use the scheme menu to switch the active scheme and destination. You can also use the scheme menu to create, edit, and manage schemes, and to add and download simulators.
使用“計(jì)劃”菜單切換活動(dòng)方案和目標(biāo)微姊。您還可以使用“計(jì)劃”菜單創(chuàng)建酸茴、編輯和管理計(jì)劃,并添加和下載模擬器兢交。
Scheme
s and destination
s are distinct. Scheme
s specify the settings for building, running, testing, profiling, analyzing, and archiving your project.
Destination
s specify installation locations for your app.
Typically, a scheme
exists for each target
in your project. Destinations
exist for physical devices and simulators.
計(jì)劃和目的地是不同的薪捍。計(jì)劃指定設(shè)置、運(yùn)行配喳、測(cè)試酪穿、分析、分析和歸檔項(xiàng)目的設(shè)置晴裹。目的地指定應(yīng)用程序的安裝位置被济。通常,項(xiàng)目中的每個(gè)目標(biāo)存在一個(gè)計(jì)劃涧团。目的地存在物理設(shè)備和模擬器只磷。
<a id="添加文件"></a>添加文件
如果要將文件和文件夾駐留在項(xiàng)目文件夾中,請(qǐng)選擇此選項(xiàng)泌绣。
Copy the files and folders into the project:
Select “Copy items if needed.”如果希望該組結(jié)構(gòu)與文件結(jié)構(gòu)相同
Create groups for each folder: Select “Create groups.”如果要將文件夾及其內(nèi)容顯示在項(xiàng)目導(dǎo)航器中钮追,但實(shí)際上不駐留在項(xiàng)目中,請(qǐng)選擇此選項(xiàng)阿迈。文件夾引用是項(xiàng)目導(dǎo)航器中文件系統(tǒng)中的文件夾的引用元媚。
Create references for each folder: Select “Create folder references.”
刪除文件
瀏覽項(xiàng)目中的類、函數(shù)和其他符號(hào)
<a id="搜索"></a>搜索
1. Find and replace content in a project
2. Find symbol references
3. Find the call hierarchy of a method
4. Scope a find or replace operation
Add code annotations to the jump bar
Add a
to-do
item: Insert a comment with the prefix TODO:. For example: // TODO: [your to-do item].Add a bug fix reminder: Insert a comment with the prefix
FIXME
:. For example: // FIXME: [your bug fix reminder].Add a heading: Insert a comment with the prefix
MARK
:. For example: // MARK: [your section heading].Add a separator line: To add a separator above an annotation, add a hyphen (-) before the comment portion of the annotation. For example: // MARK: - [your content]. To add a separator below an annotation, add a hyphen (-) after the comment portion of the annotation. For example: // MARK: [your content] -.
Create and use code snippets
Auto Layout Guide
<a id="Signing"></a>Signing
1. Add apple ID
2. Assign a project to a team
In order to sign your app, each target in your Xcode project must be assigned to the same team. If you enroll as an individual, you’re considered a one-person team. If you are not a member of the Apple Developer Program, Xcode will create a personal team for you.
3. Launch an app on a device
4. Manually sign your app
Use manual signing if you want to create your own provisioning profiles and signing identities. For example, you use one build configuration to develop for the store and another build configuration to develop for distribution outside of the store. You’ll need to sign in to your developer account to perform some of the signing steps manually. Your team role determines what tasks you can perform in Xcode and the developer account.
4.1 Enable manual signing
4.2 Download the provisioning profile from your developer account
4.3 Import the provisioning profile
4.4 Profile講解
商店供應(yīng)配置文件是分發(fā)配置文件,授權(quán)您的應(yīng)用程序使用某些應(yīng)用程序服務(wù)刊棕,并確保你是一個(gè)上傳你的應(yīng)用程序炭晒。存儲(chǔ)配置文件包含單個(gè)應(yīng)用程序ID,該應(yīng)用程序ID匹配一個(gè)或多個(gè)應(yīng)用程序和分發(fā)證書(shū)甥角。你的配置應(yīng)用程序ID間接通過(guò)Xcode使用某些應(yīng)用程序的服務(wù)腰埂。通過(guò)設(shè)置權(quán)限和執(zhí)行其他配置步驟,啟用和配置應(yīng)用程序服務(wù)蜈膨。有的權(quán)利是App ID啟用(一套你的團(tuán)隊(duì)創(chuàng)建的應(yīng)用程序),和其他人在Xcode項(xiàng)目牺荠。當(dāng)你上傳你的應(yīng)用程序商店翁巍,Xcode簽署應(yīng)用程序包與分布在存儲(chǔ)配置文件引用的證書(shū)。
查閱設(shè)備上的provisioning profiles
4.5 檢查Code Signing
4.6 **Ad Hoc講解
An ad hoc provisioning profile is a distribution provisioning profile that allows your app to be installed on designated devices and to use app services without the assistance of Xcode. It’s one of the two types of distribution provisioning profiles that you can create for apps. (You use the other type of distribution provisioning profile later to submit your app to the store.) An ad hoc provisioning profile ensures that test versions of your app aren’t copied and distributed without your knowledge.
When you’re ready to distribute your app to testers, you create an ad hoc provisioning profile specifying an App ID that matches one or more of your apps, a set of test devices, and a single distribution certificate.
Each device in an ad hoc provisioning profile is identified by its unique device ID (UDID). The devices you register and add to a provisioning profile are stored in your developer account. Each individual or organization can register up to 100 devices per product family per membership year for development and testing. You can register 100 devices of each type per year. For iOS apps, you can register 100 iPad, 100 iPhone, and 100 iPod Touch devices.
You signed the iOS App file using the distribution certificate specified in the ad hoc provisioning profile. The ad hoc provisioning profile was included in the app bundle when you built and archived the app. Then you installed the app on a test device. The app successfully launches if the app’s bundle ID matches the App ID, the signature matches the distribution certificate, and the device is in the device list of the ad hoc provisioning profile.
Bundle ID講解
Bundle ID精確地標(biāo)識(shí)單個(gè)應(yīng)用程序休雌。在開(kāi)發(fā)過(guò)程中灶壶,當(dāng)應(yīng)用程序分發(fā)給客戶時(shí),一個(gè)束ID被用來(lái)提供設(shè)備和操作系統(tǒng)杈曲。例如驰凛,游戲中心和應(yīng)用程序購(gòu)買使用Bundle ID,以確定您的應(yīng)用程序時(shí)担扑,使用這些應(yīng)用程序服務(wù)恰响。首選項(xiàng)系統(tǒng)使用此字符串來(lái)標(biāo)識(shí)給定偏好應(yīng)用程序。同樣涌献,啟動(dòng)服務(wù)使用Bundle ID來(lái)定位一個(gè)能夠打開(kāi)特定文件的應(yīng)用程序胚宦,它使用給定標(biāo)識(shí)符的第一個(gè)應(yīng)用程序。Bundle ID也用于驗(yàn)證應(yīng)用程序的簽名燕垃。
Bundle ID字符串必須是一個(gè)統(tǒng)一的類型標(biāo)識(shí)符(UTI)只包含字母數(shù)字字符(a-z枢劝、A-Z、0-9)卜壕,連字符(-)您旁,和周期(。)轴捎。字符串應(yīng)該是反向DNS格式鹤盒。例如,如果您的組織的域名acme.com和你創(chuàng)建一個(gè)應(yīng)用程序命名為您好轮蜕,您可以指定字符串com.acme.hello作為你的應(yīng)用程序的包ID昨悼。
在開(kāi)發(fā)過(guò)程中椅文,您使用一個(gè)應(yīng)用程序的BundleID在許多不同的地方欠气,以鑒定你的應(yīng)用程序。
Specifically, the bundle ID is located and used as follows:
In the Xcode project, the bundle ID is stored in the information property list file (Info.plist). This file is later copied into your app’s bundle when you build the project.
In iTunes Connect, you enter the bundle ID to identify your app. After your first build is uploaded to iTunes Connect, you can’t change your bundle ID or delete the associated explicit App ID.
In your developer account, Xcode creates an App ID that matches the app’s bundle ID. If the App ID is an explicit App ID, it exactly matches the bundle ID. However, unlike domain names, bundle IDs are case sensitive. If the App ID is lowercase, your bundle ID needs to be lowercase, too.
In iCloud, the container IDs you specify in your Xcode project are based on the bundle IDs of one or more apps.
<a id="Distribution發(fā)布App"></a>Distribution發(fā)布App
Validate an archive of your app
Validate an archive of your app to find out whether it meets minimum store submission requirements.
開(kāi)發(fā)完打包成Archive后虫埂,在提交到AppStore
之前可以通過(guò)Validate archive來(lái)驗(yàn)證Archive是否有配置問(wèn)題汇竭。
Review validation issues found, if any, and click Done.
If no issues are found, a green checkmark appears.
If Xcode doesn’t find an iTunes Connect record for your app, the validation fails. In iTunes Connect, create a record for your app and ensure that the bundle ID matches the bundle ID in your Xcode project.
Alt text
<a id="Debug"></a>Debug
- 使用調(diào)試區(qū)域來(lái)控制和檢查正在運(yùn)行的應(yīng)用程序
變量視圖顯示可用在代碼您現(xiàn)在的位置范圍檢查的變量列表葱蝗。這份名單是披露的層次結(jié)構(gòu)穴张,揭示了一個(gè)可變的結(jié)構(gòu)各部分的值作為你逐步擴(kuò)大披露三角形。
該控制臺(tái)包含一個(gè)交互式終端類文本區(qū)域两曼。您可以使用它來(lái)直接與互動(dòng)LLDB皂甘,請(qǐng)參閱使用打印描述的輸出,并與您的應(yīng)用程序的標(biāo)準(zhǔn)輸入和輸出工作悼凑。
在調(diào)試區(qū)域中查看變量
在調(diào)試區(qū)域中偿枕,單擊暫停按鈕或等待您的應(yīng)用程序在您先前設(shè)置的斷點(diǎn)處停止。
范圍中的變量出現(xiàn)在調(diào)試區(qū)域的變量視圖中户辫。
要查看變量的結(jié)構(gòu)渐夸,請(qǐng)展開(kāi)變量左側(cè)的公開(kāi)三角形。
從變量視圖左下角的彈出菜單中選擇一個(gè)范圍選項(xiàng)渔欢。
顯示最近訪問(wèn)的變量:選擇自動(dòng)墓塌。
僅顯示局部變量:選擇本地。
顯示所有變量奥额,寄存器苫幢,全局變量和靜態(tài):選擇全部。
要過(guò)濾結(jié)果垫挨,請(qǐng)?jiān)谟蚁陆堑乃阉髯侄沃休斎胛谋尽?/p>
Specialized Debugging Workflows
This chapter focuses on often encountered but more specialized debugging scenarios and highlights the Xcode debugging tools used to work with them.
Enable Zombie Objects.
Enable Zombie Objects. Replace deallocated objects with a “zombie” object that traps any attempt to use it. When you send a message to a zombie object, the runtime logs an error and crashes. You can look at the backtrace to see the chain of calls that triggered the zombie detector.
開(kāi)啟Zombie模式之后會(huì)導(dǎo)致內(nèi)存上升韩肝,因?yàn)樗砸呀?jīng)被釋放(引用計(jì)數(shù)為0)的對(duì)象被僵尸對(duì)象取代,并未真的釋放掉棒拂。這個(gè)時(shí)候再給僵尸對(duì)象發(fā)送消息伞梯,就會(huì)拋出異常,并打印出異常信息帚屉,你可以輕松的找到錯(cuò)誤代碼位置谜诫,結(jié)束Zombies時(shí)會(huì)釋放。它的主要功能是檢測(cè)野指針調(diào)用攻旦。iOS開(kāi)發(fā)之Xcode常用調(diào)試技巧總結(jié)
Using the Address Sanitizer
一旦啟用了地址清潔程序喻旷,Xcode將在下次運(yùn)行時(shí)重新編譯您的應(yīng)用程序,并添加工具來(lái)立即捕獲內(nèi)存違規(guī)并停止應(yīng)用程序牢屋。你可以在這樣發(fā)生的地方檢查問(wèn)題且预。還提供其他診斷信息,例如故障地址與堆上的有效對(duì)象之間的關(guān)系以及分配/釋放信息烙无,這有助于您快速找出并修復(fù)問(wèn)題锋谐。
將應(yīng)用程序的代碼添加到應(yīng)用程序的代碼中會(huì)產(chǎn)生性能損失,但是地址清潔程序的效率足以使用交互式應(yīng)用程序定期使用截酷。有關(guān)更多信息和地址清潔劑的演示涮拗,請(qǐng)參見(jiàn)此視頻演示:WWDC 2015:高級(jí)調(diào)試和地址消毒劑。
Using the Thread Sanitizer
When enabled, the thread sanitizer checks for uninitialized mutexes, thread leaks, unsafe calls in signal handlers, and data races.
Like the address sanitizer, running an app with the thread sanitizer introduces a small performance penalty, which should be negligible when interacting with most apps. For more information and a demo of thread sanitizer in action, see this video presentation: WWDC 2016: Thread Sanitizer and Static Analysis.
參考
更多Debug知識(shí)點(diǎn)請(qǐng)參考
Debugging with Xcode
斷點(diǎn)
發(fā)生異常時(shí)暫停
當(dāng)測(cè)試發(fā)生故障暫停
<a id="Code Test"></a>Code Test
View and adjust performance tests
<a id="附錄"></a>附錄
調(diào)試技巧
真機(jī)調(diào)試直接截圖到Mac
Xcode快捷鍵
1. Open Quickly
都知道 Cmd + Option + O
,但是你知道怎么在輔助編輯器中打開(kāi)嗎三热?按住Alt + Enter
鼓择, enjoy~
2. 交換上下行代碼: Cmd + Option + [
or Cmd + Option + ]
3. To jump to the definition. 光標(biāo)所在處Command + Control + J
或者在輔助編輯器中打開(kāi)
???+J.
4.切換多個(gè)編輯器焦點(diǎn):
Command + J
5. 文件切換
- ?+1 - Related items menu that shows files related to what you are currently working on
- ?+2 - Previous and next buttons to navigate
between your most recent files
The rest of the jump bar consists of heierarchial navigation with each level given a separate shortcut. - ?+4 - Project level navigation
- ?+5 - Switch between header and implementation files
- ?+6 - Navigation within a file
6. 在輔助編輯器中跳轉(zhuǎn)到變量的定義: Command + Alt
+ 鼠標(biāo)點(diǎn)擊對(duì)應(yīng)變量名
7. 在點(diǎn)擊文件名時(shí)候,按住Alt
將讓文件在輔助編輯器中打開(kāi)
8. 在光標(biāo)位置顯示Help: ? + ? + ? + /
9. Filter in Navigator
本來(lái)熱鍵是Cmd + Option + J
,發(fā)現(xiàn)貌似不起作用就漾,就改成Cmd + Option + O
10. 斷點(diǎn)全部disable
Cmd+ Y
呐能,灰色
刪除添加某個(gè)斷點(diǎn) Cmd + \
[Xcode8 適配iOS10時(shí)遇見(jiàn)的一些坑
](http://m.blog.csdn.net/article/details?id=52602098)
注意:Xcode8內(nèi)置了開(kāi)啟注釋的功能,位置在這里
快捷鍵的設(shè)置在這里
加速你的Xcode!
在Xcode
啟動(dòng)的時(shí)候,設(shè)置Behaviors
:
默認(rèn)是:
可以將調(diào)試的Debug放在另一個(gè)窗口抑堡,隱藏一些不需要的NaviBar
在斷點(diǎn)的時(shí)候:
還可以自定義場(chǎng)景摆出,比如寫代碼的時(shí)候:
還可以跑腳本
Xcode 8的坑
- 代碼注釋不能用的解決辦法
這個(gè)是因?yàn)樘O果解決xcode ghost,把插件屏蔽了首妖。
解決方法
打開(kāi)終端懊蒸,命令運(yùn)行:
sudo /usr/libexec/xpccachectl
然后必須重啟電腦后生效 -
權(quán)限以及相關(guān)設(shè)置
注意,添加的時(shí)候悯搔,末尾不要有空格
我們需要打開(kāi)info.plist文件添加相應(yīng)權(quán)限的說(shuō)明,否則程序在iOS10上會(huì)出現(xiàn)崩潰舌仍。
具體如下圖:
Alt text
- 麥克風(fēng)權(quán)限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風(fēng)妒貌?
- 相機(jī)權(quán)限: Privacy - Camera Usage Description 是否允許此App使用你的相機(jī)?
- 相冊(cè)權(quán)限: Privacy - Photo Library Usage Description 是否允許此App訪問(wèn)你的媒體資料庫(kù)铸豁?通訊錄權(quán)限: Privacy - Contacts Usage Description 是否允許此App訪問(wèn)你的通訊錄灌曙?
- 藍(lán)牙權(quán)限:Privacy - Bluetooth Peripheral Usage Description 是否許允此App使用藍(lán)牙?
- 語(yǔ)音轉(zhuǎn)文字權(quán)限:Privacy - Speech Recognition Usage Description 是否允許此App使用語(yǔ)音識(shí)別节芥?
- 日歷權(quán)限:Privacy - Calendars Usage Description 是否允許此App使用日歷在刺?
- 定位權(quán)限:Privacy - Location When In Use Usage Description 我們需要通過(guò)您的地理位置信息獲取您周邊的相關(guān)數(shù)據(jù)
- 定位權(quán)限: Privacy - Location Always Usage Description 我們需要通過(guò)您的地理位置信息獲取您周邊的相關(guān)數(shù)據(jù)
- 定位的需要這么寫,防止上架被拒头镊。
- 字體變大蚣驼,原有frame需要適配
經(jīng)有的朋友提醒,發(fā)現(xiàn)程序內(nèi)原來(lái)2個(gè)字的寬度是24相艇,現(xiàn)在2個(gè)字需要27的寬度來(lái)顯示了颖杏。。
希望有解決辦法的朋友坛芽,評(píng)論告我一下耶留储,謝謝啦 -
推送
如下圖的部分,不要忘記打開(kāi)咙轩。所有的推送平臺(tái)获讳,不管是極光還是什么的,要想收到推送活喊,這個(gè)是必須打開(kāi)的喲
Alt text
PS.蘋果這次對(duì)推送做了很大的變化丐膝,希望大家多查閱查閱,處理推送的代理方法也變化了。
// 推送的代理
[<UNUserNotificationCenterDelegate>]
iOS10收到通知不再是在
[application: didReceiveRemoteNotification:]
方法去處理尤误, iOS10推出新的代理方法侠畔,接收和處理各類通知(本地或者遠(yuǎn)程)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { //應(yīng)用在前臺(tái)收到通知 NSLog(@"========%@", notification);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { //點(diǎn)擊通知進(jìn)入應(yīng)用 NSLog(@"response:%@", response);}
- 屏蔽雜亂無(wú)章的bug
更新Xcode8之后,新建立工程损晤,都會(huì)打印一堆莫名其妙看不懂的Log.
如這些
subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1,
屏蔽的方法如下:
Xcode8里邊 Edit Scheme-> Run -> Arguments, 在Environment Variables里邊添加
OS_ACTIVITY_MODE = Disable
- iOS 10 字體隨著手機(jī)系統(tǒng)字體而改變
當(dāng)我們手機(jī)系統(tǒng)字體改變了之后软棺,那我們App的label也會(huì)跟著一起變化,這需要我們寫很多代碼來(lái)進(jìn)一步處理才能實(shí)現(xiàn)尤勋,但是iOS 10 提供了這樣的屬性adjustsFontForContentSizeCategory來(lái)設(shè)置喘落。因?yàn)闆](méi)有真機(jī),具體實(shí)際操作還沒(méi)去實(shí)現(xiàn)最冰,如果理解錯(cuò)誤幫忙指正瘦棋。
UIFont 的preferredFontForTextStyle: 意思是指定一個(gè)樣式,并讓字體大小符合用戶設(shè)定的字體大小暖哨。
*/
myLabel.font =[UIFont preferredFontForTextStyle: UIFontTextStyleHeadline]; /*
Indicates whether the corresponding element should automatically update its font when the device’s UIContentSizeCategory is changed.
For this property to take effect, the element’s font must be a font vended using +preferredFontForTextStyle: or +preferredFontForTextStyle:compatibleWithTraitCollection: with a valid UIFontTextStyle.
*/
//是否更新字體的變化
myLabel.adjustsFontForContentSizeCategory = YES;