The Objective-C2.0 provides the category mechanism to expand existing objective-C classes. We can use the category mechanism to solve the problem that some methods of the existing cocoa framework have too many versions.
For example, in uiwebview, the system displays the content to the Web View Based on the HTML attributes. At this time, when it encounters the retina display flag, uiwebview automatically calls the setcontentscalefactor method of uiview internally to set the resolution scaling factor. This method is available only after ios4.0 and later. If the ios3.2 system is used, the application will crash.
We can solve this problem by adding:
@ Interface uiwebview (notexpected)
-(Void) setcontentscalefactor :( cgfloat) factor;
@ End
@ Implementation uiwebview (notexpected)
-(Void) setcontentscalefactor :( cgfloat) factor
{
If (! [[Uiview class] respondstoselector: @ selector (animatewithduration: Animations :)])
Return;
Else
[Super setcontentscalefactor: factor];
}
@ End
Use the animatewithduration: Animations: Method in the uiview to check whether the system is ios4.0 or later. If it is not 4.0, it is returned directly. If it is version 4.0 or later, you can directly call the uiwebview parent class, that is, the setcontentscalefactor method of uiview.