Wednesday, August 27, 2008

Add, Set, and Delete Cookies in UIWebView with NSHTTPCookieStorage

NSHTTPCookieStorage allows you to update the cookies for every request made from your application. UIWebViews will respond to this changes in cookies at run time and NSHTTPCookieStorage allows you to listen for changes in cookies as the are normaly set in the browser.

To get the cookies for any url use:
NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* theCookies = [cookieStorage cookiesForURL:[NSURL URLWithString:@"http://example.com"]];


This makes it possible to easily login or logout a user from a website presented in your UIWebView.

Checkout NSHTTPCookieStorage* for all the details.

Labels: , , ,

Monday, August 25, 2008

AdMob Ads break in UIWebView

AdMob ads are broken in UIWebViews (a UIWebView is an iPhone native browser control) for two reasons.
  • AdMob requires a user agent and when you tell AdMob you are on an iPhone AdMob will provide ads with direct links to the App Store. This would be great, but Apple servers respond with "Internal Server Error" when AdMob ads link to the appstore through UIWebView. The same links work great in iPhone Safari.
  • When a page containing a UIWebView loads with an AdMob ad, the page is instantly redirected to about:blank. Canceling this redirect in shouldStartLoadWithRequest causes other links on the page to become unresponsive!
  • If you know how to get this working please comment.

Update: after some badgering AdMob has responded here.

Labels: , , ,

Saturday, August 23, 2008

Load web image into iphone native applicaiton UIImage with no NSURLConnection code required

It turns out it is trivial to load images into iPhone native applications.
This simple function will do all of the work. No UIWebView required no NSURLConnection required. Just dataWithContentsOfURL.

-(UIImage*) newUIImageWithURLString:(NSString*)urlString
{
return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
}

Usage:

UIImage* myUIImage = [self newUIImageWithURLString:@"http://example.com/images/myimage.jpg"];

That's it!

It is important to note that +(NSData*) dataWithContentsOfURL:(NSURL*)url blocks. This will not do an asynchronous load. :( I am looking into another more involved solution that will load images on a separate thread.

Labels: , , ,

@property and assign, retain, copy

From The Objective-C 2.0 Programming Language here is how assign retain and copy will be effectively implemented by @synthesize and how they should be functionally implemented by you if not synthesized.

property = newValue;
// retain
if (property != newValue)
{
[property release];
property = [newValue retain];
}
// copy
if (property != newValue)
{
[property release];
property = [newValue copy];
}

Labels: , , ,

Tuesday, July 22, 2008

MD5 hash on iPhone with cocoa and Objective-C

In beta 7 OpenSSL has been removed from the iPhone SDK. However, MD5 is still available.

Simply import CommonCrypto as follows:
#import < CommonCrypto/CommonDigest.h >

Then add this C function to your objective-c class between the @implementation and @end statements (if you like).

NSString* md5( NSString *str )
{
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
];
}


I am sure you can just use NSData for this but this is the way an example was posted on apple forums. Please feel free to add an NSData based solution.

Read the post here "http://discussions.apple.com/thread.jspa?threadID=1509152&tstart=96"

Labels: