Xcode 10: Improved Bitbucket and GitLab support

September 20th, 2018

Filed under: Version Control, Xcode | Be the first to comment!

Xcode 10 improves version control support for Bitbucket and GitLab. You can add your Bitbucket and GitLab accounts to Xcode from Xcode’s Accounts preferences.

XcodeAccountsPreferencesHighlighted

Click the Add button to add an account. A sheet opens.

Xcode10AddBitbucketAccount

Choose Bitbucket Cloud to add your Bitbucket account. Choose GitLab.com to add your GitLab account.

Starting in Xcode 10 you can add your Xcode project to Bitbucket or GitLab from Xcode. Open the Source Control navigator by choosing View > Navigators > Show Source Control Navigator. Select the Remotes folder, control-click, and choose Create Remote. A sheet opens.

Xcode10AddBitbucketRepoHighlighted

Choose your account from the Account menu. Click the Create button to add your project to Bitbucket or GitLab.


Xcode 10: Library Moved to Toolbar

September 17th, 2018

Filed under: Xcode | Be the first to comment!

In Xcode 10 the Library, where you access things like code snippets and user interface elements, is no longer in the lower right portion of the project window. The button to access the Library has been moved to the right side of the toolbar, next to the buttons to show the various editors.

Xcode10LibraryButtonHighlighted

Click the Library button to open the Library in a separate window. Option-clicking the Library button keeps the window open, which helps a lot when you’re building your app’s user interface.

The specific library that opens depends on the file you’re currently viewing/editing. If you’re in a source code file, clicking the Library button will open the code snippets library. If you’re in a xib file or storyboard, clicking the Library button will open the object library that contains the UI elements.


Supporting Dynamic Type in iOS Text Views

September 10th, 2018

Filed under: iOS Development | Be the first to comment!

Dynamic Type on iOS lets people adjust the size of text to make text easier to read. Someone with poor eyesight can use larger text if the app supports Dynamic Type.

You can support Dynamic Type in your iOS app without writing any code. You have to do the following things:

  • Use plain text in the text view.
  • Tell the text view to support Dynamic Type in Interface Builder.
  • Choose a Dynamic Type text style as the text view’s font.

You can perform all these tasks from the attributes inspector in Interface Builder. Select your text view from the storyboard or xib file.

TextViewDynamicTypeIB

To use plain text in the text view, choose Plain from the Text menu.

Select the Dynamic Type checkbox to support Dynamic Type. If you build your project at this point, you will most likely get the following warning in Xcode:

Automatically Adjusts Font requires using a Dynamic Type text style

Text views in iOS Xcode projects initially use the system font, which does not support Dynamic Type. You must change the font to use a Dynamic Type text style. Click the button on the right side in the Font text field to open a popover to change the font.

TextViewFontPopover

Use the Font menu to select a text style. There’s a Text Styles group of menu items. Choose one of them as the font for the text view to support Dynamic Type.


Linker command failed with exit code 1

September 6th, 2018

Filed under: Xcode | Be the first to comment!

If you have done any programming in Xcode, you have come across the following error message when building your project:

Linker command failed with exit code 1.

This error message is frustrating because it tells you nothing besides the fact the linker command failed. You want to know what the real error is so you can fix it.

Find the Real Error

The first step to fixing a link error is to find out what the real error is. To find the real error, open Xcode’s report navigator by choosing View > Navigator > Show Report Navigator.

ReportNavigatorXcode9

The report navigator shows recent builds. If you have a linker error, there will be a red icon next to the build, which you can see in the screenshot. Select the build to see the build transcript, the list of steps Xcode took to build the project.

BuildLogHighlighted

If you have a link error, the build transcript will provide the real error message. Selecting a step from the build transcript makes a little button appear next to the step (The screenshot arrow is pointing to the button). Click that button to see a more detailed build log for that step.

The two most common link errors are undefined symbol and duplicate symbol.

Undefined Symbol Error

An undefined symbol link error occurs when the linker cannot find a data structure name or a function name. The most common cause of an undefined symbol error is a missing framework or library that contains the symbol. Some things to check to fix an undefined symbol error include the following:

  • Make sure you added the library or framework to your project.
  • Make sure the library or framework you added is in the Link Binary with Libraries build phase.
  • If you’re coding in Swift, make sure you imported the library or framework in your code with the import statement.

LinkBinaryBuildPhase

Header Files

If you’re coding in a language that uses header files, working with header files is another big cause of undefined symbol link errors. Make sure you include the header properly. A common error in C or C++ code is to include a header file that’s not a system header with angle brackets.

