Professional Documents
Culture Documents
Class: 1. To Create A Webview Using Programmatically
Class: 1. To Create A Webview Using Programmatically
Class: 1. To Create A Webview Using Programmatically
Parent - UIView
Property and
Method Description
canGoBack A Boolean value indicating whether the web view can move backward.
If true, able to move backward; otherwise, false.
canGoForward A Boolean value indicating whether the web view can move forward. If true,
able to move forward; otherwise, false.
stopLoading() Stops the loading of any web content managed by the web view.
UIWebview: Conforming
navigationTitle.title =
webView.stringByEvaluatingJavaScriptFromString("d
ocument.title")
}
Method Description
3.UIApplication
#import <uikit/uikit.h>
main(argc,argv)
{
Nsautorelease *pool = [[Nsautoreleasepool
alloc] init]
Int retVal =
UIApplicationMain(argc,argv,nil,nil)
[pool release]
return retVal
}
Explanation
UIApplicationdelegate
4.Webservices
webservices:
A web service is a collection of open
protocols and standards used for exchanging data
between applications or systems.
You will generally get the data from a url. The url is usually
provided by the API you are working with. The url will give
you some data, typically in JSON format and you access
the data using a network request.
First, get the URL that will give you the data (like this
one: http://jsonplaceholder.typicode.com/users/1 from JS
ON Placeholder). Paste that url in the JSON Formatter &
Validator to ensure it is valid JSON data.
Second, you’ll need to change some settings to allow you
to access that url from within your app, or else you will get
an error like this:
App Transport Security has blocked a cleartext HTTP
(http://) resource load since it is insecure. Temporary
exceptions can be configured via your app’s Info.plist file.
Example 1:
Example 2 (source):
let urlString =
"http://jsonplaceholder.typicode.com/users/
1"
guard let requestUrl =
URL(string:urlString) else { return }
let request = URLRequest(url:requestUrl)
let task =
URLSession.shared.dataTask(with: request) {
(data, response, error) in
if error == nil,let usableData = data {
print(usableData)
//JSONSerialization
}
}
}
task.resume()
Example 3 (source):
Example 4 (source):
Example 5 (source):
let config =
URLSessionConfiguration.default
let session = URLSession(configuration:
config)
let url = URL(string: "YOUR URL STRING")!
let task = session.dataTask(with: url) {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
print(data) // JSON Serialization
}
}
task.resume()
Example 6:
Example 7 (source):
// This shows how you can specify the
settings/parameters instead of using the
default/shared parameters
let urlToRequest =
"http://www.kaleidosblog.com/tutorial/nsurl
session_tutorial.php"
func dataRequest() {
let url4 = URL(string: urlToRequest)!
let session4 = URLSession.shared
let request = NSMutableURLRequest(url:
url4)
request.httpMethod = "POST"
request.cachePolicy =
NSURLRequest.CachePolicy.reloadIgnoringCach
eData
let paramString = "data=Hello"
request.httpBody =
paramString.data(using:
String.Encoding.utf8)
let task = session4.dataTask(with:
request as URLRequest) { (data, response,
error) in
guard let _: Data = data, let _:
URLResponse = response, error == nil else {
print("*****error")
return
}
let dataString = NSString(data: data!,
encoding: String.Encoding.utf8.rawValue)
print("*****This is the data 4:
\(dataString)") //JSONSerialization
}
task.resume()
}
dataRequest()
(Here is another tutorial on network
requests: NSURLSession in Swift: get and post data. This
tutorial does a nice job of explaining the difference
between dataTaskWithUrl and dataTaskWithRequ
est.)
Summary
A lot of examples, I know, but I hope that as you look at the
examples, you’ll notice the main parts of a network call: