2015-11-25 6 views
5

こんにちは私がSafariビューコントローラのテーブルビューのセルからiOS 9以上になっている場合は、ユーザーがiOS 7または8を使用している場合、ウェブサイトは標準のSafariアプリで開く必要があります。iOS 9のテーブルビューからSafariビューコントローラを開き、iOS 8または7のSafariで開きます。

これは私が現在使用しているコードで、サファリを開きます。

case 3: { // Follow us section 
     switch (indexPath.row) { 
      case 0: { //Website 
       NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"]; 
       if (![[UIApplication sharedApplication] openURL:url]) { 
        NSLog(@"%@%@",@"Failed to open url:",[url description]); 
       } 
      } 
       break; 

      default: 
       break; 
     } 

    } 
     break; 

私は、このコードは私のウェブサイトでサファリ・ビュー・コントローラを開くべきだと考えています。しかし、私はどちらのコードセットをどのように組み合わせるのか不明です。私は理解して

- (void)openLink:(NSString *)url { 

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]]; 
if (URL) { 
    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL]; 
    sfvc.delegate = self; 
    [self presentViewController:sfvc animated:YES completion:nil]; 
} 

#pragma Safari View Controller Delegate 

- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller { 
[controller dismissViewControllerAnimated:YES completion:nil]; 
} 

が、これはその後、私のテーブルビューのセルdidSelectRowAtIndexPathの下にこのコードを追加

- (void)openLink:(NSString *)url { 

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]]; 
if (URL) { 
    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL]; 
    sfvc.delegate = self; 
    [self presentViewController:sfvc animated:YES completion:nil]; 
} else { 
    // will have a nice alert displaying soon. 
} 

if ([SFSafariViewController class] != nil) { 
    // Use SFSafariViewController 
} else { 
    NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"]; 
    if (![[UIApplication sharedApplication] openURL:url]) { 
     NSLog(@"%@%@",@"Failed to open url:",[url description]); 
    } 
} 

あなたのアドバイス私が従っている

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) { 

それが何であるかのiOSのバージョンを確認するために使用するコードです

 case 3: { // Follow us section 
     switch (indexPath.row) { 
      case 0: { //Website 
       NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]]; 
       if (URL) { 
        SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL]; 
        sfvc.delegate = self; 
        [self presentViewController:sfvc animated:YES completion:nil]; 
       } else { 
        // will have a nice alert displaying soon. 
       } 

       if ([SFSafariViewController class] != nil) { 
        // Use SFSafariViewController 
       } else { 
        NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"]; 
        if (![[UIApplication sharedApplication] openURL:url]) { 
         NSLog(@"%@%@",@"Failed to open url:",[url description]); 
        } 

       } 
      } 
       break; 

      default: 
       break; 
     } 

    } 
     break; 

私はNSStringWithFormatの終わりにURLを削除するコード

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]]; 

のこの行にエラー「宣言されていない識別子のURLの使用」を取得していますがSafariのビューコントローラの作業を行います。ただし、9.0未満のiOSでは8.4アプリケーションがクラッシュします。

答えて

18

標準的で推奨される方法は、OSバージョンではなく機能を確認することです。この例では、SFSafariViewControllerクラスの存在を確認できます。 openLink:

if ([SFSafariViewController class] != nil) { 
    // Use SFSafariViewController 
} else { 
    // Open in Mobile Safari 
} 

編集

あなたの実装が間違っています。

- (void)openLink:(NSString *)url { 
    NSURL *URL = [NSURL URLWithString:url]; 

    if (URL) { 
     if ([SFSafariViewController class] != nil) { 
      SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL]; 
      sfvc.delegate = self; 
      [self presentViewController:sfvc animated:YES completion:nil]; 
     } else { 
      if (![[UIApplication sharedApplication] openURL:url]) { 
       NSLog(@"%@%@",@"Failed to open url:",[url description]); 
      } 
     } 
    } else { 
     // will have a nice alert displaying soon. 
    } 
} 
+0

私が行ったこととそれが表示しているエラーを示す質問が更新されました。 – user5394344

+0

'SFSafariViewController'を安全に使うためにあなたのメソッドを使用していません。それだけでなく、あなたの 'openLink:'メソッドは間違っています。 'SFSafariViewController'を使用しようとすると、そのクラスが利用可能かどうかチェックされません。 – Avi

+0

@Avi iOS 9以上のアプリを作成する場合、Apiの利用可能性を確認する必要はありませんか? – Supertecnoboff

関連する問題