我是iOS开发的新手,我一直想知道在iOS 4上运行我的应用程序的用户是否尝试运行此代码:
//POST TWEET//
- (void)showTweetSheet
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
switch(result) {
case TWTweetComposeViewControllerResultCancelled:
break;
case TWTweetComposeViewControllerResultDone:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Tweet Sheet has been dismissed.");
}];
});
};
[tweetSheet setInitialText:@"Check out this cool picture I found on @Pickr_"];
// Add an URL to the Tweet. You can add multiple URLs.
if (![tweetSheet addURL:[NSURL URLWithString:ImageHost]]){
NSLog(@"Unable to add the URL!");
}
[self presentViewController:tweetSheet animated:YES completion:^{
NSLog(@"Tweet sheet has been presented.");
}];
}
会发生什么?应用程序是否会因错误而终止,或者代码是否会运行?我如何正确实现特定于操作系统的功能?我会用这样的东西:
NSString *DeviceVersion = [[UIDevice currentDevice] systemVersion];
int DeviceVersionInt = [DeviceVersion intValue];
if (DeviceVersionInt > 5)
{
//do something.
}
else
{
//don't do a thing.
}
答案 0 :(得分:7)
如果您编写iOS5功能而不检查它们是否可用,它将在iOS 4上崩溃。尝试像这样实现Twitter
Class twClass = NSClassFromString(@"TWTweetComposeViewController"); if (!twClass) // Framework not available, older iOS { //use iOS4 SDK to implement Twitter framework } else { //use Apple provided default Twitter framework
}
确保您添加了带有弱链接的Twitter Framework。
答案 1 :(得分:1)
我想象它会和其他api一样工作。如果链接到不是以前版本的函数,程序将在尝试调用该函数时崩溃。因此,正如您所演示的那样,使用版本开关来避免崩溃。
答案 2 :(得分:1)
应用程序会崩溃。如果要实现基于iOS的功能,可以使用多种方法。请参阅此question。