iphone - 如果调用新代码,则运行较低版本IOS的用户将如何处理?

标签 iphone objective-c ios

我是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.
}

最佳答案

如果您在编写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。

关于iphone - 如果调用新代码,则运行较低版本IOS的用户将如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11911790/

相关文章:

ios - 在 Objective C 中,如何使用 CATileLayer 进行分页聚类

ios - ios-Uitableviewcell中的Uilabel和UIimageview

objective-c - 将左键添加到 UINavigationBar (iPhone)

iphone - 努力使用界面生成器来设置我的应用程序布局

objective-c - 从发行版本中删除NSLog

ios - 自定义动画不起作用

c# - iPad 上的 iOS 应用程序删除点十进制值

iphone - 提交单独的 iPhone 和 iPad 应用程序版本时的命名约定

objective-c - 获取并突出显示 NSTextView 中的当前单词

iphone - 在单独的 iOS 线程上延迟执行