How to add extensions to iOS apps (part 2)

iOS Development, Tutorials

Learn how to add extensions to iOS apps to provide additional actions to your users without launching the app itself. In this part you’ll find out how to enable iOS app resource sharing and create a simple manager for saving and reading files.

In part one of our iPhone app development guide I wrote about:

  • creating the app project
  • programming the main view of iOS app
  • adding an extension to iOS app

Shared resources

To enable resource sharing, set a common data container that can be used by your targets (the main app and the extension).

Beside the marked target, choose the Capabilities bookmark. Select App Groups form the list and run it. Click the “+” button and write the container name.

App ios developer

Adding new container

After approving you should see the new entry.

How to add extensions to iOS apps

App groups

 

For now, we’ve just created a container for our iOS app targets. This whole process must be repeated for the main target of the app. So, on the left hand side, select AppExtensionSample from the list of targets and repeat these actions (write the same name when adding).

Local Storage Manager

Now that we have a dedicated container to save our data from both app targets, let’s move on to creating a simple manager for saving and reading data.

In the main app target, create new class called LocalStorageManager with the following code:


import UIKit

public class LocalStorageManager{
    
    static let sharedInstance = LocalStorageManager()
    
    private let sharedContainer: NSUserDefaults = NSUserDefaults(suiteName: "group.co.zaven.AppExtensionSample")!
    
    private var storedData: [String]! = []
    
    private init(){
        self.readData()
    }
    
    func readData() -> [String]{
        if let data = self.sharedContainer.objectForKey("FileList") as? [String] {
            self.storedData = data
        }
        return self.storedData
    }
    
    func insertData(fileName: String){
        self.storedData.append(fileName)
        self.sharedContainer.setObject(self.storedData, forKey: "FileList")
    }
    
    func removeData(index: Int){
        self.storedData.removeAtIndex(index)
        self.sharedContainer.setObject(self.storedData, forKey: "FileList")
    }
    
    func clearData(){
        self.storedData.removeAll()
        self.sharedContainer.removeObjectForKey("FileList")
    }
}

A simple singleton for using our local store. Pay attention to the sharedContainer declaration, where I refer to the name of the already created group container. By default, we would refer to the standardUserDefaults, that is allocated only to the one target in which we used it. This is how we’ll refer to a common container. Without firther obstacles, we’ll be able to save or read separated targets data.


Next on “How to add extensions to iOS apps”:

In the 3rd part of our iPhone app development guide, we will perform some final touches on the table implementation in the main application.


YOU CAN HAVE A LOOK AT THE FINISHED APP IN OUR GITHUB REPO HERE.


 

Paweł Kuźniak Technical Business Analyst, IT Operations

Paweł is technical analyst, developer and IT operations specialist at Zaven. He can solve a 3x3x3 Rubik's cube in a matter of seconds and you can find more complicated ones lying around on his desk (solved, obviously). His problem-solving attitude is reflected in his work, as he likes to keep himself busy with non-trivial puzzles.

Popular posts

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

Remember the multimedia craze in the early 2000s? It was everywhere, but did it truly revolutionize our lives? Probably not. Today, it feels like every piece of software is labeled "AI-powered." It's easy to dismiss AI chatbots in construction as just another tech fad.

Read more
Fears surrounding external support. How to address concerns about outsourcing software development?

Fears surrounding external support. How to address concerns about outsourcing software development?

Whether you’ve had bad experiences in the past or no experience at all, there will always be fears underlying your decision to outsource software development.

Read more
What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

Let’s make it clear: if the capabilities are there, a project is best delivered internally. Sometimes, however, we are missing certain capabilities that are required to deliver said project in a realistic timeline. These may be related to skills (e.g. technical expertise, domain experience), budget (hiring locally is too expensive) or just capacity (not enough manpower). What are good reasons for outsourcing software development?

Read more
Mobile Apps

Get your mobile app in 3 easy steps!

1

Spec out

with the help of our
business analyst

2

Develop

design, implement
and test, repeat!

3

Publish

get your app out
to the stores


back to top