網頁

2012年7月11日 星期三

實現Custom URL Schemes

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考來源:Implementing Custom URL Schemes
說明:
URL Schemes,通常運用在從自己的app,透過NSURL溝通其他app時,的一個機制。
<URL Schemes的介紹請參考:ios URL Scheme Reference>

Registering Custom URL Schemes


在這邊就不多做說明,先來說明設定方式,如圖:

到TARGETS->info中新增一個Add URL TYPE
並在其中添加相關資訊,如圖:
在圖中需要注意的只有:identifier、URL Schemes、Role
其它如icon...等,其實不怎麼重要,唯一可能就是若你的app提供多個第三方app跳轉,那除了文字之外,直接可以用圖片去選擇,你要開啓的外部app。
最重要的就是你的URL Schemes,這就是其他app呼叫你的app時,必須提供的辨識名稱,若你的app不需被呼叫,或者呼叫後,不須由其他app返回,基本上就不需要設定。



Handling URL Requests


設定好相關資訊後,重點就是處理返回的URL Requests。
可以根據下列方法去實現:


其中application:didFinishLaunchingWithOptions:是負責判斷該app是否被呼叫,若return YES,則application:openURL:sourceApplication:annotation:或者application:handleOpenURL:就會被執行,然後開始處理傳遞過來的URL,如圖6-2。<至於如何傳遞可參考:app溝通其他外部程式(一)>






若你的app正在運行,只是在後台暫停運行,當你的url到達時,系統就會直接調用application:openURL:sourceApplication:annotation:,若你的系統沒有實現該方法,則會調用application:handleOpenURL:,故這兩個方法可以同時存在,系統會自動判斷目前的ios版本是否4.2之後或者4.1之前。如圖6-3:


Note: Apps that support custom URL schemes can specify different launch images to be displayed when launching the app to handle a URL. For more information about how to specify these launch images, see “Providing Launch Images for Custom URL Schemes.”
若要使用URL Scheme圖片,就請參考Note


所有的資訊均會透過url去傳遞到其他的app,你可以參考<NSURL根據RFC 1808下的解析方式>,去制定你的url。
關於程式的參考,可以參考本文開頭,官方所寫文件,其中有範例,或者參考<app溝通其他外部程式(一)>




注意:一定要驗證你的url,不管是傳進還是傳出,驗證的方式很多種,包含加密,可以參考apple  “Validating Input And Interprocess Communication” inSecure Coding Guide 去處理你的url,以避免問題發生。
關於NSURL Scheme reference請參考:ios URL Scheme Reference




As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

NSURL根據RFC 1808下的解析方式

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考資料:RFC 1808 文件How to Parse an NSURL Object
說明:
ios在URL的使用下,除了可以利用URL Scheme的方式,呼叫外部app,或者開啟iTunes等應用<相關文章:ios URL Scheme Referenceapp溝通其他外部程式(一)>,還可以依照RFC 1808的規則,去解析URL的成分。

<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>

根據規則,可以解析URL的成分,如下:
 
NSURL *url = [NSURL URLWithString:
 @"http://some-site.com:999/dir1/dir2;param?field-1=value-1&field-2=value-2#anchor1"];
 
NSLog(@"Scheme: %@", [url scheme]); 
NSLog(@"Host: %@", [url host]); 
NSLog(@"Port: %@", [url port]);     
NSLog(@"Path: %@", [url path]);     
NSLog(@"Relative path: %@", [url relativePath]);
NSLog(@"Path components as array: %@", [url pathComponents]);        
NSLog(@"Parameter string: %@", [url parameterString]);   
NSLog(@"Query: %@", [url query]);       
NSLog(@"Fragment: %@", [url fragment]);

The output is shown below:






As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

app溝通其他外部程式(一)

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考來源:Communicating with Other Apps


說明:
透過NSURL和UIApplicetion的功能,可以呼叫外部的應用程式。
其中todolist為URL Scheme,是讓應用程式間,彼此知道對方代號的名稱


Note: If more than one third-party app registers to handle the same URL scheme, there is currently no process for determining which app will be given that scheme.
這是Guide中特別標示的一段,表示若多個app均用相同的URL scheme,則目前無法判斷。
有趣的是,國外網站有做一個搜尋目前已經使用過的scheme,目的就是為了盡量避免使用相同的scheme,並且幫助他們建立數據庫:Zwapp


關於URL scheme的介紹,可參考:ios URL Scheme Reference


