Saturday, September 13, 2014

Show only Photos in photo gallery iOS - Objective c


UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = (id)self;
picker.allowsEditing = YES;
 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            //photo 
            picker.mediaTypes =  [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];;
        }
        

[self presentViewController:picker animated:YES completion:nil];

        

Wednesday, August 20, 2014

Debugging with Assertions - Swift Language - iOS 8


An assertion is a runtime check that a logical condition definitely evaluates to true. Literally put, an assertion “asserts” that a condition is true. You use an assertion to make sure that an essential condition is satisfied before executing any further code. If the condition evaluates to true, code execution continues as usual; if the condition evaluates to false, code execution ends, and your app is terminated.

If your code triggers an assertion while running in a debug environment, such as when you build and run an app in Xcode, you can see exactly where the invalid state occurred and query the state of your app at the time that the assertion was triggered. An assertion also lets you provide a suitable debug message as to the nature of the assert. You write an assertion by calling the global assert function. You pass the assert function an expression that evaluates to true or false and a message that should be displayed if the result of the condition is false:


let age = -3
assert(age >= 0, "A person's age cannot be less than zero") // this causes the assertion to trigger, because age is not >= 0



In this example, code execution will continue only if age >= 0 evaluates to true, that is, if the value of age is non-negative. If the value of age is negative, as in the code above, then age >= 0 evaluates to false, and the assertion is triggered, terminating the application.

Assertion messages cannot use string interpolation. The assertion message can be omitted if desired, as in the following example:

assert(age >= 0)

When to Use Assertions

Use an assertion whenever a condition has the potential to be false, but must definitely be true in order for your code to continue execution. Suitable scenarios for an assertion check include:
An integer subscript index is passed to a custom subscript implementation, but the subscript index value could be too low or too high.A value is passed to a function, but an invalid value means that the function cannot fulfill its task.An optional value is currently nil, but a non- nil value is essential for subsequent code to execute successfully.


NOTE
Assertions cause your app to terminate and are not a substitute for designing your code in such a way that

invalid conditions are unlikely to arise. Nonetheless, in situations where invalid conditions are possible, an assertion is an effective way to ensure that such conditions are highlighted and noticed during development, before your app is published. 

Tuples - Swift language iOS 8







Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.
In this example, (404, "Not Found") is a tuple that describes an HTTP status code. An HTTP status code is a special value returned by a web server whenever you request a web page. A status code of 404 Not Found is returned if you request a webpage that doesn’t exist.



let http404Error = (404, "Not Found")

// http404Error is of type (Int, String), and equals (404, "Not Found")




The (404, "Not Found") tuple groups together an Int and a String to give the HTTP status code two separate values: a number and a human-readable description. It can be described as “a tuple of type (Int, String)”.

You can create tuples from any permutation of types, and they can contain as many different types as you like. There’s nothing stopping you from having a tuple of type (Int, Int, Int), or (String, Bool), or indeed any other permutation you require.
You can decompose a tuple’s contents into separate constants or variables, which you then access as usual:


let (statusCode, statusMessage) = http404Error 

println("The status code is \(statusCode)")

// prints "The status code is 404"

println("The status message is \(statusMessage)"

// prints "The status message is Not Found"



If you only need some of the tuple’s values, ignore parts of the tuple with an underscore (_) when you decompose the tuple:

let (justTheStatusCode, _) = http404Error 

println("The status code is \(justTheStatusCode)")   
// prints "The status code is 404"

Alternatively, access the individual element values in a tuple using index numbers starting at zero:


println("The status code is \(http404Error.0)")
// prints "The status code is 404"

println("The status message is \(http404Error.1)"
// prints "The status message is Not Found"


You can name the individual elements in a tuple when the tuple is defined:

let http200Status = (statusCode: 200, description: "OK")

If you name the elements in a tuple, you can use the element names to access the values of those elements:


println("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"

println("The status message is \(http200Status.description)")


// prints "The status message is OK"


Tuples are particularly useful as the return values of functions. A function that tries to retrieve a web page might return the (Int, String) tuple type to describe the success or failure of the page retrieval. By returning a tuple with two distinct values, each of a different type, the function provides more useful information about its outcome than if it could only return a single value of a single type.

NOTE
Tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple. 




Monday, August 18, 2014

UIAlertView Swift language iOS8

Code

 var alert=UIAlertController(title: "Warning", message: "Success", preferredStyle: UIAlertControllerStyle.Alert);
        
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Cancel, handler:nil));


self.presentViewController(alert, animated: true, completion: nil);

Wednesday, August 13, 2014

iOS Accessing Music List

One way is to take the array of MPMediaItems you get from the MPMediaQuery and sort it by MPMediaItemPropertyLastPlayedDate using an NSSortDescriptor:
NSTimeInterval start  = [[NSDate date] timeIntervalSince1970];

MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
NSArray *songsArray = [songsQuery items];

NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:MPMediaItemPropertyLastPlayedDate
                                                         ascending:NO];
NSArray *sortedSongsArray = [songsArray sortedArrayUsingDescriptors:@[sorter]];

NSTimeInterval finish = [[NSDate date] timeIntervalSince1970];
NSLog(@"Execution took %f seconds.", finish - start);

Tuesday, August 12, 2014

iOS Networking Open source tools

Networking

AFNetworking iOS & OS X networking framework
asi-http-request CFNetwork wrapper for HTTP requests
RestKit Consuming and modeling RESTful web resources
Reachability ARC and GCD modification for Apple’s reachability class
socket.IO-objc socket.io v0.7.2+ for iOS devices
AFNetworkActivityLoggerv AFNetworking 2.0 Extension for Network Request Logging

Monday, August 11, 2014

iOS Debug Tools



Debug Tools

PonyDebugger Remote network and data debugging
CocoaLumberJack Flexible logging framework for Mac and iOS
superdb realtime wireless debugger for iOS
OHHTTPStubs Stub network requests
NSLogger A modern, flexible logging tool
Xtrace Trace Objective-C method calls by class or instance
RHObjectiveBeagle Beagle is an Objective C debugging tool that can sniff out class instances on the heap.
KSCrash The Ultimate iOS Crash Reporter
chisel Chisel is a collection of LLDB commands to assist debugging iOS apps.
plcrashreporter Mirror of the official PLCrashReporter repository