Getting Started with WidgetKit Create Widgets for iOS and iPadOS by Sagun Raj Lage Prakshapan Shrestha

161 7 0
Getting Started with WidgetKit Create Widgets for iOS and iPadOS by Sagun Raj Lage Prakshapan Shrestha

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Develop handy, UI/UX friendly and eye-pleasing widgets using the brand new WidgetKit. Apple’s brand new widgets allow iOS users to work with their favorite apps in the home screen of their iPhone or iPad without even opening the app! Join us in this exciting journey as we explore the APIs introduced in Apple’s WidgetKit framework. You''ll dive into the human interface guidelines (HIG) for creating widgets and review the recommendations Apple gives to developers for developing widgets with intuitive, easy-to-learn, and consistent user interfaces. In addition, you’ll take a look at some SwiftUI views that are useful not only in creating widgets for iOS apps, but also for creating iOS apps themselves. You’ll put everything you learn into practical application by actually writing code and creating widgets. Get a clear view of how everything works so that you’re able to incorporate widgets into your real-world projects authentically and successfully. What You''ll Learn Configure widgets and make them talk to APIs using URLSession Work with timelines and event handling in widgets Fetch content from a remote server and display the data in a widget Make content dynamic both remotely and locally

Getting Started with WidgetKit Create Widgets for iOS and iPadOS — Sagun Raj Lage Prakshapan Shrestha Getting Started with WidgetKit Create Widgets for iOS and iPadOS Sagun Raj Lage Prakshapan Shrestha Getting Started with WidgetKit: Create Widgets for iOS and iPadOS 45 Sagun Raj Lage Golmadhi, Nepal Prakshapan Shrestha Tokha, Nepal ISBN-13 (pbk): 978-1-4842-7041-7 https://doi.org/10.1007/978-1-4842-7042-4 ISBN-13 (electronic): 978-1-4842-7042-4 10 Copyright © 2021 by Sagun Raj Lage and Prakshapan Shrestha 11 12 13 14 15 This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed 16 17 18 19 Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark 20 21 22 The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights 23 24 25 26 While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein 27 28 29 30 Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Aaron Black Development Editor: James Markham Coordinating Editor: Jessica Vakili 31 32 33 34 35 Distributed to the book trade worldwide by Springer Science+Business Media New York, NY Plaza, New York, NY 10014 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@ springer-sbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation 36 37 For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com 38 39 40 Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales 41 42 43 Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-­1-­4842-­7041-­7 For more detailed information, please visit http://www.apress.com/source-­code 44 Printed on acid-free paper To my father, late Shree Ram Lage, my role model, my inspiration, my pride, who taught me what duties and responsibilities mean I hope I am making you proud, Baba To my mother, Jamuna Laxmi Sitikhu (Lage), my support system, who has befriended struggles and has taught me to bravely face challenges I know you’ve made many sacrifices to make me who I am today, Mamu To my little sister, Sarina Lage, who has always been there for me in my highs and lows, joys and sorrows And I know you’ll be there for me in the days to come too I love you To my grandparents, Ganga Laxmi Sitikhu and Narayan Bhakta Sitikhu, who have always showered me with their precious blessings and unconditional love To my uncles and aunts, Narayan Prasad Sitikhu and Ram Devi Sitikhu, Sunil Kharbuja and Srijana Kharbuja, Krishna Prasad Bohaju and Rejina Bohaju, for wholeheartedly providing their guidance and love to me To the person who lit the spark of interest in computers, gadgets, and technology in me since my childhood – my uncle, Jibesh Sitikhu I wouldn’t be where I am today without your contributions, your teachings, your talks and without breaking down your computer many times And to my aunt, Rajyashwori Sitikhu You’re an epitome of kindness and affection 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 AU1 64 65 66 67 68 69  70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 To my lovely cousins, Bigyan Sitikhu, Sachin Bohaju, Binam Sitikhu, Salin Bohaju, Shrijal Kharbuja, Jibisha Sitikhu, Swornim Kharbuja, Jibika Sitikhu, Raunak Sitikhu, and Raunika Sitikhu You have filled my life with joy I love you all To my brother from another mother, Kshitij Raj Lohani, who has always helped me selflessly and who allowed me to access and use his personal MacBook Pro in the United States, from Nepal, for almost a year I was able to join an iOS bootcamp, write blogs on iOS development, and write this book just because he allowed me to use his computer as I couldn’t afford one To all my teachers, seniors, mentors, friends, and juniors You are my gems I feel blessed to have you in life Thank you for everything! —Sagun Raj Lage 85 86 87 To my dear mother —Prakshapan Shrestha Table of Contents 88 About the Authors�������������������������������������������������������������������������������vii 89 About the Technical Reviewer�������������������������������������������������������������ix 90 Before You Begin… �����������������������������������������������������������������������������xi 91 Chapter 1: Getting Familiar with WidgetKit in a Flash�������������������������1 92 Background�����������������������������������������������������������������������������������������������������������1 93 Hello, WidgetKit!����������������������������������������������������������������������������������������������������2 94 Summary��������������������������������������������������������������������������������������������������������������3 95 Chapter 2: SwiftUI, Human Interface Guidelines, and Widget Family���������������������������������������������������������������������5 96 97 SwiftUI������������������������������������������������������������������������������������������������������������������5 98 Basic SwiftUI Views for Widgets����������������������������������������������������������������������7 99 Human Interface Guidelines��������������������������������������������������������������������������������14 100 Widget Family�����������������������������������������������������������������������������������������������������16 101 Summary������������������������������������������������������������������������������������������������������������18 102 Chapter 3: Writing Your First Widget��������������������������������������������������19 103 Widget Extension������������������������������������������������������������������������������������������������22 104 TimelineEntry������������������������������������������������������������������������������������������������������24 105 TimelineProvider�������������������������������������������������������������������������������������������������26 106 placeholder(in:)����������������������������������������������������������������������������������������������29 107 getSnapshot(in:completion:)��������������������������������������������������������������������������30 108 getTimeline(in:completion:)���������������������������������������������������������������������������33 109 v Table of Contents 110 Developing Your Widget’s UI��������������������������������������������������������������������������������35 111 WidgetConfiguration�������������������������������������������������������������������������������������������39 112 Summary������������������������������������������������������������������������������������������������������������44 113 Chapter 4: Making Widgets Configurable and Interactive������������������45 114 Let’s Get Started�������������������������������������������������������������������������������������������������45 115 Giving Widgets the Power to Talk to API��������������������������������������������������������������58 116 Allowing Users to Configure Widgets������������������������������������������������������������������62 117 Create and Configure a SiriKit Intent Definition File��������������������������������������63 118 Switch to IntentConfiguration������������������������������������������������������������������������69 119 Talk to the API and Display Fresh Information in Widgets�����������������������������75 120 Time to Put Your Widgets to the Test!������������������������������������������������������������83 121 Navigating to the Relevant Screens of the App Through Tap Targets������������������84 122 Addition of Tap Target in Small Widget����������������������������������������������������������86 123 Addition of Tap Target in Medium Widget������������������������������������������������������91 124 Addition of Tap Target in Large Widget��������������������������������������������������������106 125 Summary����������������������������������������������������������������������������������������������������������110 126 Chapter 5: Fetching Configuration Options���������������������������������������111 127 Getting Started��������������������������������������������������������������������������������������������������112 128 Time to Create a SiriKit Intent Definition File����������������������������������������������������121 130 Setting Up IntentHandler to Fetch Top Trends and Send Them to the Widget�������������������������������������������������������������������������������������������125 131 Switching to IntentConfiguration����������������������������������������������������������������������132 132 Create an IntentTimelineProvider����������������������������������������������������������������132 133 Make the Switch to IntentConfiguration������������������������������������������������������140 134 Test – Test – Test!���������������������������������������������������������������������������������������������141 135 Summary����������������������������������������������������������������������������������������������������������141 136 Index�������������������������������������������������������������������������������������������������143 129 vi About the Authors 137 Sagun Raj Lage started his professional career in software development as a Full Stack Web Developer and later moved into developing iOS applications He has been a part of development teams on applications used in fields such as transportation, multimedia, shopping, finance, astrology, and management He is actively involved in organizing developer events and in contributing as a mentor and tutor in programming bootcamps Apart from software development and programming, he enjoys reading and writing blogs, music, graphic design, and video editing.  138 Prakshapan Shrestha is an entrepreneurial app developer with years of iOS development experience He devoutly follows the latest tools and technologies that make a developer's life easier and actively helps out budding developers Aside from software development, Prakshapan enjoys hiking and heading his recent venture, Pregasathi, which provides new families in need of baby products with help 147 vii 139 140 141 142 143 144 145 146 148 149 150 151 152 About the Technical Reviewer Felipe Laso is a Senior Systems Engineer working at Lextech Global Services He’s also an aspiring game designer/programmer You can follow him on Twitter at @iFeliLM or on his blog ix 153 154 155 156 Before You Begin… 157 Before you start exploring the beauty and power of WidgetKit through this book, you will need to make sure you have the following prerequisites set up: 158 A Mac running macOS Catalina (version 10.15.4) or later: However, we recommend a Mac running macOS Big Sur (version 11) or later as the code was tested on that version 160 Xcode 12 or later: Xcode is the primary tool used to develop apps for the Apple ecosystem You can download the latest version of Xcode from Apple’s developer site.1 We recommend using Xcode 12.4 or later as the code was tested on that version 164 Swift or later: Since the new versions of Xcode ship with the updated versions of Swift, you need not worry about this 169 Simulators and devices with iOS 14 or later installed since the WidgetKit framework, the framework you will use to develop widgets, is only supported from iOS 14 172 • • • • 159 161 162 163 165 166 167 168 170 171 173 174 https://developer.apple.com/xcode/ xi Chapter 408 Fetching Configuration Options Switching to IntentConfiguration 415 Right now, if you build and run your project, you will see that long pressing your widgets does not give the option to Edit Widget It is because you have still not made necessary preparations to replace StaticConfiguration with IntentConfiguration And to use IntentConfiguration, you will need a different timeline provider than the one you are using right now Just follow the steps given to make “the big switch” to IntentConfiguration 416 Create an IntentTimelineProvider 409 410 411 412 413 414 417 418 419 As mentioned previously, the first step to switch to IntentConfiguration is to set up an IntentTimelineProvider Go through the given steps to set it up: Right-click the Provider folder of the TwitterTrendsWidget folder, click New File…, and create a new Swift file named TwitterTrendsIntentProvider.swift Make sure the TwitterTrendsWidgetExtension target is checked at the bottom of the dialog box before creating the file 420 421 422 423 424 425 426 427 428 Note  You could use the existing TwitterProvider.swift file instead of creating TwitterTrendsIntentProvider.swift, but for better clarity, we recommend you to create TwitterTrendsIntentProvider.swift Open TwitterTrendsIntentProvider.swift and replace the existing code content with the code given in Listing 5-3 429 430 431 132 Chapter Fetching Configuration Options Listing 5-3.  Creating TwitterTrendsIntentProvider that conforms to IntentTimelineProvider 432 433 import SwiftUI import WidgetKit 434 struct TwitterTrendsIntentProvider: IntentTimelineProvider { 436 } 437 435 In Listing 5-3, a struct named TwitterTrendsIntentProvider that conforms to IntentTimelineProvider is created As soon as you add the code, Xcode will ask if you want to add protocol stubs Click Fix and add them to see two typealiases, Entry and Intent, added to the struct 438 In TwitterTrendsIntentProvider, replace the type placeholder of Entry with TweetWidgetEntry, a type of timeline entry Also, replace the type placeholder of Intent with TrendsIntent, whose name was generated from the custom intent, Trends, you had created in TwitterTrends.intentdefinition 444 Now your code should look like the code in Listing 5-4 450 439 440 441 442 443 445 446 447 448 449 Listing 5-4.  TwitterTrendsIntentProvider after adding Entry and Intent types 451 452 import SwiftUI import WidgetKit 453 struct TwitterTrendsIntentProvider: IntentTimelineProvider {     typealias Entry = TweetWidgetEntry     typealias Intent = TrendsIntent } 455 454 456 457 458 133 Chapter Fetching Configuration Options You will still see Xcode asking you to add protocol stubs Add them too Then, placeholder(in:), getSnapshot(for:in:completion:), and getTimeline (for:in:completion:) methods will get generated 459 460 461 462 For implementing the placeholder(in:) and getSnapshot(for:in:completion:) methods of TwitterTrendsIntentProvider, you can copy the code from the placeholder(in:) and get Snapshot(for:in:completion:) methods of TwitterProvider in TwitterProvider.swift as the same implementations will work 463 464 465 466 467 468 469 The getTimeline(for:in:completion:) method of TwitterTrendsIntentProvider is different than the getTimeline(in:completion:) method of TwitterProvider as TwitterTrendsIntentProvider conforms to IntentTimelineProvider, but TwitterProvider conforms to TimelineProvider And the getTimeline(for:in:completion) method of TwitterTrendsIntentProvider allows you to access the data from the configuration of the widget 470 471 472 473 474 475 AU2 476 477 478 In the current scenario, you will access the selected trend from the widget’s configuration and use it to perform the API call to fetch tweets related to that selected trend In case there isn’t a selected trend, you will make the widget fetch the tweets related to the first trend in the array of the trends fetched from Twitter’s API. And you will everything in getTime line(for:in:completion:) 479 480 481 482 483 484 485 486 134 Chapter Fetching Configuration Options Before you start working on getTimeline(for:in: completion:), let’s first create a method, create TimelineFromTweets(response:), to create a timeline from the tweets that will be fetched from the API. Then, you can call this method from getTimeline(for:in:completion:) This will make the code readable and easy to understand Copy the code given in Listing 5-5 and paste it in TwitterTrendsIntentProvider 487 488 489 490 491 492 493 494 495 Listing 5-5.  Creating the createTimelineFromTweets(response:) method 496 497 func createTimelineFromTweets(response: Result) -> Timeline {         // a         let currentDate = Date()         // b         let refreshDate = Calendar.current.date(byAdding: minute, value: 30, to: currentDate)!         // c         var entry = TweetWidgetEntry(date: refreshDate, statuses: Tweets.dummyTweets, trendTitle: "")         // d         var timeline = Timeline(entries: [entry], policy: after(refreshDate)) 498         // e         switch response {         case let success(tweets):             // f              entry = TweetWidgetEntry(date: refreshDate, statuses: tweets.statuses, trendTitle: tweets.title) 511 499 500 501 502 503 504 505 506 507 508 509 510 135 512 513 514 515 516 Chapter 517 518 519 520 521 522 523 524 525             timeline = Timeline(entries: [entry], policy: after(refreshDate))             return timeline         case let failure(error):             // g             print(error.localizedDescription)             return timeline         }     } In Listing 5-5, you created a method createTimeline FromTweets(response:) that has response as the parameter and returns a timeline In it, the following things have been done: 526 527 528 529 530 a At the beginning, the current date is stored in currentDate b Now, the date after adding 30 minutes to currentDate is generated and stored in refreshDate You will use it later to set up the widget’s refresh policy c In this step, the TweetWidgetEntry timeline entry is created by passing refreshDate, dummy tweets, and an empty title string to its initializer Then, it is stored in entry d Now a timeline is created using entry and by setting the refresh policy to make the widget request a new timeline after 30 minutes In this way, the timeline is created e As response may either have a success value or a failure value, it is put through a switch case If response has a success value, it uses 531 532 533 534 535 536 537 538 539 540 541 542 543 544 Fetching Configuration Options 545 546 136 Chapter Fetching Configuration Options tweets received with success and creates a new TweetWidgetEntry by using tweets’ title and statuses properties Then, the value of the entry variable is replaced by this new entry And the value of the timeline variable is also replaced with a new timeline created using the new value of entry, and therefore the timeline is returned 547 548 549 550 551 552 553 554 f In case response contains failure, the error variable is accessed, and its value is printed into the console Also, since the method must return a timeline, the timeline variable with dummy tweets and empty title (that you created in steps “c” and “d”) is returned 555 Now it’s time to start working in the getTimeline( for:in:completion:) method Replace the existing getTimeline(for:in:completion:) method with the code given in Listing 5-6 561 556 557 558 559 560 562 563 564 Listing 5-6.  Implementation of the getTimeline(for:in:completion:) method func getTimeline(for configuration: TrendsIntent, in context: Context, completion: @escaping (Timeline) -> Void) {         // a         if let trend = configuration.selectedTrend {             let selectedTrend = Trend(name: trend displayString, query: trend.identifier!)             TwitterTrendsAPI.getTweets(on: selectedTrend) { response in 137 565 566 567 568 569 570 571 572 573 574 575 Chapter 576 577 578 579 580 581 582 583 584 585                 completion(createTimelineFromTweets(response: response))             }         } else { // b             TwitterTrendsAPI.getLatestTweets { response in                 completion(createTimelineFromTweets(response: response))             }         }     } In Listing 5-6, the implementation of the getTime line(for:in:completion) method is given The following things take place in Listing 5-6: 586 587 588 589 Fetching Configuration Options 590 591 592 593 594 595 596 a This method checks for two cases – the first is the case when there exists a value of selectedTrend in configuration, and the second one is the case when selectedTrend does not contain any value The selectedTrend property is the parameter that you had created in TwitterTrends.intentdefinition in the previous sections of this chapter In the current step, the code checks if users have already selected a trend from their configuration screen Thus, if users have selected a trend, meaning that there exists a value in selectedTrend, it is stored in trend, and a new instance of Trend is created by using the trend’s displayString and identifier 597 598 599 600 601 602 Then, the getTweets(on:completion:) method of TwitterTrendsAPI is called to fetch the tweets related to that trend When a response is received, 603 604 605 138 Chapter Fetching Configuration Options the completion handler of the getTimeline(for:in: completion:) method is called by passing the call to createTimelineFromTweets(response:) as its argument 606 The createTimelineFromTweets(response:) takes the response received from the API call as an argument, generates a timeline, and returns it At last, the completion handler of getTimeline(for: in:completion:) uses that timeline, and a timeline is created for the widget 610 607 608 609 611 612 613 614 615 b But in case users have not selected any trend (or there is no value in the selectedTrend property of configuration), the getLatestTweets(completion:) method of TwitterTrendsAPI is called This method first fetches all the trends from Twitter’s API, selects the first trend from the API’s response, fetches the tweets related to that trend, and returns it as response On receiving the response, the completion handler of the getTimeline(for: in:completion:) method is called by passing the call to createTimelineFromTweets (response:) as its argument 616 617 618 619 620 621 622 623 624 625 626 627 628 Then, the createTimelineFromTweets(response:) uses the response received from the API call as an argument, generates a timeline, and returns it At last, the completion handler of getTimeline(for: in:completion:) uses that timeline, and a timeline is created for the widget 629 630 631 632 633 634 139 Chapter In this way, you created an IntentTimelineProvider, which is necessary for switching to IntentConfiguration 635 636 637 638 Fetching Configuration Options Make the Switch to IntentConfiguration 644 Now you are all set to make the switch to IntentConfiguration If you open the TwitterTrendsWidget.swift file of the Widget folder in the TwitterTrendsWidget folder, you will see that StaticConfiguration has been used right now in the body of TwitterTrendsWidget In TwitterTrendsWidget, create a variable, dynamicConfiguration, by using the code given in Listing 5-7 645 Listing 5-7.  Creation of a dynamicConfiguration variable 639 640 641 642 643 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 var dynamicConfiguration: some WidgetConfiguration {          IntentConfiguration(kind: kind, intent: TrendsIntent self, provider: TwitterTrendsIntentProvider()) { entry in             LargeWidgetView(tweets: entry.statuses, title: entry.trendTitle)         }         .supportedFamilies([.systemLarge])         .configurationDisplayName("Tweets")         .description("Tweets Trending Today")     } In Listing 5-7, a variable named dynamicConfiguration is created which returns an IntentConfiguration initializer It uses the value of TwitterTrendsWidget’s kind variable, TrendsIntent as its intent, and the initializer of TwitterTrendsIntentProvider as its timeline provider All the other lines of code are similar to that of the StaticConfiguration that exists in the body of TwitterTrendsWidget Now, replace the body of TwitterTrendsWidget with the code given in Listing 5-8 140 Chapter Fetching Configuration Options Listing 5-8.  Using dynamicConfiguration in the body of TwitterTrendsWidget 664 665 var body: some WidgetConfiguration {         dynamicConfiguration     } 666 The code given in Listing 5-8 replaces the StaticConfiguration with the dynamicConfiguration variable that contains IntentConfiguration Well done! You have switched to IntentConfiguration 669 667 668 Test – Test – Test! 670 671 672 Uninstall all the existing installations of TwitterTrends in your device or simulator Now, select the TwitterTrends scheme and run it To test your widget, add the TwitterTrends widget to your homescreen and try editing it You should see a configuration screen similar to the one shown in Figure 5-4 Tap on Choose to see a screen where you can see a list of trends fetched from Twitter This screen will look similar to Figure 5-5 From that screen, select any trend and go back to your homescreen Now, in your widget, you should see the tweets related to that particular trend you had selected Awesome job! We hope you had fun Summary 673 674 675 676 677 678 679 680 681 682 683 Congratulations on making it this far! In this chapter, you learned how you can make your widget fetch data from a server so that you can use those data as configuration options for the parameter in your configurable widget Some part of this chapter must have been like a revision of the 141 684 685 686 687 Chapter 688 689 690 691 692 693 694 695 696 Fetching Configuration Options previous chapters to you And we hope you enjoyed following along and learning If you have any confusions, please check out the final version of the code by opening the final project folder named TwitterTrendsFinal in TwitterTrends.zip Now you are able to create any kind of widgets – be it a widget that can or cannot be configured or a widget whose parameter has hard-coded configuration options or dynamic configuration options fetched from a server You have mastered them all We wish you luck for the future Happy coding! 142 Author Queries Chapter No.: 0005120290 Queries Details Required AU1 Please check if “step of Creating and Configuring SiriKit Intent Definition File” is correct as is AU2 Please check if instances of “getTimeline(for:in:completion)” should be changed to “getTimeline(for:in:completion:)” Author’s Response Index 10 11 12 13 14 15 16 17 18 19 20 21 22 23 A, B, C H, I, J, K, L CategoriesExtension.swift, 82, 87 createTimelineFromTweets(respo nse:) method, 135, 139 handleLinks(for:) method, 89, 101, 102 HStack, 9–12, 107–109 Human Interface Guidelines (HIG), 3, 14–16 D, E DateHelper.getDayAndMonth InNumbers() method, 59 Deep linking, 84, 89, 91, 94, 101 detail(with:category:) method, 95–97, 109 F fetchOnThisDayData(for:complet ion:) method, 76–79 fetchOnThisDayData(with:) method, 58, 60, 75–77 G getSnapshot(in:completion:) method, 30, 33, 71 getTimeline(for:in:completion:) method, 71, 134, 137, 139 24 25 26 27 28 29 M, N 30 @main property, 39, 41 31 O 32 onOpenURL(perform:) method, 86, 89, 91, 98 OnThisDay API, 59 detail screen, 49 folder structure, 55, 57 homescreen, 46, 47 large-size widget, 52 medium-size widget, 52 options, 53 Provider.swift, 60 screenshot display, 51 select events, 48 small-size widget, 51 types of events, 58 users, 54 © Sagun Raj Lage and Prakshapan Shrestha 2021 S R Lage, P Shrestha, Getting Started with WidgetKit, https://doi.org/10.1007/978-1-4842-7042-4 143 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 INDEX 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 OnThisDayApp.swift, 55, 56, 90 OnThisDayWidgetExtension, 61, 83 OnThisDayWidget.swift API, 75 create variable, 80 WidgetView, 81 IntentConfiguration, 69, 74 category property, 73 intent types, 70 methods, 71 WidgetEvent timeline entry, 74 StaticConfiguration, 62, 63 Categories enum, 67 intent definition file, 64 parameter, 66 SiriKit, 63 P, Q, R placeholder(in:) method, 29, 30 provideSelectedTrendOptionsColl ection(for:with:) method, 128, 130 S supportedFamilies(_:), 17, 18 SwiftUI, declarative approach, definition, lifesaver, views button, HStack, 9, 11 144 image, text, VStack, 11, 12 ZStack, 12, 13 T, U, V Tap target, 85 large widget, 106 medium widget, 91–95, 98, 100, 103, 105, 107–109 small widget, 86–91 TimelineEntry, 24–26, 29, 37, 57, 121 TimelineProvider date property, 26 definition, 26 getSnapshot, 30, 32, 33 getTimeline, 33–35 placeholder, 29, 30 SmallWidgetDataProvider, 26, 28, 29 TrendsView.swift, 120 TweetsView.swift, 120 TwitterTrends, 112 create file, 121 create parameter, 123, 124 dynamicConfiguration, 140 folder structure, 119 homescreen, 113 IntentConfiguration, 132 intent definition file, 122 IntentTimelineProvider, 132, 133 large-sized, 115 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 INDEX 111 112 113 114 115 116 117 118 119 list of tweets, 114 selectedTrend parameter, 125 TrendsIntentHandling, 127 users, 118 TwitterTrendsWidget.swift, 121 W, X, Y, Z Widget components, 44 extension, 22, 23 SoccerTime’s, 21, 22 UI, 35–37, 39 WidgetConfiguration, 24, 39–43 WidgetFamily, 16–18 WidgetKit Apple’s framework, definition, features, widgetURL(_:) method, 86, 88, 106 WidgetView.swift file, 57 120 121 122 123 124 125 126 127 128 129 130 145 .. .Getting Started with WidgetKit Create Widgets for? ?iOS and? ?iPadOS Sagun? ?Raj? ?Lage Prakshapan? ?Shrestha Getting Started with WidgetKit: Create Widgets for iOS and iPadOS 45 Sagun? ?Raj? ?Lage Golmadhi,... SwiftUI flushed out the debate and brought a new and easier way to create © Sagun Raj Lage and Prakshapan Shrestha 2021 S R Lage and P Shrestha, Getting Started with WidgetKit, https://doi.org/10.1007/978-1-4842-7042-4_2... of the homescreen) That was quite an injustice for © Sagun Raj Lage and Prakshapan Shrestha 2021 S R Lage and P Shrestha, Getting Started with WidgetKit, https://doi.org/10.1007/978-1-4842-7042-4_1

Ngày đăng: 17/05/2021, 07:47

Từ khóa liên quan

Mục lục

  • Table of Contents

  • About the Authors

  • About the Technical Reviewer

  • Before You Begin…

  • Chapter 1: Getting Familiar with WidgetKit in a Flash

    • Background

    • Hello, WidgetKit!

    • Summary

    • Chapter 2: SwiftUI, Human Interface Guidelines, and Widget Family

      • SwiftUI

        • Basic SwiftUI Views for Widgets

          • Text

          • Button

          • Image

          • HStack

          • VStack

          • ZStack

          • Human Interface Guidelines

          • Widget Family

          • Summary

          • Chapter 3: Writing Your First Widget

            • Widget Extension

            • TimelineEntry

            • TimelineProvider

              • placeholder(in:)

              • getSnapshot(in:completion:)

Tài liệu cùng người dùng

Tài liệu liên quan