//使用下方的語法,就可以透過你的app去呼叫外部的應用程式,但前提是必須取得外部應用程式的URL Scheme,才可以呼叫得到。 
NSURL *myURL = [NSURL URLWithString:@"todolist:"];
//『todolist』就是外部app的URL Scheme
[[UIApplication sharedApplication] openURL:myURL];

至於如何呼叫外部app後,做一些事情後,再返回原本的app?


若要帶一些參數呢??

<解析回傳的URL使用方式請參考:NSURL根據RFC 1808下的解析方式>


若想要返回dic的資料呢??


ios提供給我們一個很好的方法,就請參考下一篇文章:app溝通其他外部程式(二)


ps:
要注意的有:從外部app返回後,是返回最後app關到背景的頁面,無法指定跳到任意頁面。
除非動一點小手腳,這邊可以參考UIApplicationDelegate Protocol Reference有興趣的,可以參考,這邊就不多做討論。

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

2012年7月10日 星期二

ios URL Scheme Reference

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考文檔:Apple URL Scheme Reference.


說明:
首先我們在app應該常見到很多功能像是:直接從app中開啓信箱、撥打電話、寄送sms、開啓地圖、開啓youtube、開啓itunes...等等功能,其實都是透過URL scheme去操作。
例如,如果你的IOS應用程序顯示的電話號碼,只要點擊該號碼,app就可以直接撥打電話。同樣,點擊iTunes的鏈接,啟動iTunes應用程序和播放鏈接指定的歌曲。


如果你想從你的IOS應用程序,或從在Safari上運行的頁面,啟動一個系統的應用,你可以閱讀此文件。文件包含使用openURL:UIApplication對象共享的方法,以打開的URL和HTML範例。
欲了解更多信息:就要參考openURL:的使用方法,還有UIApplication Class Reference


其中apple也提供了許多的方法,去讓你的app呼叫其他的內建application。請參考:

這邊可以查到是否你的URL Scheme是否有跟別人重復:Zwapp
基本上寫入自己的bundle identifier就不會重復到。

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

2012年7月9日 星期一

ARC專案中,針對單一檔案『取消arc的使用』

【環境:xCode4.3.2 , ios5.1 , USE ARC】
說明:
因為ios版本更新速度,舊的sdk等等,本來就不是用ARC去編寫,這樣讓使用者會有很多困擾,所以開發者中心亦提供此種方法,讓使用者可以在ARC的專案環境下,同時使用非使用ARC的SDK,主要利用 -fno-objc-arc 指令的使用
路徑位置:Targets -> Build Phases -> Compile Sources. 




As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

ios5 use Twitter & Accounts framework

【環境:xCode4.3.2 , ios5.1 , USE ARC】
參考資源:
twitter開發者頁面
ios Twitter framework
The Accounts and Twitter Framework on iOS 5 by Peter Friese
The official Twitter framework example from Apple is available for download.

說明:此篇文章包含兩個按鈕,方法是使用ios 5的twitter app,你可以在手機中的ios 5設定中的Twitter按鈕中設定帳號資訊,並啓用,接下來你可以去你twitter帳戶中看到,如圖:

表示你的帳戶已經啓用該ios的應用程式,此文章是利用這個應用程式權限的使用,去做twitter的操作。
ps:twitter另有別的方法可以用你自己申請的twitter app,當作基本權限的取用設定,有興趣可以去ios Twitter framework,使用的流程類似facebook SDK

此教學程式,一個按鈕為驗證是否使用該帳號連接你自己的app,如下,若確定使用,則按鈕無效果。


另一個為發送tweet,若無帳號,則要求帳號,若有帳號未開啟app權限,則提示使用者,如下。


Start:


step1:加入 framework :

step2: 寫入相關必要資訊
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
@property (strong, nonatomic) ACAccountStore *accountStore; 
@property (strong, nonatomic) NSArray *accounts;
@synthesize accounts = _accounts;
@synthesize accountStore = _accountStore;

此為第一個按鈕

#pragma mark - login
- (void)login_twitter
{
    [self fetchData];
}

- (void)fetchData
{
    if (_accounts == nil) 
    {
        if(_accountStore == nil) 
        {
            self.accountStore = [[ACAccountStore alloc] init];
        }
        ACAccountType *accountTypeTwitter = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        
        [self.accountStore requestAccessToAccountsWithType:accountTypeTwitter 
                                     withCompletionHandler:^(BOOL granted, NSError *error) {
                                         if(granted) 
                                         {
                                             dispatch_sync(dispatch_get_main_queue(), ^{
                                                 self.accounts = [self.accountStore accountsWithAccountType:accountTypeTwitter];
                                                 
                                             });
                                              
                                         }
                                         else { 
                                             // User denied access to his Twitter accounts 
                                             NSLog(@"用戶拒絕使用twitter帳戶連接app");
                                         }
                                     }
         ];
    } 
    else 
    { 
        // This iOS verion doesn't support Twitter. Use 3rd party library 
    }
    
}

