Skip to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SWIFT] Basic Web API

Featured Replies


// Copyright (c) 2014 Anthony Puppo. All rights reserved.

import Foundation

private var instance:WebAPI!

func getWebAPIInstance() -> WebAPI {

if (instance == nil) {

instance = WebAPI()

}

return instance

}

class WebAPI {

private init() {

//Singleton Design Pattern

}

// Sends a pre-configured asynchronous request

func sendRequest(request:NSMutableURLRequest, callback:(String!, NSError!) -> Void) {

let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request) {

(data:NSData!, response:NSURLResponse!, error:NSError!) -> Void in

if (error == nil) {

dispatch_async(dispatch_get_main_queue(), {

callback(NSString(data: data, encoding: NSUTF8StringEncoding), nil)

})

}

else {

dispatch_async(dispatch_get_main_queue(), {

callback(nil, error)

})

}

}

task.resume()

}

// Retrieves a JSON object and handles it asynchronously with the defined callback

func fetchJSON(json:AnyObject!, httpMethod:String, url:String, cachePolicy:NSURLRequestCachePolicy, timeoutInterval:NSTimeInterval, callback:(Dictionary<String, AnyObject>!, NSError!) -> Void) {

let request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: url), cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)

request.HTTPMethod = httpMethod

request.setValue("application/json", forHTTPHeaderField: "Accept")

if (json != nil) {

request.setValue("application/json", forHTTPHeaderField: "Content-Type")

request.HTTPBody = stringifyJSON(json).dataUsingEncoding(NSUTF8StringEncoding)!

}

sendRequest(request, callback: {

(data:String!, error:NSError!) -> Void in

if (error == nil) {

dispatch_async(dispatch_get_main_queue(), {

callback(self.parseJSONString(data), nil)

})

}

else {

dispatch_async(dispatch_get_main_queue(), {

callback(nil, error)

})

}

})

}

// MARK: - Privates

// Converts an object into JSON String form if possible

private func stringifyJSON(json:AnyObject) -> String {

var error:NSError?

let jsonData:NSData = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions(0), error: &error)!

return (error == nil) ? NSString(data: jsonData, encoding: NSUTF8StringEncoding) : ""

}

// Converts a JSON string (if possible) into a usable Dictionary

private func parseJSONString(jsonString:String) -> Dictionary<String, AnyObject> {

var error:NSError?

let data:NSData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!

let jsonObj:Dictionary<String, AnyObject> = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &error) as Dictionary<String, AnyObject>

return (error == nil) ? jsonObj : Dictionary<String, AnyObject>()

}

}

Guest
This topic is now closed to further replies.

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.