IOS 8 excellent cross-app communication effect: Interpreting action Extensions

Source: Internet
Author: User


this article reprinted to http://mobile.51cto.com/iphone-464809.htm


The program extension was originally unveiled at the WWDC 2014 conference, a way to extend the functionality of iOS applications to other parts of the system, and to achieve better cross-application communication effects.



AD: Dry Goods come, don't wait! WOT2015 Beijing Station lecture ppt open download!






The application extension was originally unveiled at the WWDC 2014 conference, a way to extend the functionality of iOS applications to other parts of the system, and to achieve better cross-application communication effects.



For example, you can use the Today extension to create a feature that can be displayed in a notification hub, and the sharing extension helps users share information with the social network, and the action extension is intended to allow the user to execute the current content- Include displaying it in different ways or making changes to the content. In today's Getting Started guide, we'll learn how to create an action extension from scratch.



Although this article does not put too many requirements on the knowledge of the people, but I still suggest that readers can read some of the relevant materials, so that after the guidance of this article to more easily grasp more extensions to create knowledge.



WWDC speech: Creating an extension for iOS and OS X-Part I vs. II



Application Extensions Programming Guide



1. What do we create?



We are going to create a simple action extension called "Read it". This extension will use the text content as input and read it aloud using the speech synthesis API in the Avfoundation framework. I think the whole process is well suited as the core of this tutorial, because we don't need to introduce any third-party dependencies in the process, and we don't have any other problems that are difficult to handle.



The following is the effect of the extended functionality we get after the creation is complete. You can click here to download the results of this tutorial from GitHub.






2. Create an action extension



First Step: Project Setup



The first thing to do is start Xcode 6.1 or later, and then create a new project. Choose New > Project ... from the File menu in Xcode and select single View application from the list of templates.



Click Next and set a Sampleactionextensionapp name for our project. Enter a organization identifier and set the devices type to iphone. In this tutorial, we will use the programming language as objective-c.



Step two: Add a target



After you have completed the above project creation, you can then add a target to the action extension. From the File menu, select New > Target .... In the left panel, select Application Extension from the iOS option and click Next after the action Extension is selected.






Now set the product name to Readitaction. There are also other options to note, especially the action Type. We will discuss this topic in more detail later. Now click Finish to create the action extension.






Now the system will ask you if you intend to activate the Readitaction project. Click Cancel temporarily, as we will then use the run content to install the action extension instead of activating it directly.



Action type



The action extension is divided into two types, one with the user interface and the other without the user interface. You might find it strange-what is the real benefit of an action extension without a user interface? Let me give you an explanation.



Action extensions that do not have a user interface are targeted for content changes to the current project. An action extension, for example, can remove red-eye from a photo, and it does not require a user interface as a match at all. The content application can then be applied to this part of the changed content, in which case the modified photo material is completed.



An action extension with a user interface can be represented as a full-screen or format-table Form. The action extension target template is in full screen form, so we'll also use it as a design idea in the next creation process.



Step three: Implementation of the user interface



Now that we have completed the basic setup process, we are ready to create the user interface. Let's start with the application content.



First click on Main.storyboard under the Sampleactionextensionapp group in Project Navigator. In the right panel, select File Inspector and uncheck the use size classes. Note that if you are going to create a real application and do not need to support the ipad, using size classes (that is, the size class) may be the best choice.



Open the object Library below and drag the text view and toolbar to the view. In size Inspectoron on the right, set the box body of the text view to {x:8, y:20, width:304, height:288}. For toolbars, we also set them in size inspector, with the parameters {x:0, y:308, width:320, height:44}.



A bar button should be included in the toolbar. Select it, then set its style to plain in Attributes inspector and set its identifier to action.



As a finishing touches, we need to remove the default text content from the text view and replace it with "tap the action button to invoke Activity View controller." Then select ' Read it ' action and the text will is Read by our sample action extension. (Click the action button to invoke the active view controller.) Then select the ' Read it ' operation, which will be read aloud by our example action extension. )”



The user interface display for the view controller should now look like the following:






Of course, we can also leave the content application blank. After all, our goal is to build a set of exemplary application extensions, so the app does not need real execution capabilities. But I personally want to show you how easy it is to implement active controller calls from within an application, and I'm sure you can learn how to incorporate additional action extensions into your application.



When you click a button in the toolbar, a set of active view controllers is displayed, and we are able to invoke our action extension from here. Another reason to choose this approach is that if you're going to publish your extension to the App Store, it has to be part of a real application, and of course the app has the ability to pass Apple's official approval.



Fourth step: Current active view Controller



Next, we need to add part of the code to the VIEWCONTROLLER.M. The first is to create a set of skins for the text view in the class extension of the view controller, as shown in the following code.


    1. @interface Viewcontroller ()
    2. @property (nonatomic, weak) Iboutlet Uitextview *textview;
    3. @end


Create an action named Actionbuttonpressed, where we will initialize and display the Uiactivityviewcontroller instance and provide it to the user.


    1. -  (ibaction) actionbuttonpressed: (ID) sender { 
    2.      uiactivityviewcontroller *activityvc = [[uiactivityviewcontroller alloc]  initwithactivityitems:@[self.textview.text] 
    3.                                                                                   applicationactivities:nil]; 
    4.     [SELF PRESENTVIEWCONTROLLER:ACTIVITYVC  animated:yes completion:nil]; 
    5. } 


Let's go back to Main.storyboard by clicking Control and dragging the text view appearance scheme from the View Controller object under the view controller scene to the text view, Then choose TextView in the pop-up menu, which allows the text view to be docked to the text view.