第二個按鈕
#pragma mark - tweet

- (void)send_tweet
{
    //建立viewcontroller
    TWTweetComposeViewController *tweetTOtwitter = [[TWTweetComposeViewController alloc] init];
    
    //設定推文的內容
    [tweetTOtwitter setInitialText:@"N11 Studio Twitter API 測試。"];
    
    //推文中加入圖片資訊
    [tweetTOtwitter addImage:[UIImage imageNamed:@"logo.png"]];
    
    //推文中加入網址超連結資訊
    [tweetTOtwitter addURL:[NSURL URLWithString:[NSString stringWithString:@"http://n11studio.blogspot.tw/"]]];
    
    //顯示viewcontroller
    [self presentModalViewController:tweetTOtwitter animated:YES];
    
    //按下Send或是Cancel時的處理動作(block)
    [tweetTOtwitter setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
        NSString *tweet_action;
        
        switch (result) {
            case TWTweetComposeViewControllerResultCancelled:
                tweet_action = @"取消";
                break;
                
            case TWTweetComposeViewControllerResultDone:
                tweet_action = @"完成";
                break;
                
            default:
                break;
        }
        
        
        NSLog(@"%@", tweet_action);
        
        //移除viewcontroller
        [self dismissModalViewControllerAnimated:YES];
    }];
}

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我


歡迎轉載,請註明出處,感謝。

2012年7月5日 星期四

IOS 使用 facebook SDK 教程(二)『Adding Social Context』


相關文章更新:2012/12/21

【facebook SDK 3.1】what is NEW?

【facebook SDK 3.1】教程1-設定

【facebook SDK 3.1】教程2-登入,認證,登出



==請改用sdk 3.1==


【環境:xCode4.3.2 , ios5.1 , USE ARC】
說明:
說明:此教程主要為facebook 2012 08後,官方提供的主要流程。
此教程為文件中的『Adding Social Context』,理解後的教學執行方式。
在這篇文章中,可以學到如何取得用戶社交圈的各類資訊,和如何使用『分享』、『邀請』等facebook常見功能。
建議將IOS 使用 facebook 教程『Getting Started』理解後,再來觀看文章。
官方網站教程:http://developers.facebook.com/docs/mobile/ios/build/#graph

Step 6: Using the Graph API


Facebook Graph API 提供一種簡單的、一致性的Facebook social graph使用方法,此種方法可以取得用戶的社交圈的各類資訊(e.g., people, photos, events, and fan pages) and the connections between them (e.g., friend relationships, shared content, and photo tags)

你可以透過此種方法(Graph API )去請求相關你想要的訊息,例如:用戶資訊
 
// get information about the currently logged in user
[facebook requestWithGraphPath:@"me" andDelegate:self];

// get the posts made by the "platform" page
[facebook requestWithGraphPath:@"platform/posts" andDelegate:self];

// get the logged-in user's friends
[facebook requestWithGraphPath:@"me/friends" andDelegate:self];

請將你的程式碼加入FBRequestDelegate的協議(protocol),這樣才可以去處理你所請求的回應(your request responses)。
注意:server適用json去傳遞回應你的請求,facebook SDK中已經有這個JSON library。
所以若你的程式碼中,必須包含request:didFailWithError:避免解析錯誤的狀況發生。

『回應請求』若成功,程式就會呼叫request:didLoad:,『回應』傳遞的delegate可能為NSArray、NSDictionary。

ps:基本上你加入一個按鈕,然後請求其中一個回應,並加入FBRequestDelegate的協議,並加入下列code:
 
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"received response");
}
就可以看到你的回應已經成功。



你的app也可以根據自己的需求,去解析自己善用的方式。


Step 7: Integrating with Social Channels


Social Channels能使用戶去po文到他們朋友的牆上或者寄送邀請給他人,facebook SDK提供方法經由Facebook Platform dialogs去整合social channels ,目前支援的dialogs有以下:
  • Feed Dialog - the dialog used for publishing posts to a user's News Feed.(分享)
  • Requests Dialog - the dialog used to send a request to one or more friends.(寄送邀請)
