iOS8 App Groups




App Groups
Though AppExtension and its containing Application is in same Bundle they cannot directly communicate or share data, Even direct communication between iOS Applications also not possible. Here, AppGroups helps you. App Groups are used to share data between AppExtension and its containing application, and also helps to share data between different iOS applications.
Lets see how you can use AppGroups in your application.

Step 1 Create XCode Project

Open Xcode and create new project with single view application template as shown in below figure. 
create-single-view-application
In the next step enter AppGroupDemo as a product name and also select Objective-C as a language as shown in below figure. 
appgroup-demo

Step 2 On AppGroups

Go to capabilities and switch on AppGroups It will ask you to add your Apple ID account that is enrolled in a Developer Program. 
developer-program
Select Add.. and provide your AppleID and Password.

Step 3 Add AppGroup

Add AppGroup by clicking on + button it will ask you to enter group name. 
enter-group-name
Click Ok after adding group name then it will add “App Groups” entitlement in your entitlements file, add “App Groups” entitlement to your Apple ID and add “App Groups Containers” entitlement to your Apple ID.

Step 4 Share Data using App Group

In this demo example I have added a UITextField to get data from user, which can be shared to other application. For that…
Add UITextField and UIButton in ViewController in storyboard. Add action method of UIButton. In that method declare NSUserDefault object and provide your AppGroup name as suite name. Set UITextField text as value for particular key lets say UserName.
  • - (IBAction)btnShareClicked:(id)sender
  • {
  • NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.tag.testGroupDemo"];
  • [myDefaults setValue:self.txtName.text forKey:@"UserName"];
  • self.txtName.text = @"";
  • }

Step 5 Display Shared Data in Other App

Create other Application to receive shared data.
*Note :- perform Step:1, Step:2 and Step:3 to create other application and add AppGroup in that application. You can see list of all AppGroups select your AppGroup from which you want to access data.
Now create NSUserDefault object and provide AppGroup name as suite name and you can access data using valueForKey as below.
  • NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.tag.testGroupDemo"];
  • self.lblUserName.text = [NSString stringWithFormat:@"Hello, %@", [myDefaults valueForKey:@"UserName"]];
I hope this blog is very helpful while App Groups in iOS 8. Let me know if you have any problem or questions regarding iOS 8 please comment here. I will reply you ASAP
Got an Idea of iPhone App Development ? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iOS App Development Company in India

Comments

Most popular posts

Implementing a Custom Back Button in Swift

Using Realm Mobile Database with Swift 4.0 (Insert, Update, Delete, List)

Chronometer Tutorial With Example In Android Studio

WKWebView advanced tutorial (catch JS events, access properties etc...) (Swift)

How to adjust image content mode using aspect fill, aspect fit and scaling

How to clear all activity stack in Android

From Swift to Javascript and Back

How To: Map, Reduce and Filter in Swift

Extension functions in Kotlin: Extend the Android Framework (KAD 08)

SQL injection in Android content providers and how to be protected