Common method in Swift


1. Check for any field empty in NSDictionary class func checkforEmptyValueinDictioanty(dic:NSDictionary)-> Bool{ for (keyVal, dataVal) in dic {   if (dataVal.length()==0){                 println("\(keyVal): \(dataVal.length())")                 return false             }                 ... Continue Reading →

Advertisements

Use different font text in a Label in swift


let secondLabel=UILabel(frame: CGRectMake(0, 16, _screenWidth-2, 20))         secondLabel.textColor=UIColor.whiteColor()         secondLabel.textAlignment=NSTextAlignment.Center         secondLabel.font=UIFont(name: "Arial", size: 12)         bottomView.addSubview(secondLabel)                  let attrSting=NSMutableAttributedString(string: "I agree to the Terms of Service and Privacy Policy.")             ... Continue Reading →

Basic HTTP Authentication in iOS while calling webservice


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {    if ([challenge previousFailureCount] == 0) {        NSLog(@"received authentication challenge");        NSURLCredential *newCredential = [NSURLCredentialcredentialWithUser:@"tylerhorne"                                                           ... Continue Reading →

Post Json data to server in iOS


-(void)webservice{ NSString *url= SERVER_URL; NSLog(@"url is %@",url);   NSMutableURLRequest *_request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];   NSMutableDictionary *post=[[NSMutableDictionaryalloc] init]; [post setObject:_emailText.text forKey:@"username"]; [post setObject:_passText.text forKey:@"password"];   NSError *error; NSData *data=[NSJSONSerializationdataWithJSONObject: post options:kNilOptionserror:&error]; NSString *jsonString = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *post = [NSString stringWithFormat:@"json=%@", jsonString]; NSData*postData = [post dataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:NO];   NSLog(@"string is %@",post); [_request setHTTPMethod:@"POST"]; [_request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"content-type"];... Continue Reading →

Move upward textField in Scrollview when keyboard appears


Step 1>>First add a view in Interface class@property(weak, nonatomic) UIView *activeTextView;Add Notification in ViewWillAppear- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    // register for keyboard notifications    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotificationobject:nil];     [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotificationobject:nil];    }Step 2>>- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    // unregister for keyboard notifications    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil];    }Step 3>>- (void)textFieldDidBeginEditing:(UITextField *)textField{   ... Continue Reading →

Call a function using Notification in Xcode


Step 1: Post Notification from class A.NSString *data=@"Demo";[[NSNotificationCenter defaultCenter] postNotificationName:@"golferUpdate" object:nil userInfo:data];if you do not want to pass data use [[NSNotificationCenter defaultCenter] postNotificationName:@"golferUpdate" object:nil];Step 2: Register Notification in Class B.[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(GetData:) name:@"golferUpdate"object:nil];Step 3: Add function GetData-(void)GetData:(NSNotification *)notification{    NSDictionary *dict = [notification userInfo];// write your code Here}  Step 4: Enjoy Coding………..Best Of Luck.

Send Class Name as a argument in iOS


Step 1::   in your current class.#import "HomeView.h"#import "MSYCommon.h"Step 2 ::*call this from any method of view where MSYCommon is a name of common class.[MSYCommon pushBackToSpecific:self.navigationController withName:@"HomeView"];Step 3":: in MSYCommon class+(void)pushBackToSpecific:(UINavigationController *)nav withName:(NSString *)screenName{    Class classFromString = NSClassFromString(screenName);    for (UIViewController *controller in nav.viewControllers) {        if ([controller isKindOfClass:[classFromString class]]) {       ... Continue Reading →

Pop to specific View controller in iOS


First Method ::- (void) RetunToSpecificViewController{ for (UIViewController *controller in self.navigationController.viewControllers) { if ([controller isKindOfClass:[AnOldViewController class]]) { //Do not forget to import AnOldViewController.h [self.navigationController popToViewController:controller animated:YES]; break; } } Second Method ::NSArray *array = [self.navigationController viewControllers]; [self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];Best Of Luck.

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: