I am implementing Firebase dynamic links in my iOS project. It is working fine and it is opening my iOS Home screen properly. But now I would like to extract values from url and open appropriate screen based on url.
for example:
https://www.dd.com/forums/hot-deals-online/topics/smart-tv-carniva
in this url I would like to get 'hot-deals-online' and 'smart-tv-carniva' permalink which I will pass to view controller to open that screen on the app
can someone suggest me best approach for this.
Shortest is to access path of URLComponents object:
let url = "https://www.dd.com/forums/hot-deals-online/topics/smart-tv-carniva"
if let comps = URLComponents(string: url) {
var elements = comps.path.split(separator: "/").map(String.init)
// ["forums", "hot-deals-online", "topics", "smart-tv-carniva"]
// To build an url back, example:
var url = URL(string: comps.host!)!
url.appendPathComponent(elements[0])
url.appendPathComponent(elements[1])
// Result: www.dd.com/forums/hot-deals-online
}
P.S. calling .map(String.init) makes in-place conversion from array of substrings to normal String array.
Related
What URL exactly we need to specify inside .widgetURL()?
In WWDC sessions on Widget Code Along, They used .widgetURL(entry.character.url). Here entry.character.url is URL(string: "game:///egghead")!
Please explain about URL we need to pass here (widgetURL()) to reach the particular view of APP?
the url is in the form scheme://someAction
At risk of oversimplifying it, the scheme is typically the name of your app. this is how you confirm that the url is intended for your app
in your widget body you use the widgetURL modifier:
var body: some View {
content().widgetURL("myApp://someAction")
}
then in your app you use the onOpenURL modifier to pick up the url, check its for your app and parse it to determine what to do. someAction is the message you are sending the app to tell it what to do
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
guard url.scheme == "myApp" else { return }
print(url) // parse the url to get someAction to determine what the app needs do
}
}
}
I usually make the url of the form myApp://someAction/parms so that I can parse the url to determine what the app needs to do and also pass it some parms. Some people like to do this by forming the URL properly with URLComponents - path, queryItems etc. this is probably best practice.
I have to play HLS stream and it is secured/protected. In order to download manifest file and further ts file it is expected that request have valid Cookies information sent. I do receive Cookies information from server but it is not part of response header whereas it comes as part response String. It means Cookies automatically does not become part of NSHTTPStorage and application have to consume response string and fetch Cookies information from there; further need to be set while initialising AVURLAsset. As per AVURLAsst documentation in order to send Cookie information we should use options "AVURLAssetHTTPCookiesKey". I have mentioned in below code how to initialise AVURLAsset using this key
let cookieOptions = [AVURLAssetHTTPCookiesKey: Any]
let assets = AVURLAsset(url: url as URL, options: cookieArrayOptions)
My main problem how to set value against AVURLAssetHTTPCookiesKey. It take value of 'Any' type.Since Apple just mention it expect Any value and does not tell how internally it interpret and convert that value in correct format.
I tried two approach to set value: Say I have to set two Cookies key viz MyCookies1, MyCookies2
Approach 1 using NSHTTPStorage
let propertiesKey1 = [HTTPCookiePropertyKey.domain : "Domain=abc.com",HTTPCookiePropertyKey.path:"/",HTTPCookiePropertyKey.secure:true,HTTPCookiePropertyKey.init("HttpOnly"):true,HTTPCookiePropertyKey.value:"abc1",HTTPCookiePropertyKey.name:"MyCookies1"] as [HTTPCookiePropertyKey : Any]
let propertiesKey2 = [HTTPCookiePropertyKey.domain : "Domain=abc.com",HTTPCookiePropertyKey.path:"/",HTTPCookiePropertyKey.secure:true,HTTPCookiePropertyKey.init("HttpOnly"):true,HTTPCookiePropertyKey.value:"abc2",HTTPCookiePropertyKey.name:"MyCookies2"] as [HTTPCookiePropertyKey : Any]
let cookieKey1 = HTTPCookie(properties: propertiesKey1)
let cookieKey2 = HTTPCookie(properties: propertiesKey2)
HTTPCookieStorage.shared.setCookie(cookieKey1!)
HTTPCookieStorage.shared.setCookie(cookieKey2!)
let cookiesArray = HTTPCookieStorage.shared.cookies!
let cookieArrayOptions = [AVURLAssetHTTPCookiesKey: cookiesArray]
guard let url = URL(string:"abc.com") else { return }
let assets = AVURLAsset(url: url as URL, options: cookieArrayOptions)
But this is not working and AVPlayer just initializing and stopped working
Approach 2
let values = ["Cookie": "MyCookies1=abc1; MyCookies2=abc2"]
let cookieArrayOptions = [AVURLAssetHTTPCookiesKey: values]
guard let url = URL(string:"abc.com") else { return }
let assets = AVURLAsset(url: url as URL, options: cookieArrayOptions)
But this options always crashes.May be I am not setting values in correct format.
I have tried to provide as much information as possible but let me know if you need any further information.
After couple of tries I am able to make it working with Approach 1 usign NSHTTPStorage. I made a mistake of not using HTTPS streaming URL. I had already allowed to play non-HTTP url by passsing ATS and allow arbitrary loads. But AVURLAssetHTTPCookiesKey expect to provide only HTTPS url to be set for cookies. I used HTTPS streams and ultimately Cookies started to be sent internally in request header while downloading manifest/segment file in playing HLS stream
In my Firebase database, I have a 'link' with some URL of a video as a value.
I'm trying to call it it via:
enum Media {
static let videoURL = "linkLabel.text = posts[selectedIndexPath].link"
}
Without success. If I call it just as a label text, it is fine. However, I also try the following:
static let videoURL = URL(string: "linkLabel.text = posts[selectedIndexPath].link")
As well as trying to call from my super.viewDidload() like:
videoVRView.load(from: URL(string: "\(linkLabel.text = posts[selectedIndexPath].link)"))
Anybody would know how to achieve this correctly? It will be amazing! Thanks!
----- EDIT:
I got it work by videoVRView.load(from: URL(string: "\(posts[selectedIndexPath].link)"))
I have x 2 questions about urls and webViews.
Question 1:
I have a string which I am getting from an API that is supposed to be a url. The string is https://godochurch.blob.core.windows.net/sermons/1031/30-1-2017-Vision Sunday-Devotion.mp3
When trying to convert to a url I'm getting nil.
Here is the code:
if let sermonUrl = sermonUrl {
if let url = URL(string: sermonUrl) {
let requestObj = URLRequest(url: url)
webView.loadRequest(requestObj)
}
}
I have worked out that the space between 'Vision' and 'Sunday' is the problem.
Should I be encoding the string in some way before trying to convert it to a URL? What's confusing is that if I paste the string into my browser it works just fine, but I notice the browser is percent encoding the space.
If I am supposed to be encoding the string, how do I do that?
Question 2:
I see that URL(string: "urlStringHere") is only available from iOS 10. My app needs to work for iOS 9. How can I convert the above code so it works on iOS 9 and 10.
Thanks in advance for your time.
1: escape that space with %20 or +:
if let url = URL(string: "https://godochurch.blob.core.windows.net/sermons/1031/30-1-2017-Vision%20Sunday-Devotion.mp3") {
// ...
}
2: URL was introduced with Swift 3, which is compatible with iOS 8 and later. See this question on how to make it work with older versions of iOS.
Edit: the easiest way to percent-escape a URL is to use URLComponents:
var components = URLComponents(string: "https://godochurch.blob.core.windows.net")!
components.path = "/sermons/1031/30-1-2017-Vision Sunday-Devotion.mp3"
if let url = components.url {
// ...
}
If you happen to get you URL strings from a webservice and it contains a space, that service is bad. Space is not allowed in an URL. Web browsers relax that rule because they know there are bad URLs out there. Swift won't take it.
I'm working on an horoscope application now I have a problem after user selected is zodiac how can I know what php url to show him for example:
there is 12 zodiac's so how can I give him his horoscope for this url for example
NSURL: "blablabla_horscope.com/libra/today_horoscope"
I mean I don't want to write in Xcode twelve options of url, and start making if statements for example "if this user and then use here is NSUserDefaults with his choosed zodiac"
since then I will have to start making a list of a lot of if statement and it will make my app work slow and it's a bad idea so I have thinking about making some kinda of php file, that will handle all that after the user registered in the background but where to start for that?
I'm currently working on Swift, core-data, php.
let urlString = "http://www.blablabla/?sign=libra&time=today"
let url = NSURL (string: urlString)
let dataURL = try? NSData(contentsOfURL: url!, options: [])
let result: String = String(data: dataURL!, encoding: NSUTF8StringEncoding)!
print("the result is %#", result)
Set your constant for the sign type prior to setting your urlString.
let sign = "libra"
or
let sign = "cancer"
etc...
and then you can:
let urlString = "http://www.blablabla/?sign=\(sign)&time=today"
Late