2012-01-23 47 views
21

Trang tôi muốn mở sử dụng ứng dụng twitter:Cách mở trang twitter trong ứng dụng twitter từ ứng dụng iphone của tôi?

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

Để mở ứng dụng twitter tôi sử dụng đoạn mã sau:

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

Nhưng mã này dường như không làm việc như mong đợi, tôi chỉ nhận được ứng dụng twitter mà không có trang mà tôi muốn hiển thị.

Trả lời

38

Bạn đang tìm kiếm url sau:

twitter:///user?screen_name=PAGE 

Lưu ý rằng Twitter không được cài đặt trên tất cả các thiết bị. Bạn nên kiểm tra kết quả của phương pháp openURL. Nếu không thành công, hãy mở trang trong Safari bằng url thông thường.

+4

Có ba dấu gạch chéo thể hiện trong URL cho câu trả lời này. Sử dụng hai làm việc cho tôi. –

14

Tôi biết phản ứng khá muộn của nó đối với câu hỏi này và tôi đồng ý rằng, câu trả lời của Murat là hoàn toàn chính xác. Chỉ cần thêm séc như sau:

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

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

Tôi hy vọng điều này sẽ giúp ai đó. Chúc mừng !! :)

2

@Alexey: Nếu bạn chỉ muốn biết làm thế nào để khởi động twitter từ ứng dụng của bạn thực hiện điều này:

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

Đoạn mã dưới đây sẽ mở ra trang twitter trên ứng dụng twitter nếu nó đã được cài đặt, nếu không mở twitter trên safari:

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"]]; 

Đừng quên thay thế 'username' với tên của bạn.

+0

Cũng nhớ thêm LSApplicationQueriesSchemes vào info.plist. – appthumb

0

Đây là mã đầy đủ cần thiết trong Swift. Tôi đang sử dụng Swift 4 nhưng tôi tin rằng nó là như nhau cho Swift 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) 
    } 
Các vấn đề liên quan