Tuesday 8 October 2013

Copy a UIView from xib and programmatically repeat it using NSKeyedArchiver and NSKeyedUnarchiver.


Lets create a _contactView in .h file using IBOutlet, and connect it in .xib file.
Connect it to the _contactView.

In .m:
//The following line will create a similar view as _contactView in the XIB;
 id copyOfView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:_contactsView]];



// Repeat the view to generate multiple copies and add to the scroll view.
for (int i =0; i<_contactArray.count; i++) {
        UIView* tempView ;//= [[UIView alloc]init];
        id copyOfView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:_contactsView]];
        tempView = copyOfView;
        tempView.tag = i;
        [tempView setFrame:CGRectMake(xOffset, yOffset, width, height)];
        yOffset += ySpacing+height;
        [_contactScrollView addSubview:tempView];

    }

    [_contactScrollView setContentSize:CGSizeMake(_contactsView.frame.size.width, _contactsView.frame.size.height*(_contactArray.count+1))];


Open other apps from iOS app using URL scheme. Open google maps, mail from the app, call a number, send sms using Xcode.


Use openURL: function of UIApplication.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Open The Google Maps:

// Create string for the place ...
NSString* placeStr = @"1 Infinite Loop, Cupertino, CA 95014";
// Encode URL encode
placeStr =  [addressText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Now create the URL string ...
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", placeStr];
// use openURL to launch the app ...
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Mail from the app:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://info@example.com"]];

Call a number:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9876543210"]];

Send SMS:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:55555"]];

Open App Store:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/en/app/apple-store/id375380948?mt=8"]];