How to get HTML Source from URL in Swift Anonymously?
I am trying to scrape html code from a URL using Swift, but it needs to be done anonymously.
The issue with my case is that the content differs when I am logged in the website or not. My app has a webkit that allows users to view a certain page of a website. If they log in via the webkit, the scraping result is different from if they did not log in.
I have read and searched through tons of questions on StackOverflow to see how to get the HTML source from a URL and the one that I am using can be found here:
How To Get HTML source from URL with Swift I have also tried the codes from different answers and questions.
guard let getHTMLString = try? String(contentsOf: theUrl, encoding: .utf8).condensingWhitespace() as String else {
callErrorPage()
return
}
I get the content of the html page with the code above.
But like I mentioned above, the codes differ if the user has logged in the website.
Edit: I need the code to always be the version where the user is not logged in.
html ios swift
New contributor
|
show 1 more comment
I am trying to scrape html code from a URL using Swift, but it needs to be done anonymously.
The issue with my case is that the content differs when I am logged in the website or not. My app has a webkit that allows users to view a certain page of a website. If they log in via the webkit, the scraping result is different from if they did not log in.
I have read and searched through tons of questions on StackOverflow to see how to get the HTML source from a URL and the one that I am using can be found here:
How To Get HTML source from URL with Swift I have also tried the codes from different answers and questions.
guard let getHTMLString = try? String(contentsOf: theUrl, encoding: .utf8).condensingWhitespace() as String else {
callErrorPage()
return
}
I get the content of the html page with the code above.
But like I mentioned above, the codes differ if the user has logged in the website.
Edit: I need the code to always be the version where the user is not logged in.
html ios swift
New contributor
check for the content which is available only for the logged-in users...?
– holex
12 hours ago
I need contents of when the user is not logged in.
– Seifer
12 hours ago
In many cases the login information is stored in a cookie. If this is the case useURLSession
andURLRequest
which is able to ignore cookies. (httpShouldHandleCookies = false
). The APIString(contentsOf
for a remote URL is inappropriate anyway. And why do you cast aString
to aString
?
– vadian
12 hours ago
you know, your question does not make any sense at all... if you don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only.
– holex
12 hours ago
@holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user.
– Seifer
12 hours ago
|
show 1 more comment
I am trying to scrape html code from a URL using Swift, but it needs to be done anonymously.
The issue with my case is that the content differs when I am logged in the website or not. My app has a webkit that allows users to view a certain page of a website. If they log in via the webkit, the scraping result is different from if they did not log in.
I have read and searched through tons of questions on StackOverflow to see how to get the HTML source from a URL and the one that I am using can be found here:
How To Get HTML source from URL with Swift I have also tried the codes from different answers and questions.
guard let getHTMLString = try? String(contentsOf: theUrl, encoding: .utf8).condensingWhitespace() as String else {
callErrorPage()
return
}
I get the content of the html page with the code above.
But like I mentioned above, the codes differ if the user has logged in the website.
Edit: I need the code to always be the version where the user is not logged in.
html ios swift
New contributor
I am trying to scrape html code from a URL using Swift, but it needs to be done anonymously.
The issue with my case is that the content differs when I am logged in the website or not. My app has a webkit that allows users to view a certain page of a website. If they log in via the webkit, the scraping result is different from if they did not log in.
I have read and searched through tons of questions on StackOverflow to see how to get the HTML source from a URL and the one that I am using can be found here:
How To Get HTML source from URL with Swift I have also tried the codes from different answers and questions.
guard let getHTMLString = try? String(contentsOf: theUrl, encoding: .utf8).condensingWhitespace() as String else {
callErrorPage()
return
}
I get the content of the html page with the code above.
But like I mentioned above, the codes differ if the user has logged in the website.
Edit: I need the code to always be the version where the user is not logged in.
html ios swift
html ios swift
New contributor
New contributor
edited 7 hours ago
Mr.Kushwaha
474511
474511
New contributor
asked 12 hours ago
SeiferSeifer
42
42
New contributor
New contributor
check for the content which is available only for the logged-in users...?
– holex
12 hours ago
I need contents of when the user is not logged in.
– Seifer
12 hours ago
In many cases the login information is stored in a cookie. If this is the case useURLSession
andURLRequest
which is able to ignore cookies. (httpShouldHandleCookies = false
). The APIString(contentsOf
for a remote URL is inappropriate anyway. And why do you cast aString
to aString
?
– vadian
12 hours ago
you know, your question does not make any sense at all... if you don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only.
– holex
12 hours ago
@holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user.
– Seifer
12 hours ago
|
show 1 more comment
check for the content which is available only for the logged-in users...?
– holex
12 hours ago
I need contents of when the user is not logged in.
– Seifer
12 hours ago
In many cases the login information is stored in a cookie. If this is the case useURLSession
andURLRequest
which is able to ignore cookies. (httpShouldHandleCookies = false
). The APIString(contentsOf
for a remote URL is inappropriate anyway. And why do you cast aString
to aString
?
– vadian
12 hours ago
you know, your question does not make any sense at all... if you don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only.
– holex
12 hours ago
@holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user.
– Seifer
12 hours ago
check for the content which is available only for the logged-in users...?
– holex
12 hours ago
check for the content which is available only for the logged-in users...?
– holex
12 hours ago
I need contents of when the user is not logged in.
– Seifer
12 hours ago
I need contents of when the user is not logged in.
– Seifer
12 hours ago
In many cases the login information is stored in a cookie. If this is the case use
URLSession
and URLRequest
which is able to ignore cookies. (httpShouldHandleCookies = false
). The API String(contentsOf
for a remote URL is inappropriate anyway. And why do you cast a String
to a String
?– vadian
12 hours ago
In many cases the login information is stored in a cookie. If this is the case use
URLSession
and URLRequest
which is able to ignore cookies. (httpShouldHandleCookies = false
). The API String(contentsOf
for a remote URL is inappropriate anyway. And why do you cast a String
to a String
?– vadian
12 hours ago
you know, your question does not make any sense at all... if you don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only.
– holex
12 hours ago
you know, your question does not make any sense at all... if you don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only.
– holex
12 hours ago
@holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user.
– Seifer
12 hours ago
@holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user.
– Seifer
12 hours ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
It will always do this anonymously :
var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
if let data = data {
let contents = String(data: data, encoding: .ascii)
}
}.resume()
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Seifer is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54249859%2fhow-to-get-html-source-from-url-in-swift-anonymously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It will always do this anonymously :
var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
if let data = data {
let contents = String(data: data, encoding: .ascii)
}
}.resume()
New contributor
add a comment |
It will always do this anonymously :
var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
if let data = data {
let contents = String(data: data, encoding: .ascii)
}
}.resume()
New contributor
add a comment |
It will always do this anonymously :
var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
if let data = data {
let contents = String(data: data, encoding: .ascii)
}
}.resume()
New contributor
It will always do this anonymously :
var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
if let data = data {
let contents = String(data: data, encoding: .ascii)
}
}.resume()
New contributor
New contributor
answered 12 hours ago
EmreEmre
765
765
New contributor
New contributor
add a comment |
add a comment |
Seifer is a new contributor. Be nice, and check out our Code of Conduct.
Seifer is a new contributor. Be nice, and check out our Code of Conduct.
Seifer is a new contributor. Be nice, and check out our Code of Conduct.
Seifer is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54249859%2fhow-to-get-html-source-from-url-in-swift-anonymously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
check for the content which is available only for the logged-in users...?
– holex
12 hours ago
I need contents of when the user is not logged in.
– Seifer
12 hours ago
In many cases the login information is stored in a cookie. If this is the case use
URLSession
andURLRequest
which is able to ignore cookies. (httpShouldHandleCookies = false
). The APIString(contentsOf
for a remote URL is inappropriate anyway. And why do you cast aString
to aString
?– vadian
12 hours ago
you know, your question does not make any sense at all... if you don't know what you are looking for, how does anyone else would know...? your question in its current state cannot be answered at all – speculations can be made only.
– holex
12 hours ago
@holex I know what I want. I want to get the html code of a website. But the problem is the code is different depending on if I am logged in to the website. I want the code of a non-logged in user.
– Seifer
12 hours ago