Class: NSURLConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/bean/nsurl_connection_additions.rb

Overview

Extensions to the NSURLConnection class

Instance Method Summary collapse

Instance Method Details

#connection(conn, didFailWithError: error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Connection failed callback



54
55
56
# File 'lib/bean/nsurl_connection_additions.rb', line 54

def connection(conn, didReceiveResponse:resp)
  @data.setLength(0)
end

#connectionDidFinishLoading(conn) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Connection finished callback



74
75
76
77
78
# File 'lib/bean/nsurl_connection_additions.rb', line 74

def connectionDidFinishLoading(conn)
  @response = NSString.alloc.initWithData(@data, encoding:NSUTF8StringEncoding)
  @on_success.call(@response) if @on_success
  @data = nil
end

#initWithRequest(req, &blk) ⇒ NSURLConnection

Helper method for creating a NSURLConnection.

Using this method will cause the URL Connection to use itself as the delegate. You can then add error and success handlers to be called when the download is finished.

Parameters:

  • req (NSURLRequest)

    The URL Request

  • blk (Proc)

    The success block to hookup

Returns:



13
14
15
16
17
18
19
20
21
22
# File 'lib/bean/nsurl_connection_additions.rb', line 13

def initWithRequest(req, &blk)
  @error = nil
  @response = nil

  @on_error = nil
  @on_success = blk
  @data = NSMutableData.data

  initWithRequest(req, delegate:self)
end

#on_error(&blk) ⇒ Nil

Attach an error handler to the connection.

The error handler will be executed in case of a connection error. The handler can be attached after the connection is completed.

Parameters:

  • blk (Proc)

    The block to execute

Returns:

  • (Nil)


33
34
35
36
# File 'lib/bean/nsurl_connection_additions.rb', line 33

def on_error(&blk)
  @on_error = blk
  @on_error.call(@error) if @error
end

#on_success(&blk) ⇒ Nil

Attach a success handler to the connection.

The success handler will be executed when the connection is complete. The handler can be attached after the connection is completed.

Parameters:

  • blk (Proc)

    The block to execute

Returns:

  • (Nil)


47
48
49
50
# File 'lib/bean/nsurl_connection_additions.rb', line 47

def on_success(&blk)
  @on_success = blk
  @on_success.call(@response) if @response
end