To achieve docking with the action method, we need to select the Bar button in the tool and open the connections Inspector. Drag it from the selector in the sent actions to the View Controller object, and then select Actionbuttonpressed in the popup menu:.



Here the user interface of the application has been designed and formally delivered, and we can proceed with the action extension creation work.



Fifth step: Implementation of the user interface



In Project Navigator, expand the Readitaction group and click Maininterface.storyboard. You should notice that the storyboard is not blank, and that it already contains a subset of the user interface components. We can make use of some of them, but the specific image view is not helpful. So select the image view and remove it by pressing the DELETE key.



Next open the object library and add a set of text views in the navigation bar below. Change its frame to {x:8, y:72, width:304, height:300}. Finally, double-click the title view of the navigation bar and set the caption content to "Text reader".



The sixth step: the realization of Actionviewcontrolle



In this step, we need to take care of the implementation of the action extension. Select ACTIONVIEWCONTROLLER.M in Project Navigator and implement the following changes to it.



The following import statement adds an import statement to the Avfoundation framework so that we can use its speech synthesis API in its own action extension.



1 @import avfoundation;



In the class extension of the Actionviewcontroller class, remove the ImageView appearance from it and add it to the new look we had previously added to the text view.


    1. @interface Actionviewcontroller ()
    2. @property (nonatomic, strong) Iboutlet Uitextview *textview;
    3. @end


In addition to this, we also need to make the following changes to the Viewdidload method in the Actionviewcontroller class.


  1. -(void) Viewdidload {

  2. [Super Viewdidload];

  3. //Get the items we intend to process from the extended background information.

  4. //In our action extension, we only need one input item (that is, text), so we use the first item in the array.

  5. Nsextensionitem *item = self.extensioncontext.inputitems[0];

  6. Nsitemprovider *itemprovider = item.attachments[0];

  7. if ([Itemprovider hasitemconformingtotypeidentifier: (NSString *) Kuttypeplaintext]) { 

  8. //This is a piece of plain text!

  9. __weak Uitextview *textview = Self.textview;

  10. [Itemprovider loaditemfortypeidentifier: (NSString *) Kuttypeplaintext options:nil completionhandler:^ (NSString *item , Nserror *error) {

  11. if (item) {

  12. [[Nsoperationqueue Mainqueue] addoperationwithblock:^{

  13. [TextView Settext:item];

  14. //Set up speech synthesis and start

  15. Avspeechsynthesizer *synthesizer = [[Avspeechsynthesizer alloc]init];

  16. Avspeechutterance *utterance = [Avspeechutterance speechUtteranceWithString:textView.text];

  17. [Utterance Setrate:0.1];

  18. [Synthesizer speakutterance:utterance];

  19. }];

  20. }

  21. }];

  22. }

  23. }




The entire implementation process is very easy to understand. In Viewdidload, we get the input text content, assign it to the text view, and then create a speech synthesis object that can read it aloud.



Seventh Step: Configure the action extension



While we are nearing the end of the entire project creation process, there are still some detail issues that need to be focused. First, we need to connect the text view in the storyboard with the look that we've created before.



Open Maininterface.storyboard and connect the text view to the image scene, just as we did before in Main.storyboard.



Next we also need to specify the type of data that can be supported for the action extension. In this case, we only need to support plain text data. Expand the supporting Files group and select Info.plist. In Info.plist, follow the nsextension > nsextensionattributes > Nsextensionactivationrule navigation Process, Finally, the type of nsextensionactivationrule is changed from string to dictionary.



In the dictionary that has been expanded, click the + button next to it to add a new sub-key. Set its name to Nsextensionactivationsupportstext, the type to Boolean, and the value to Yes. In this way, we can ensure that our action extension is only displayed when the input item contains textual content.



Still in Info.plist, we're going to change the bundle Display name to read It. This will look clearer. The results of the settings shown in the relevant sections of the Info.plist file are as follows:






Eighth step



As a finishing touch, you can add an icon to the action extension. In Project Navigator, choose the project and select the Readitaction target under the target. Under the General tab in the app Icons and Launch images section, click the Use Asset Catalog next to App Icons source. On cue, we need to click Migrate next. Then use the asset catalog carefully and drag it to the iphone App iOS 7,8 60pt 2x location.






Complete the application build and run steps to see if everything works as expected. However, there is one more concern: if the sound icon is not displayed properly within the action extension, you need to make sure that the primary images.xcassets file is correctly copied to the extended target.



To do this, we need to select the project in Project Navigator and choose the readitaction target from the targets list. Open the Build Phases tab at the top of the screen and expand the Copy Bundle resources step. If the Images.xcassets file does not appear in the resource list, click the small plus sign to add it to the list manually.



3. Operation and Testing



The last thing to do is to run the application and try out the performance of its various functions. The two images shown below are the extended skins in operation. You can also try calling the active view controller through the Notepad app and have our extension read the text content that was previously recorded. In addition, we may as well open the activity list in the photo app, and you will find that your extension is not listed. Yes, that's exactly what we've set up for the activation rule.






Summarize



In today's tutorial, you learned how to build a simple action extension. We also covered some basic knowledge of how to use the speech synthesis API in the Avfoundation framework. If you have a lift to create more extensions, you can also click here to see the Today Extension Creation Guide brought by Cesar Tessarin.



If you have any questions or suggestions about the content of this article, please share it with us in the comments section below.



English Original: http://code.tutsplus.com/tutorials/ios-8-how-to-build-a-simple-action-extension--cms-2279



IOS 8 excellent cross-app communication effect: Interpreting action Extensions


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.