2012-01-23 16 views
21

私がTwitterアプリを使って開きたいページ:私のiPhoneアプリからtwitterアプリでTwitterページを開くには?

https://twitter.com/#!/PAGE

私は次のコードを使用Twitterアプリを開くには:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://https://twitter.com/#!/PAGE"]]; 
[[UIApplication sharedApplication] openURL:urlApp]; 

をしかし、このコードはしていないようです期待どおりの仕事、私は私が表示したいページなしで起動された唯一のTwitterアプリを持っています。

答えて

38

次のURLを探しています:Twitterのは、すべてのデバイスにインストールされていないことを

twitter:///user?screen_name=PAGE 

注意。 openURLメソッドの結果を確認する必要があります。それが失敗した場合は、Safariで通常のURLでページを開きます。

+4

スウィフト3についても同様であると考えています。 2つを使用して私のために働いた。 –

14

私はこの質問に対するかなり遅い応答を知っています。私はMuratの答えは絶対正しいと同意します。 次のように単純にチェックを追加します。

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter:///user?screen_name=PAGE]]; 

if ([[UIApplication sharedApplication] canOpenURL:urlApp]){ 
     [[UIApplication sharedApplication] openURL:urlApp]; 
    } 

を私は願っています、これは誰かに役立ちます。乾杯!! :)

2

@Alexey:あなただけのこの操作を行うアプリケーションからツイッターを起動する方法を知りたい場合は、次の

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://"]]; 
    if ([[UIApplication sharedApplication] canOpenURL:urlApp]){ 
     [[UIApplication sharedApplication] openURL:urlApp]; 
    }else{ 
     UIAlertView *appMissingAlertView = [[UIAlertView alloc] initWithTitle:@"Twitter App Not Installed!" message:@"Please install the Twitter App on your iPhone." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
     [appMissingAlertView show]; 
     [appMissingAlertView release]; 
    } 
11

それが既にインストールされている場合、次のコードは、TwitterアプリにTwitterのページを開き、それ以外のさえずりを開きますサファリ:

NSURL *twitterURL = [NSURL URLWithString:@"twitter://user?screen_name=username"]; 
if ([[UIApplication sharedApplication] canOpenURL:twitterURL]) 
    [[UIApplication sharedApplication] openURL:twitterURL]; 
else 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.twitter.com/username"]]; 

は、自分の名前で「ユーザー名」を置き換えることを忘れないでください。

+0

また、LSApplicationQueriesSchemesをinfo.plistに追加することを忘れないでください。 – appthumb

0

これはSwiftに必要な完全なコードです。私はスウィフト4を使用していますが、私はそれがこの回答のURLに示される3つのスラッシュがあるDon't forget to allow access to using the scheme in your info.plist file.

let Username = "YOUR_USERNAME_HERE" 
let appURL = NSURL(string: "twitter:///user?screen_name=\(Username)")! 
let webURL = NSURL(string: "https://twitter.com/\(Username)")! 
let application = UIApplication.shared 
if application.canOpenURL(appURL as URL) { 
     application.open(appURL as URL) 
    } else { 
     // if Instagram app is not installed, open URL inside Safari 
     application.open(webURL as URL) 
    } 
関連する問題