Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

How to Use ShareLink for Sharing Data Like Text and Photos

learningcode_x1mckf by learningcode_x1mckf
September 6, 2022
in Swift
0
How to Use ShareLink for Sharing Data Like Text and Photos
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


In iOS 16, SwiftUI comes with a brand new view referred to as ShareLink. When customers faucet on the share hyperlink, it presents a share sheet for customers to share content material to different functions or copy the information for later use.

You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

The ShareLink view is designed to share any varieties of knowledge. On this tutorial, we’ll present you use ShareLink to let customers share textual content, URL, and pictures.

Fundamental Utilization of ShareLink

Let’s start with an instance. To create a share hyperlink for sharing a URL, you possibly can write the code like this:

struct ContentView: View

    personal let url = URL(string: “https://www.appcoda.com”)!

 

    var physique: some View

        VStack

            ShareLink(merchandise: url)

        

    

SwiftUI robotically renders a Share button with a small icon.

sharelink-basic-for-swiftui

When tapped, iOS brings up a share sheet for customers to carry out additional actions similar to copy and including the hyperlink to Reminders.

swiftui-share-sheet

To share textual content, as an alternative of URL, you possibly can merely cross the a string to the merchandise parameter.

ShareLink(merchandise: “Try this new characteristic on iOS 16”)

Customizing the Look of Share Hyperlink

To customise the looks of the hyperlink, you possibly can present the view content material within the closure like this:

ShareLink(merchandise: url)

    Picture(systemName: “sq..and.arrow.up”)

On this case, SwiftUI solely shows the share icon for the hyperlink.

swiftui-tap-and-share

Alternatively, you possibly can current a label with a system picture or customized picture:

ShareLink(merchandise: url)

    Label(“Faucet me to share”, systemImage:  “sq..and.arrow.up”)

When initializing the ShareLink occasion, you possibly can embody two further parameters to offer further details about the shared merchandise:

ShareLink(merchandise: url, topic: Textual content(“Try this hyperlink”), message: Textual content(“If you wish to study Swift, check out this web site.”))

    Picture(systemName: “sq..and.arrow.up”)

The topic parameter permits you to embody a title concerning the URL or any merchandise to share. The message parameter lets you specify the outline of the merchandise. Relying on the actions the person shares to, iOS will current the topic or message or each. Say, in the event you add the URL to Reminders, the Reminders app shows the preset message.

swiftui-sharelink-reminders

Sharing Photographs

Aside from URLs, you possibly can share pictures utilizing ShareLink. Here’s a pattern code snippet:

struct ContentView: View

    personal let picture = Picture(“bigben”)

 

    var physique: some View

        VStack(alignment: .main, spacing: 10)

            picture

                .resizable()

                .scaledToFit()

 

            ShareLink(merchandise: picture, preview: SharePreview(“Large Ben”, picture: picture))

 

        

        .padding(.horizontal)

    

For the merchandise parameter, you specify the picture to share. And, you present a preview of the picture by passing an occasion of SharePreview. Within the preview, you specify the title of the picture and the thumbnail. While you faucet the Share button, iOS brings up a share sheet with the picture preview.

swiftui-sharelink-image

Conforming to Transferable

Aside from URLs, the merchandise parameter accepts any objects that conforms to the Transferable protocol. In iOS, the next varieties are the usual Transferable varieties:

  • String
  • Information
  • URL
  • Attributed String
  • Picture

Transferable is a protocol that describes how a kind interacts with transport APIs similar to drag and drop or copy and paste.

So what if in case you have a customized object, how will you make it transferable? Let’s say, you create the next Picture construction:

struct Picture: Identifiable

    var id = UUID()

    var picture: Picture

    var caption: String

    var description: String

To let ShareLink share this object, it’s important to undertake the Transferable protocol for Picture and implement the transferRepresentation property:

extension Picture: Transferable

    static var transferRepresentation: some TransferRepresentation

        ProxyRepresentation(exporting: .picture)

    

There are a variety of Switch Representations together with ProxyRepresentation, CodableRepresentation, DataRepresentation, and FileRepresentation. Within the code above, we use the ProxyRepresentation which is a switch illustration that makes use of one other kind’s switch illustration as its personal. Right here, we use the Picture‘s built-in Transferable conformance.

swiftui-image-transferable

Since Picture now conforms to Transferable, you possibly can cross the Picture occasion to ShareLink:

ShareLink(merchandise: picture, preview: SharePreview(picture.caption, picture: picture.picture))

When customers faucet the Share button, the app brings up the share sheet for sharing the picture.

What’s Subsequent

This tutorial exhibits you use ShareLink for sharing textual content, URL, and pictures. Actually, this new view lets you share any varieties of knowledge so long as the kind conforms to the Transferable protocol.

For customized varieties, you undertake the protocol and supply a switch illustration by utilizing one of many built-in TransferRepresentation varieties. We briefly talk about the ProxyRepresentation kind. If it’s essential share a file between functions, you should utilize the FileRepresentation kind. In later tutorials, we’ll talk about extra on that.





Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

The abstract Vapor service factory design pattern

by learningcode_x1mckf
February 1, 2023
0
The abstract Vapor service factory design pattern

I've written a number of articles about manufacturing unit design patterns on my weblog and this time I might like to speak a couple of particular one, which...

Read more

SwiftNIO tutorial – The echo server

by learningcode_x1mckf
January 27, 2023
0
SwiftNIO tutorial – The echo server

Intoducing SwiftNIO In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops...

Read more

Introducing – Vapor cheatsheet – The.Swift.Dev.

by learningcode_x1mckf
January 23, 2023
0
Introducing – Vapor cheatsheet – The.Swift.Dev.

Out there on Gumroad Thanks for supporting my work by buying the cheatsheet. 🙏 Download now A whole Vapor framework reference for novices. greater than...

Read more

iCloud Programming Tutorial for iOS: An Introduction

by learningcode_x1mckf
January 18, 2023
0
iCloud Programming Tutorial for iOS: An Introduction

Editor’s observe: This week, we work with Ziad Tamim once more to provide you an introduction of iCloud programming. You’ll learn to save and retrieve knowledge from iCloud.On...

Read more

Easy multipart file upload for Swift

by learningcode_x1mckf
January 18, 2023
0
Easy multipart file upload for Swift

I imagine that you've got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not,...

Read more
Next Post
Decoding the Three Dots (…) Or Spread Operator in Javascript

Decoding the Three Dots (…) Or Spread Operator in Javascript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Ultimate UICollectionView guide with iOS examples written in Swift

Ultimate UICollectionView guide with iOS examples written in Swift

October 8, 2022
JavaScript bugs aplenty in Node.js ecosystem – found automatically – Naked Security

JavaScript bugs aplenty in Node.js ecosystem – found automatically – Naked Security

September 4, 2022
Reach Android/iOS with JavaScript, HTML and CSS — Visual Studio Magazine

Reach Android/iOS with JavaScript, HTML and CSS — Visual Studio Magazine

September 21, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?