這允許你的app去提供基本的facebook功能,不需要去建立native dialogs, make API calls, or handle responses.詳細請參閱mobile distribution guide
要使用此功能,請在你的class中加入『FBDialogDelegate』的協議(protocol),這樣才可以使用dialog的功能。

Requests

Requests』是最好的方法使用戶邀請他們的朋友去使用你的app,你的app可以使用『Request dialog』讓用戶邀請朋友去使用你的app。
若用戶的裝置支援此功能,每當朋友發送請求,除了他們一般在Facebook中的通知,他們將獲得Facebook的本機應用程序推送通知。
 
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"Come check out my app.",  @"message",
                               nil];

    [facebook dialog:@"apprequests"
                  andParams:params
                andDelegate:self]; 

News Feed

這就是大家所熟悉的『分享』功能。
使用方法如下:
 
[facebook dialog:@"feed" andDelegate:self];

Timeline and Open Graph

這個部分有興趣就到官網看吧,這邊不多做介紹。





As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

2012年7月4日 星期三

IOS 使用 facebook SDK 教程(一)『Getting Started』

相關文章更新:2012/12/21

【facebook SDK 3.1】what is NEW?

【facebook SDK 3.1】教程1-設定

【facebook SDK 3.1】教程2-登入,認證,登出



==請改用sdk 3.1==

【環境:xCode4.3.2 , ios5.1 , USE ARC】
說明:此教程主要為facebook 2012 08後,官方提供的主要流程。
此教程為文件中的『Getting Started』,理解後的教學執行方式。
在這篇文章中,你可以學到一開始的設定,包含如何在你的app中使用SSO,一鍵授權,一建取銷售權的方法。
官方網站教程:http://developers.facebook.com/docs/mobile/ios/build/#graph

Getting Started

Step 1: Registering your iOS App with Facebook


先到facebook開發者中心,建立你的application,這個application負責溝通facebook去使用facebook允許你的一些動作。
ps:至於如何去申請的facebook的應用程序上,開啓SSO的設定,就請去官方文件參考:Setting up for Facebook iOS App Distribution and Deep Linking

如圖:

Step 2: Installing the iOS SDK


下載XcodeGitClone the GitHub repository
其中GitHub repository裡面的download裡的資料,包含此次要用的facebook ios SDK和他的範例。
當你下載完畢後,首先下面的步驟就是教你如何啓用 Single Sign-On (SSO),這是一個關鍵的執行功能,在這個步驟之後,將會帶你走一次IOS app的設定。

Step 3: Implementing Single Sign-On (SSO)


Single Sign-On (SSO)是一個特別的功能,讓使用者利用 Facebook identity登入你的ios app。
使用者將不需要輸入帳號、密碼等資訊去使用facebook ios app。
此外可以透過facebook app取得使用者的授權資訊,含社交功能和帳戶資訊。

SSO主要的功能就是在他們自己的裝置中,將使用者重新導向到facebook app,因為使用者已經登入facebook,他們將不需要再一次的填寫自己的帳號、密碼。
使用者將看到app提供的『權限驗證對話框』,且會將access_token重新導向到ios app中。

開發者必須瞭解到facebook的SSO機制,會根據用戶的設備安裝,其表現略有不同。
下列在某些IOS的配置上會發生的情況:

  • 若用戶本身有裝facebook開發並發佈的app應用程式,則你的app中的facebook SDK會打開facebook app的驗證對話框。用戶授權或者拒絕後,facebook app會返回到你的app,並傳遞access token, expiration, and any other parameters the Facebook OAuth server may return.
  • 如果沒有安裝3.2.3版本的Facebook app,SDK將打開在Safari手機瀏覽器驗證對話框。用戶授予或撤銷授權後,Safari瀏覽器返回你的app。類似的Facebook app的授權,允許多個應用程序共享相同的Facebook用戶access_token通過Safari瀏覽器的cookie。
  • 若app的程式運行的ios版本,不支持多任務.....(應該是指舊的ios,所以這點不須考慮)

facebook將盡可能會要求使用者去更新到最新的facebook app,所以開發者也追隨facebook的腳步吧....
ps:這點還蠻白爛的....因為像我用shareKit這個套件的話,就被迫強迫更新為最新的版本,不然就不能用摟~~~

將SSO加入到你的app中是非常簡單的一件事情,將在下方跟你介紹。

下面會先教你一開始的程式設定->將你的facebook app access token放入專案中->執行

ps:至於facebook app在官網的開啓sso設定,相當簡單,請到Setting up for Facebook iOS App Distribution and Deep Linking觀看教學

