18 JulWhat I’ve already used more than once from iOS 4.0

Want several code snippets on iOS 4.0 features to share.


1. iAds and ADBannerView

In Alterplay we have free apps with US targeting. It’s good possibility to try iAds with that.

Check if there is a ADBannerView and then create it programmatically. You can’t do it in Interface Builder if iOS 3.0-3.1.3 is required to support.

#import

bannerIsVisible = NO;if (NSClassFromString(@”ADBannerView”)){ // create banner banner =
[[ADBannerView alloc] initWithFrame:CGRectZero]; banner.frame = CGRectMake(0, -50, 320,
50); banner.alpha = 0.0; banner.requiredContentSizeIdentifiers = [NSSet setWithObject:
ADBannerContentSizeIdentifier320x50]; banner.currentContentSizeIdentifier =
ADBannerContentSizeIdentifier320x50; [self.view addSubview:banner]; banner.delegate=self;
bannerIsVisible=NO;}

Then just two methods from ADBannerViewDelegate. I show iAds on views with browser, so I
need to move a UIWebView also. ‘TrackPage()’ is Google Analytics function to gather
statistics. I’ll make separate post for it.

- (void)bannerViewDidLoadAd:(ADBannerView *)aBanner{ TrackPage(@”bannerViewDidLoadAd”);
if (!bannerIsVisible) {  [UIView beginAnimations:@"animateBannerAppear" context:NULL];
aBanner.alpha = 1.0;  aBanner.frame = CGRectOffset(aBanner.frame, 0, 50);  webBrowser.
frame = CGRectMake(0, aBanner.frame.size.height, 320, 416-aBanner.frame.size.height);
[UIView commitAnimations];  bannerIsVisible = YES; }} – (void)bannerView:(ADBannerView *)
aBanner didFailToReceiveAdWithError:(NSError *)error{ if (bannerIsVisible) {  [UIView
beginAnimations:@"animateBannerDissappear" context:NULL];  aBanner.alpha = 0.0;  aBanner.
frame = CGRectOffset(aBanner.frame, 0, -50);  webBrowser.frame = CGRectMake(0, 0, 320, 416);
[UIView commitAnimations];  bannerIsVisible = NO; }}

2. inApp SMS and MFMessageComposeViewController

Again, it’s need to support older iPhoneOS, so ‘NSClassFromString’ helps. iPodTouch is
one more case to check. When it’s older iPhoneOS my suggestion is to copy message and
ask user to open SMS app. ‘LOCAL’ is my define for localized text constants. Think,
I’ll share my defines with another post.

+ (void) SendSMSFrom:(UIViewController*)ctrl withText:(NSString*)text{ if
(NSClassFromString(@”MFMessageComposeViewController”) &&
[MFMessageComposeViewController canSendText]) {  MFMessageComposeViewController *picker =
[[MFMessageComposeViewController alloc] init];  picker.messageComposeDelegate =
[Sharer Shared];  picker.body = text;  [ctrl presentModalViewController:picker animated:YES];
[picker release]; } else  {  [UIPasteboard generalPasteboard].string = text;   UIAlertView
*alert = [[UIAlertView alloc] initWithTitle:LOCAL(@”sharing sms title”)
message:LOCAL(@”sharing sms message”)
delegate:[Sharer Shared]              cancelButtonTitle:LOCAL(@”No”)
otherButtonTitles:LOCAL(@”Open”), nil];  [alert show];  [alert release]; } }
- (void)messageComposeViewController:(MFMessageComposeViewController *)
controller didFinishWithResult:(MessageComposeResult)result{
[controller dismissModalViewControllerAnimated:YES];} – (void)alertView:(UIAlertView *)
alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if ([[alertView title]
isEqualToString:smsAlertViewTitle] && buttonIndex == 1) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://+"]]; }}

Now looking Locating and Timing on background. Will post soon.

  • http://www.blogger.com/profile/13467174593249474216 Tadej

    What's new about MFMailComposer? Been here since 3.0

  • http://www.blogger.com/profile/10136059479406321000 Slava

    Sure, it was.