Iphone development my Sina Weibo client-user logon to OAuth authorization (1.3)

Source: Internet
Author: User
Tags oauth

In the previous article, we have completed the UIDialogWindow custom pop-up window component. In this article, we will complete the user's OAuth authorization and authentication function. The effect is shown in.

1. Create a class file of the UIViewController subclass type named LoginViewController. Remember to check the With XIB user interface option when you are new. Next, add the ViewController to the window for display and continue.

2. Open the MinBlog4SinaAppDelegate. h file automatically created by xcode and add the following code:


# Import <UIKit/UIKit. h>

@ Class LoginViewController;

@ Interface MinBlog4SinaAppDelegate: NSObject <UIApplicationDelegate> {

IBOutlet UIWindow * window;

IBOutlet LoginViewController * loginViewController;

}

@ Property (nonatomic, retain) IBOutlet UIWindow * window;

@ Property (nonatomic, retain) IBOutlet LoginViewController * loginViewController; @ end

3. Open the MinBlog4SinaAppDelegate. m file and add the following code:

# Import "MinBlog4SinaAppDelegate. h

"# Import" LoginViewController. h"

@ Implementation MinBlog4SinaAppDelegate

@ Synthesize window;

@ Synthesize loginViewController;

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {

[Application setStatusBarHidden: NO];

[Self. window addSubview: loginViewController. view];

[Self. window makeKeyAndVisible];

Return YES;

}

-(Void) dealloc

{

[Window release];

[LoginViewController release];

[Super dealloc];

}

4. Next we will use the Interface Builder tool to process the UI. Double-click MainWindow. xib file. In this way, IB will open this file and drag a View Controller component from the Library to MainWindow. in xib, and name the class of this component as the LoginViewController we created above, as shown in:

5. Click and select the Min Blog4 Sina App Delegate file, select Connections on the right panel, and a loginViewController is displayed. Then, click the circle following it, then, you can pull a line and drag it to the LoginViewController component created in the preceding four steps to complete the connection, as shown in:

At this point, we have added the view of LoginViewController to the window for display. In this way, we can run it. However, after running it, there is nothing except the white screen.

6. Open the LoginViewController. h file and add the following code:

# Import <UIKit/UIKit. h>

# Import "UIDialogWindow. h

"# Import" Sqlite. h"

# Import "InfoViewController. h"

# Import "BlogClient. h" # import "JSON. h"

@ Interface LoginViewController: UIViewController

{

UIDialogWindow * dialog;

InfoViewController * infoViewController;

NSMutableArray * userList;

BlogClient * blog;

}

@ Property (nonatomic, retain) NSMutableArray * userList;

@ End

The above infoViewController is a UIViewController, which is used to provide the view of the content in the pop-up window. As mentioned in the previous article, the view of the UIDialogWindow component is provided only during use, this infoViewController is used to provide the display content view for the UIDialogWindow component named dialog. In this case, we also need to create a class file of the UIViewController subclass type named InfoViewController. Remember to check the With XIB user interface option when you are new.

7. As shown in Figure 6 at the top, the InfoViewController pop-up window contains a TextView that displays the text description, and a Button that starts execution, and the blue small icon and "Information prompt" in the upper left corner. Open the previous step to create InfoViewController. the H file declares TextView, Button, and Button events. As for the small blue icon and "Information prompt", add IB directly to the xib file. For delegate and SEL onClick, after clicking the start button, the user will call the onClick method of delegate, telling the parent ViewController button of the UIDialogWindow component that calls this dialog is clicked, And the dialog does not process specific services. The specific code of InfoViewController. h is as follows:

# Import <UIKit/UIKit. h>

# Import "Global. h"

@ Interface InfoViewController: UIViewController {

Id delegate;

SEL onClick;

IBOutlet UITextView * textView;

IBOutlet UIButton * startBtn;

}

@ Property (nonatomic, retain) IBOutlet UITextView * textView;

@ Property (nonatomic, retain) IBOutlet UIButton * startBtn;

-(Void) setDelegate :( id) aDelegate onClick :( SEL) aOnClick;-(IBAction) startAuth :( id) sender;

@ End

8. In the InfoViewController. m code, we used the pngWithPath method defined in Global in the preparation section. The details are as follows:

# Import "InfoViewController. h"

@ Implementation InfoViewController

@ Synthesize textView;

@ Synthesize startBtn;

-(Void) setDelegate :( id) aDelegate onClick :( SEL) aOnClick {delegate = aDelegate;

OnClick = aOnClick;

}

-(IBAction) startAuth :( id) sender

{

If (delegate)

{

[Delegate initialize mselector: onClick withObject: @ "start"];

 

}

}

-(Void) viewDidLoad

{

[Super viewDidLoad];

TextView. font = [UIFont systemFontOfSize: 15];

[StartBtn setBackgroundImage: [[Global pngWithPath: @ "btn_h_bg"] stretchableImageWithLeftCapWidth: 12.0 topCapHeight: 14.0] forState: UIControlStateNormal];

[StartBtn setBackgroundImage: [[Global pngWithPath: @ "btn_h_bg"] stretchableImageWithLeftCapWidth: 12.0 topCapHeight: 14.0] forState: UIControlStateHighlighted];

}

-(Void) dealloc

{

[TextView release];

[StartBtn release];

[Super dealloc];

}

@ End

9. Next we will use IB to process the interface and double-click InfoViewController. open xib, set the View size to a proper size, and set the Background of the View to Clear Color, which is transparent, the others are to drag the corresponding TextView Button Label to the View for proper layout, the approximate effect is as follows:

After the control layout is completed, you need to perform the connection operation, as shown in the following figure:

10. In Step 6, we only complete LoginViewController. h file, and then do InfoViewController, now we continue to complete LoginViewController. the m file is first checked by the account in the sqlite database in the viewDidLoad method, and the BlogClient object blog in the SDK is instantiated. The specific code is as follows, this blog object provides a series of Weibo-related methods, such as oauth authentication, Weibo retrieval, and Weibo Publishing. If you are interested, you can read the sdk source code and I believe it will be helpful.

# Define kOAuthConsumerKey @ "3983859935"

# Define kOAuthConsumerSecret @ "201fea7b1e1203a76a10f3be570f5abb"

@ Implementation LoginViewController

...................

// If an account already exists in the Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.