Create your iOS Project

首先建立一個View-based Application project template專案。
並將你下載的IOS Facebook SDK 檔案加入到你的專案中,有幾種方法加入,但最簡單的就是將src這個資料夾的檔案全部拖曳到你的專案中。其中.pch and .xcodeproj files.的兩個檔案可以排除掉,如圖:

Creating an iOS Facebook SDK Static Library
若你在建立你的專案的時候選擇啟用ARC,你必須去使用static library version of the iOS Facebook SDK替代直接拖曳src的方式。
你必須先打開你的command line,並於該資料夾的scripts下執行
 
sh build_facebook_ios_sdk_static_lib.sh

編譯後,就會跑出一段訊息,告訴你創建的static library version of the iOS Facebook SDK存放的位置,將整個檔案加入到你的專案中即可。 

Modify the application delegate header file

Step 1 在你的專案中加入此標頭檔。
#import "FBConnect.h" 
Step 2 在你的class中加入protocol。
@interface MyGreatIOSAppAppDelegate : NSObject <UIApplicationDelegate,FBSessionDelegate>
Step 3.   Set up the application delegate header file by creating instance variable:
Facebook *facebook; 
Step 4.   Add a property for an instance of the Facebook class:
@property (nonatomic, retain) Facebook *facebook; 


Modify the application delegate implementation file


Step 1 Synthesize the facebook property (this creates getter and setter methods):
@synthesize facebook; 

Step 2 Within the body of the application:didFinishLaunchingWithOptions: method create instance of the Facebook class using your app ID (available from the App Dashboard):
facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self];
This instance is used to invoke SSO, as well as the Graph API and Platform Dialogs from within the app.

Step 3.   Once the instance is created, check for previously saved access token information (We will show you how to save this information in Step 6). Use the saved information to set up a "session valid" check by assigning the saved information to the facebook access token and expiration date properties. This ensures that your app does not redirect to the Facebook app and invoke the Auth Dialog if the app already has a valid access_token. If you have asked for offline_access extended permission, then your access token will not expire, but can still get invalidated if the user changes their password or uninstalls your app. Read more on How-To: Handle expired access tokens.
Note: offline_access is deprecated! See the Extending the access token section for information on how to extend the expiration time of an access token for active users of your app.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
} 

Step 4.   Check for a valid session and if it is not valid call the authorize method which will both log the user in and prompt the user to authorize the app:
if (![facebook isSessionValid]) {
    [facebook authorize:nil];
}

Step 5.   Add the application:handleOpenURL: and application:openURL: methods with a call to thefacebook instance:
// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url]; 
}

// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [facebook handleOpenURL:url]; 
} 
The relevant method is called by iOS when the Facebook App redirects to the app during the SSO process. The call to Facebook::handleOpenURL: provides the app with the user's credentials.

Step 6.   Implement the fbDidLogin method from the FBSessionDelegate implementation. In this method you will save the user's credentials specifically the access token and corresponding expiration date. For simplicity you will save this in the user's preferences - NSUserDefaults: 
 
- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

} 

最後的長相可能就是下方

Modify the app property list file


最後需要完成的一件事情就是到你的project中的.plist設定相關參數,使你的專案啟用SSO。
找到你建立專案時產生的.plist檔,並在其中按下右鍵新增一行,建立一個URL types,並在URL Schemes中,加入你的 fb[YOUR_APP_ID]  
【注意】必須先是fb的字符串+你去facebook上申請應用程式的APP_ID
如下圖:


到這一步,恭喜你已經完成設定,剩下的就是在你的app加入使用的相關功能。

Test your app


現在只要你輸入好上面的步驟,並填入你所申請的facebook 應用程式 ID,執行你的app後,就會發現認證的對畫框會被打開。
用戶經過確認後,給予你的app權限去訪問他們的facebook信息。
當用戶按下『允許』後,你的app將被授權使用。
若用戶按下『不允許』,則你的app無法被授權。

Step 4: Adding Log Out to your App


當用戶不想在你的app中授權使用他們的帳戶資訊時,就必須使用下列方法去登出
 
[facebook logout];

此將執行 fbDidLogout 的方法,可以藉 FBSessionDelegate 的協議去處理註銷的動作。
注意:執行登出後,你的app將被吊銷登入的權限,將會清除掉app中的access_token。
用戶可經由facebook的隱私設定去修改應用程式權限。
開發者也可以使用Graph API call.去revoke an app's permissions.

