Thursday, 27 April 2017

Upload an app with share extension to App store in iOS

I discussed in Sharing data between app and a share extension blog. In this you will find what is app extension and how to share data between an app and a share extension .

After adding a new target in our app for a share extension and running it on device , the next question comes in mind is that which app id we will use for uploading an app to iTunes connect. An app extension is bundled with the app when we create a binary of the app and installed when we install the app on the device.

Because each target have separate app id, so we need to create a separate app id for the target in developer site and have to create separate provisioning profile for each target.

So follow these steps to create a share extension and upload it on iTunesConnect:

After adding a new target for the share extension in the project, set the bundle identifier for this new target in target setting in Xcode and then visit the developer site to create a new app id for this new target.

1) In developer site, create a new app id  for your share extension and enter valid details for it like     bundle identifier which you added in target setting of the share extension in Xcode.

2) After creating app id for the share extension , create the provisioning profile for the new app id. Here you should create development , adhoc and app store provisioning profile.
     Download all necessary provisioning profile and switch back to Xcode.

3) In Xcode , set the downloaded provisioning profile for the share extension by selecting the share extension target and set developer and app store provisioning profile here.
     Also make sure you have also set the correct provisioning profile for your main app targets.

4)Now it's time to create an app on iTunes in order to upload it for reviewing by Apple. So visit     iTunesConnect and login with valid credentials.

5) This is the main answer to the question,"Which app id will be used for creating an app on iTunes , which has a target for share extension". As we discussed earlier, your share extension is bundled with main app so , we create an app with the app id of your main app not with the app id of share extension.
 So create an app with  app id of your main iOS app and fill all necessary information here to create an app and save it.

6) Now in Xcode , if you have setup all the things correctly, it's time to create an archive of the     project and upload it to iTunesConnect. So select project from project navigator window in Xcode      and create an archive from Product-> Archive menu.
  Here as you follow the steps, you will see your share extension is bundled with the main app. and that's it.

Reference:developer.apple.com

Sunday, 23 April 2017

Share Data between app and share extension in iOS

App extension provide  a way to perform core functionality of the app, from the activity view controller. For example , if we are seeing an photo in Photos app, then if we want to share this photo on Facebook, we can open it with  Facebook by choosing Facebook form the default activity view controller of the Apple. 
We can also list our app in the default list of the sharing app in activity view controller provided by the Apple by default.
I am not discussing the fundamental of how to create a share extension in iOS app. Today I am going to discuss how to share the data between app and share extension. 

Because iOS app is sandboxed, so to  have common access to data between two targets, we have to create a shared container, in which both targets have read/write access.

1) To create a shared container, enable the group service for the app from the Xcode setting and give the name of group in reversed domain. example: group.appIdentfier

2) Now also enable the group service for the extension target. Here don't create an another group, just choose the group we have created last.

3) Now switch to developer account and create separate provisioning profiles for app and share extension with group configured.

Now we have a shared container, so to have common access to data(like login preferences ) in both targets(app and share extension) we can store these kinds of data in shared UserDefaults. 

Write data in shared UserDefaults: 
if let defaults = UserDefaults.init(suiteName:
        "sharedGroupName"){
        //read and write to UserDefaults       
        }


To read and write files into shared document directory, get the url of document directory and perform task, which is similar to as we do with default file manager.

if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppConstants.kSharedGroupName){
 }

Here url is the path of the document directory, You can create file and folder at this path.

        





Thursday, 6 April 2017

Search content using UISearchBar


When we have a large number of data to display in UITableView, then looking for a particular  data is very tedious task. To overcome this problem we can enable a search functionality on the tableview. As I used this in one of my app.

UISearchBar provide a functionality to search the content we look for from the specific view (here it is table view).It is very simple to use UISearchBar in an app. Steps are as follows:

1) Add UISearchBar property in your view controller's interface file  and set its delegate

         self.searchBar = [[UISearchBar alloc]init];
        self.searchBar.showsCancelButton = YES;
        self.searchBar.delegate = self;
        [self.searchBar becomeFirstResponder];

2)I am displaying the search bar in navigation bar as:
        self.navigationItem.titleView = self.searchBar;


3) Implement the delegate method of the  UISearchBar 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    //hide the searchBar, and reload the tableview to show original data
    isSearching = NO;    
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    //start the search
    NSLog(@"search btn cliked");
    [searchBar resignFirstResponder];
    [self searhTableList];
    
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"search should begin editing");
    isSearching = YES;
    return YES;
}

Here when user type some text to search and tap the search button, we are searching the table list and then if any matches occur we add the specific row's data to a mutable array and we reload the table view to show the filtered result.

To store the filtered result, declare a mutable array in interface file , here it is filteredArrData,


-(void)searhTableList{
    NSString *searchString = self.searchBar.text;
   
    for (NSString *data in self.arr_tableData) {
         
        NSRange range = [data rangeOfString:searchString options:NSCaseInsensitiveSearch];
         
        NSComparisonResult result = [data compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:range];
        
        if (result == NSOrderedSame) {
            [self.filteredArrData addObject:data];
        }

    }

    if (self.filteredArrData.count == 0) {
         //show an alert, no results found
        
    }else{
         //reload the tableview
          
     }

That 's it. Here I am showing how to implement search on table's data.I am not showing how to use same table view to show the search result and original data(full data to show in table view).