#include <MyHeader.h>

Xcode won’t be able to find MyHeader.h in this case because the header is not a system header. Use quotes to include the header file.

#include "MyHeader.h"

Search Paths

If none of the fixes I mentioned earlier work, you may need to add search paths for the library, framework, or header file so Xcode can find them. Xcode has a Search Paths collection of build settings, which you can access from the project editor.

SearchPathsBuildSettings

Duplicate Symbol Error

A duplicate symbol error occurs when the linker finds two or more data structures or functions with the same name. People using Xcode to learn C++ can run into this error if they create a command-line tool project. The command-line tool project includes a main.cpp file that has a main function in it. If you add a new file with its own main function and forget to remove the one Xcode created for you, you get a duplicate symbol error.

When trying to fix a duplicate symbol error, the first thing to check is that you haven’t defined a symbol twice in your project. Xcode’s find navigator helps you find all occurrences of a given symbol in your project.

FindNavigator

If you’re using external frameworks or libraries, make sure your symbol names aren’t duplicates of symbols in the framework or library. If you are using custom namespaces in your code, make sure you set up the namespaces properly.


Creating a Keyboard Input Accessory View

August 10th, 2018

Filed under: iOS Development | Be the first to comment!

In this article you’ll learn about keyboard input accessory views and how to add them to your iOS app to make text editing smoother.

Adding a keyboard input accessory view isn’t too difficult. Add a new xib file to your project for the view. Load the view from the xib file. Set the input accessory view to the view you loaded. There’s less than 10 lines of code involved.

What Is a Keyboard Input Accessory View?

A keyboard input accessory view is a view that appears above the iOS onscreen keyboard. The default iOS behavior is to have no input accessory view, but nothing is stopping you from adding one. A common use of an input accessory view is to show a toolbar with frequently used commands when typing text. The writing app Ulysses uses an input accessory view to insert Markdown characters into the text. If you were writing an iOS HTML editor, you could show a toolbar with buttons to insert the most common HTML tags.

Keep in mind that a keyboard input accessory view has type UIView?, which means the accessory view can be any kind of view. The accessory view does not have to be a toolbar. I’m using a toolbar as an example in this article because many apps have a toolbar as the accessory view.

Step 1: Create a Xib File for the Accessory View

The first step in creating a keyboard input accessory view is to create a new xib file and add it to your project. In Xcode choose File > New > File to add a new file to your project. The xib file templates are in the User Interface section under iOS.

AddNewiOSXibFile

The empty xib file is the best option for toolbars. If you want a custom view for the accessory view, you can choose View instead of Empty.

Step 2: Add the Accessory View to the Xib File

Take a toolbar (or whatever kind of view you want to use) from the object library and drag it to the canvas. The toolbar has one item in it. Add bar button items to the toolbar to fill the toolbar.

While you’re in Interface Builder go to the identity inspector and set the class for the File’s Owner object in the xib file.

FilesOwnerClassBlurred

Set the class to the view controller where you’re going to use the input accessory view. By setting the File’s Owner class to the view controller, you will be able to connect the controls in the input accessory view to IBActions in the view controller to respond to tapping the controls. In the xib file make a connection from the controls to File’s Owner to connect to IBActions.

Step 3: Load the Accessory View from the Xib File

Now it’s time to write some code to load the input accessory view. Call the Bundle class’s loadNibNamed function to load a xib file from the app bundle. Supply the name of the xib file as the first argument. Pass self as the owner argument, and pass nil as the options argument.

Calling loadNibNamed returns an array of the top level objects in the xib file. The input accessory view is the only top level object in the xib file you created so accessing the view is easy. Use the first property to access the first item in the array and grab the accessory view. The last code to write is to set the text view’s input accessory view to the view you loaded from the xib file. The following function sets up an input accessory view for a toolbar:

func setupInputAccessoryView() {
    let nibContents = Bundle.main.loadNibNamed("KeyboardInputAccessoryView", 
        owner: self, options: nil)
    if let toolbar = nibContents?.first as? UIToolbar {
        textView.inputAccessoryView = toolbar
    }    
}

Conclusion

There’s no more code to write. If you build your project and show the keyboard, the toolbar will appear at the top of the keyboard.

I noticed a bug in the iOS 11 Simulator where the toolbar would stay at the bottom of the screen after dismissing the keyboard. I did not run into this issue on iOS devices. If the toolbar is not going away, set the isHidden property of the input accessory view to true when dismissing the keyboard. Set isHidden to false when showing the keyboard.