接下來就來將logout的按鈕,加到你的app中吧。
    Step 1   加入 logout 按鈕和相關處理程序
    // Add the logout button
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logoutButton.frame = CGRectMake(40, 40, 200, 40);
    [logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(logoutButtonClicked:)
        forControlEvents:UIControlEventTouchUpInside];
    [self.viewController.view addSubview:logoutButton]; 
    
    ps:若是用fb develop上的程式,稍微有點錯誤,在@selector(logoutButtonClicked:)上,少了:的符號

    接下來加入按下按鈕後的動作
    // Method that gets called when the logout button is pressed
    - (void) logoutButtonClicked:(id)sender {
        [facebook logout];
    }
    

    Step 2 添加註銷回調處理程序
     
    - (void) fbDidLogout {
        // Remove saved authorization information if it exists
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:@"FBAccessTokenKey"]) {
            [defaults removeObjectForKey:@"FBAccessTokenKey"];
            [defaults removeObjectForKey:@"FBExpirationDateKey"];
            [defaults synchronize];
        }
    } 
    

儲存,並且執行你的app,按下按鈕後,已經成功地登出,但除非你的app有提示使用者,否則沒有其他視覺上的提示。但若將app全部關掉後,重新執行你的app,則會被要求再次授權。

Step 5: Requesting Additional Permissions

預設情況下,用戶授權app去存取基本公開資訊(一般為facebook的預設情況),若你的app需要更多資訊,你必須從用戶上取得特別的權限。
實現這種方法,是利用NSArray傳遞權限的授權方法。
下方的例子是要求使用者授權的頁面,授權取得user_likes(你說讚的內容)權限read_stream(存取你在動態消息的貼文)權限:
NSArray *permissions = [[NSArray alloc] initWithObjects:
        @"user_likes", 
        @"read_stream",
        nil];
[facebook authorize:permissions];
[permissions release];

在首次登入授權的情況下,將顯示用戶和朋友的權限關係,在範例中(左下圖),這是user_likes(你說讚的內容)權限。
此外延伸的權限將被要求在第二個授權頁面,在範例中(右下圖),這是read_stream(存取你在動態消息的貼文)權限。
你可以參考permissions guide去獲得更多的使用方式。
所有的權限設定請參考permissions guide
在這邊建議,因為你如果設定越多的權限,將會影響用戶的使用心情,會予以授權的用戶將較少。故建議你只跟用戶要求你app中所必須需要的權限即可。

現在讓我們測試一下程式吧,為了讓你剛剛的project更加方便地使用,請將下列的code,加入到你的專案中:
if (![facebook isSessionValid]) {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
            @"user_likes", 
            @"read_stream",
            nil];
        [facebook authorize:permissions];
        [permissions release];
}

使用上述code時,注意,你可以特別用一個按鈕去增加這個額外權限,或者放在一開始登入就立即確認,均可,若你直接使用貼在你的code中,並和之前的if (![facebook isSessionValid])重復到,可能會產生額外的錯誤效果

儲存,並且執行,你就可以看到你的app已經取得用戶的額外權限。




As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

2012年7月2日 星期一

File Management(ㄧ)目錄清單位置&list

【環境:xCode4.3.2 , ios5.1 , USE ARC】
說明:

首先要搞清楚 simulator 的 app 資料存放的位置
基本上裡面都會有四個檔案
  1. 一個自己命名的app。
  2. Documents
  3. Library
  4. tmp


<以下範例>
 
    NSLog(@"%@",NSHomeDirectory());//取得目前以此app名稱為根目錄的資料夾位置
    
    NSFileManager *file = [NSFileManager defaultManager];//利用類別方法取得檔案系統的一個預設NSFileManager實體
    //利用這個,可以對根目錄裡的檔案進行操作。
    NSDirectoryEnumerator *dir = [file enumeratorAtPath:NSHomeDirectory()];
    NSString *currPath;
    //依序列出資料夾下的資料有哪些
    while (currPath = [dir nextObject]) {
        NSLog(@"Found : %@",currPath);
    } 

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。

2012年7月1日 星期日

Omnigraffle 5.3.6 自製繁體中文語言包

【環境:Omnigraffle 5.3.6】
說明:大部份已經中文化,因為某些中文覺得沒有翻譯的sense,所以改為英文。
製作的有點快,有問題請於下方留言。

檔案下載

檔案下載後請按照步驟放入即可

顯示套件內容

檔案路徑

頁面顯示如下



As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

歡迎轉載,請註明出處,感謝。